#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define N 4 //课程数目
typedef struct stu
{
long stuID; //学号
char stuname[10]; //名字
char stusex; //性别
int score[N]; //分数
int total; //总分
float aver; //平均分
int line; //排名
}STU;
typedef struct node //创建结点类型
{
STU stu; //数据域
struct node* next;//指向下一个节点的指针
}NODE;
NODE* head = NULL; //定义头指针
void start();
void inputstudent();
void savestudent();
void readstudent();
void printfstudent(NODE* p = NULL);
void rank();//排名次函数
NODE* find_id(long id);
NODE* find_name(char*);
void swap(STU& stu1, STU& stu2);
void paixu1();
void paixu2();
int main()
{
int exit = 0; long id; char name[10]; NODE* pt = NULL;
while (1)
{
start();
char ch = _getch();
switch (ch)
{
case '1': //录入学生信息
inputstudent();
rank();
break;
case '2': //保存学生信息
savestudent();
break;
case '3'://读取学生信息
readstudent();
break;
case '4': //打印学生信息
printfstudent();
break;
case'5': //根据学号查询学生
printf("请输入学号:");
scanf_s("%ld", &id);
pt = find_id(id);
if (pt)
printfstudent(pt);
else
printf("未找到该学号学生的记录!\n");
break;
case'6': //根据姓名查询学生
printf("请输入姓名:");
scanf_s("%s", name, 10);
pt = find_name(name);
if (pt)
printfstudent(pt);
else
printf("未找到该姓名学生的记录!\n");
break;
case '7': //按学号由小到大排出成绩表
paixu1();
printfstudent(); //打印学生信息同时按由高到低排名
break;
case '8': //按姓名字典顺序排序排出成绩表
paixu2();
printfstudent();
break;
case '0':
printf("再见!\n");
exit = 1;
break;
default:
printf("菜单选择错误,请重新选择!\n");
break;
}
if (exit) break;
}
return 0;
}
void start()
{
printf("*****************************************\n");
printf("欢迎使用学生成绩管理系统 *\n");
printf("*1.录入学生信息 *\n");
printf("*2.保存学生信息 *\n");
printf("*3.读取学生信息 *\n");
printf("*4.打印学生信息 *\n");
printf("*5.按学号查询学生的分数及名次 *\n");
printf("*6.按姓名查询学生的分数及名次 *\n");
printf("*7.按学号由小到大排出成绩表 *\n");
printf("*8.按姓名字典顺序排序排出成绩表 *\n");
printf("*0.退出系统! *\n");
printf("*****************************************\n");
}
void inputstudent()
{
int i;
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->next = NULL;
printf("学号:");
scanf_s("%ld", &newnode->stu.stuID);
printf("姓名:");
scanf_s("%s", newnode->stu.stuname, 10);
printf("性别:");
scanf_s(" %c", &newnode->stu.stusex, 1);
printf("%d门课程成绩:", N);
for (i = 0, newnode->stu.total = 0; i < N; i++) { //输入4科成绩并计算总分
scanf_s("%d", &newnode->stu.score[i]);
newnode->stu.total += newnode->stu.score[i];
}
newnode->stu.aver = (float)(newnode->stu.total / N); //计算平均数
if (head == NULL)
head = newnode;
else
{
NODE* p = head;
while (p->next && p->next->stu.total > newnode->stu.total)
p = p->next;
if (p == head && p->stu.total < newnode->stu.total) {
newnode->next = head;
head = newnode;
}
else {
newnode->next = p->next;
p->next = newnode;
}
}
}
void savestudent() //保存学生信息
{
FILE* pf = fopen("pph.txt", "w"); //创建并打开文件
if (pf == NULL) //判断打开文件是否失败
{
printf("打开文件失败\n");
return;
}
NODE* p = head;
while (p != NULL) {
fwrite(&p->stu, sizeof(STU), 1, pf);
p = p->next;
}
fclose(pf);
printf("数据保存成功\n");
}
void readstudent() //读取学生信息
{
FILE* pf = fopen("pph.txt", "r"); //打开文件
if (pf == NULL) //判断打开文件是否失败
{
printf("err!\n");
return;
}
NODE* pt = NULL;
while (1)
{
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->next = NULL;
if (fread(&newnode->stu, sizeof(STU), 1, pf) != 1) {
free(newnode);
break;
}
if (head == NULL)
head = newnode;
else {
if (!pt) {
pt = head;
while (pt->next) pt = pt->next;
}
pt->next = newnode;
pt = newnode;
}
}
fclose(pf);
printf("加载数据成功\n");
}
void printfstudent(NODE* p)
{
int i;
NODE* P = NULL;
if (!p)
P = head;
else
P = p;
while (P != NULL) {
printf("%ld %s %c", P->stu.stuID, P->stu.stuname, P->stu.stusex);
for (i = 0; i < N; i++)
printf(" %d", P->stu.score[i]);
printf(" %d %.2f %d\n", P->stu.total, P->stu.aver, P->stu.line);
if (!p)
P = P->next;
else
break;
}
system("pause");
}
void rank()// 排名次函数
{
int k = 0;
NODE* P = head, * pre = NULL;
while (P != NULL) {
if (P == head)
k++;
else if (pre->stu.total != P->stu.total) {
k++;
}
P->stu.line = k;
pre = P;
P = P->next;
}
}
NODE* find_id(long id) //按学号查找
{
NODE* p = head;
while (p) {
if (p->stu.stuID == id)
return p;
p = p->next;
}
return NULL;
}
NODE* find_name(char* name)//按姓名查找
{
NODE* p = head;
while (p) {
if (strcmp(p->stu.stuname, name) == 0)
return p;
p = p->next;
}
return NULL;
}
void swap(STU& stu1, STU& stu2)
{
STU temp;
temp = stu1;
stu1 = stu2;
stu2 = temp;
}
void paixu1()
{
NODE* p = NULL, * pre = NULL;
if (!head) return;
for (p = head; p->next != NULL; p = p->next)
for (pre = p->next; pre != NULL; pre = pre->next)
if (p->stu.stuID > pre->stu.stuID) //按学号从小到大排序
swap(p->stu, pre->stu);
}
void paixu2()
{
NODE* p = NULL, * pre = NULL;
if (!head) return;
for (p = head; p->next != NULL; p = p->next)
for (pre = p->next; pre != NULL; pre = pre->next)
if (strcmp(p->stu.stuname, pre->stu.stuname) > 0) //按姓名字典顺序排序
swap(p->stu, pre->stu);
}
求大佬帮忙写一下关于链表的代码的注释,最好详细一点