#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HEADER1 "-----------------------------------STUDENT--------------------------------------
"
#define HEADER2 " | number | name | Comp | Math | Eng | sum | ave | mici |
"
#define HEADER3 " |- - - - - - |- - - |- - - |- - - |- - -|- - -| - - - |- - - |
"
#define FORMAT "| %-10s | %-5s|%4d |%4d |%4d |%4d | %.2f | %4d |
"
#define DATA p->data.num,p->data.name,p->data.cgrade,p->data.mgrade,p->data.egrade,p->data.total,p->data.ave,p->data.mingci
#define END "--------------------------------------------------------------------------------
"
int saveflag=0; //是否需要存盘的标志变量
//定义与学生有关的数据结构
typedef struct student
{
char num[10];
char name[15];
int cgrade; //comp,c language
int mgrade; //math
int egrade; //english
int total; //total
float ave; //average
int mingci; //名次
};
//定义每条记录或节点的数据结构,标记为:node
typedef struct node
{
struct student data; //数据域
struct node *next; //指针域
}Node,*Link; //Node为node 的别名,Link是node型指针的别名
//主菜单
void menu()
{
system("cls"); //调用DOS命令,清屏,与clrscr()功能相同
printf("The Students Grade Management System
");
printf(" * * * * * * * * * * * * * * * * * * Menu * * * * * * * * * * * * * * * * * * *
");
printf("* 1 input record 2 delete record *
");
printf("* 3 search record 4 modify record *
");
printf("* 5 insert record 6 count record *
");
printf("* 7 sort record 8 save record *
");
printf("* 9 display record 0 quit record *
");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
");
}
void printheader() //格式化输出表头
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(Node *pp) //格式化输出表中数据
{
Node *p;
p=pp;
printf(FORMAT,DATA);
}
void Wrong() //按键错误提示
{
printf("
* * * * * Error:input has wrong! press any key to continue* * * * *
");
getchar();
}
void Nofind() //未找到该学生信息
{
printf("
= = = = = >Not find this student!
");
}
void Disp(Link l) //显示单链表l中存储的学生纪录,内容为student结构体中定义的内容
{
Node *p;
p=l->next; //l存储的是单链表中头节点的指针,该头节点没有存学生信息,指针域指向的后继节点才有学生信息
if(!p) //p==NULL,NULL在stdlib中定义为0
{
printf("
= = = = = > Not student record!
");
getchar();
return;
}
printf("
");
printheader(); //输出表格头部
while(p) //逐条输出链表中存储的学生信息
{
printdata(p);
p= p->next; //指针移到下一个节点
printf(HEADER3);
}
getchar();
getchar();
printf("
");
}
//作用:定位链表中符合要求的节点,参数findmess[]保存要查找的具体内容,Locateofnum按学号在单链表l中查找,并返回指向该节点的指针,locateofname按姓名在单链表l中查找,将符合条件的节点(可能不止一个)地址存入指针数组中
Node*Locateofnum(Link l,char findmess[]) //按学号查询
{
Node*r;
r=l->next;
while(r)
{
if(strcmp(r->data.num,findmess)==0) //若找到findmess学号的
return r;
r=r->next;
}
return 0; //若未找到,返回一个空指针
}
void Locateofname(Link l,char findmess[],Node*n[]) //按姓名查询
{
Node *r;
int i=0;
r=l->next;
while(r)
{
if(strcmp(r->data.name,findmess)==0) //若找到findmess值的学生姓名
n[i++]=r;
r=r->next;
}
}
//输入字符串并进行长度验证(长度<lens)
void stringinput(char*t,int lens,char*notice)
{
char n[255];
printf(notice); //显示提示信息
scanf("%s",n); //输入字符串
while(strlen(n)>lens)
{
printf("
exceed the required length!
"); //进行长度校验,超过lens值重新输入
printf(notice);
scanf("%s",n);
}
strcpy(t,n); //将输入的字符串拷贝到字符串t中
}
//输入分数,要求分数在0~100之间
int numberinput(char*notice)
{
int t=0;
printf(notice); //显示提示信息
scanf("%d",&t); //输出分数
while(t>100||t<0)
{
printf("
score must in [0,100]!
");
printf(notice);
scanf("%d",&t);
}
return t;
}
//增加学生记录
void Add(Link l)
{
Node*p,*r,*s; //实现添加操作的临时的结构体指针变量
char ch,flag=0,num[10];
r=l;
s=l->next;
system("cls");
Disp(l); //先显示出已有的学生信息
while(r->next!=NULL)
r=r->next; //将指针移到链表最末尾,准备添加记录
while(l) //一次可输入多条记录,直到输入学号为0的记录结束添加操作
{
while(l) //输入学号,保证学号没被使用过,若输入为0,则退出添加记录操作
{
stringinput(num,10,"input number(press '0' return menu):"); //格式化输入学号并检验
flag=0;
if(strcmp(num,"0")==0) //输入为0,退出添加操作,返回主界面
return;
s=l->next;
while(s) //查询学号是否已经存在,若存在则要求重新输入一个未被占用的学号
{
if(strcmp(s->data.num,num)==0) //提前终止循环
{
flag=1;
break;
}
s=s->next;
}
if(flag==1) //提示用户是否重新输入
{
getchar();
printf("===== >The number %s is existing,try again? (y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return;
}
else
break;
}
p=(Node*)malloc(sizeof(Node)); //申请内存空间
if(!p)
{
printf("
allocate memory failure "); // 如果没有申请到,打印提示信息
return; //返回主界面
}
strcpy(p->data.num,num); //将字符串num 拷贝到p->data.num中
stringinput(p->data.name,15,"Name:");
p->data.cgrade=numberinput("C language Score[0~100]:"); //输入并检验分数,分数必须在0~100之间
p->data.mgrade=numberinput("Math Score[0~100]:"); //输入并检验分数,分数必须在0~100之间
p->data.egrade=numberinput("English Score[0~100]:"); //输入并检验分数,分数必须在0~100之间
p->data.total=p->data.egrade+p->data.cgrade+p->data.mgrade; //计算总分
p->data.ave=(float) ((p->data.total)/3); //计算平均分
p->data.mingci=0;
p->next=NULL; //表面这是链表的尾部节点
r->next=p; //将新建的节点加入链表尾部中
r=p;
saveflag=1;
}
return;
}
//按学号或姓名,查询学生记录
void Qur(Link l)
{
int select; //select是1就按学号查,是2就按姓名查,其他则返回主菜单
char searchinput[20]; //保存用户输入的查询内容
Node*p;
Node*n[5]={NULL}; //用于存放按姓名查找的节点的指针
int i=0;
if(! l->next) //若链表为空
{
system("cls");
printf("
= = = = = > No student record!
");
getchar();
return;
}
system("cls");
printf("
===== > 1 Search by number ===== > 2 Search by name
");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1) //按学号查询
{
stringinput(searchinput,10,"input the existing student number:");
p=Locateofnum(l,searchinput); //在链表l中查找学号为searchinput值的节点,并返回节点的指针
if(p) //即p!=NULL
{
printheader();
printdata(p);
printf(END);
printf("press any key to return");
getchar();
}
else
Nofind();
getchar();
}
else if(select==2) //按姓名查询
{
stringinput(searchinput,15,"input the existing student name:");
Locateofname(l,searchinput,n); //在链表l中查找姓名为searchinput值的节点,并将节点的指针保存于数组n中
while((p=n[i++])!=NULL)
{
printheader();
printdata(p);
printf(END);
}
printf("press any key to return");
getchar();
if(n[0]==NULL)
Nofind();
getchar();
}
else
Wrong();
getchar();
}
//删除学生记录,先找到保存该学生记录的节点,然后删除该节点
void Del(Link l)
{
//int sel;
Node *p,*r;
char findmess[20],ch;
if(!l->next)
{
system("cls");
printf("
==== > No student record!
");
getchar();
return;
}
system("cls");
Disp(l);
stringinput(findmess,10,"input the existing student number:");
getchar();
p=Locateofnum(l,findmess);
printf("Are you sure to delete %s (y/n)?",findmess); //显示确认信息,避免误删数据
scanf("%c",&ch);
if(ch=='y'||ch=='Y');
{
if(p) //p!=NULL
{
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next; //将p所指节点从链表中去除
free(p); //释放内存空间
printf("
==== > delete success!
");
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
}
//修改学生记录:先按输入的学号查询到该记录,然后提示用户修改学号之外的值,学号不能修改
void Modify(Link l)
{
Node *p;
char findmess[20];
if(!l->next)
{
system("cls");
printf("
===== > No student record!
");
getchar();
return;
}
system("cls");
printf("modify student record");
Disp(l);
stringinput(findmess,10,"input the existing student number:"); //输入并检验该学号
p=Locateofnum(l,findmess); //查询到该节点
if(p) //若p!=NULL,表明已经找到该节点
{
printf("Number:%s,
",p->data.num);
printf("Name:%s,",p->data.name);
stringinput(p->data.name,15,"input new name:");
printf("C language score:%d,",p->data.cgrade);
p->data.cgrade=numberinput("C language Score[0~100]:");
printf("Math score:%d,",p->data.mgrade);
p->data.mgrade=numberinput("Math Score [0~100]:");
printf("English score:%d,",p->data.egrade);
p->data.egrade=numberinput("English Score [0~100]:");
p->data.total=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=(float)(p->data.total/3);
p->data.mingci=0;
printf("
===== > modify success!
");
Disp(l);
saveflag=1;
}
else
Nofind();
getchar();
getchar();
}
//插入记录,将学号查询到要插入的节点的位置,然后在该学号之后插入一个新节点
void Insert(Link l)
{
Link p,v,newinfo; //p指向插入位置,newinfo指新插入记录
char ch,num[10],s[10]; //s[]保存插入点位置之前的学号,num[]保存输入的新纪录的学号
int flag=0;
v=l->next;
system("cls");
Disp(l);
while(l)
{
stringinput(s,10,"please input insert location after the Number:");
flag=0;
v=l->next;
while(v) //查询该学号是否存在,flag=1表示该学号存在
{
if(strcmp(v->data.num,s)==0)
{
flag=1;
break;
}
v=v->next;
}
if(flag==1)
break; //若学号存在,则进行插入之前的新纪录的输入操作
else
{
getchar();
printf("
===== > The number %s is not existing,try again? (y/n):",s);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
continue;
else
return;
}
}
//以下新记录的输入操作与Add()相同
stringinput(num,10,"input new student Number:");
v=l->next;
while(v)
{
if(strcmp(v->data.num,num)==0)
{
printf("==== > Sorry,the new number: '%s' is existing !
",num);
printheader();
printdata(v);
printf("
"); //提示新输入学号已经存在,保证学号的唯一性
getchar();
getchar();
return;
}
v=v->next;
}
newinfo=(Node*)malloc(sizeof(Node));
if(!newinfo)
{
printf("
allocate memory failure"); //如果没有申请到,打印提示信息
return; //返回主界面
}
strcpy(newinfo->data.num,num);
stringinput(newinfo->data.name,15,"input new name:");
newinfo->data.cgrade=numberinput("C language Score[0~100]:");
newinfo->data.mgrade=numberinput("Math Score [0~100]:");
newinfo->data.egrade=numberinput("English Score [0~100]:");
newinfo->data.total=newinfo->data.egrade+newinfo->data.cgrade+newinfo->data.mgrade;
newinfo->data.ave=(float)(newinfo->data.total/3);
newinfo->data.mingci=0;
newinfo->next=NULL;
saveflag=1; //在main()中有对该全局变量的判断,若为1,则进行存盘操作
p=l->next; //p指向实际保存着学生的记录的第一个节点
while(l)
{
if(strcmp(p->data.num,s)==0) //在链表中插入一个节点
{
newinfo->next=p->next;
p->next=newinfo;
break;
}
p=p->next;
}
Disp(l);
printf("
");
getchar();
}
//统计该班总分第一名和单科第一和各科0~59,60~69,70~79,80~89,90~100各个分数段的人数
void Count(Link l)
{
Node *pm,*pe,*pc,*pt; //用于指向分数最高的节点
Node *r=l->next;
int countc[5]={0},countm[5]={0},counte[5]={0}; //保存3门成绩各个分数段的人数
if(!r)
{
system("cls");
printf("
==== > No student record!
");
getchar();
return;
}
system("cls");
Disp(l);
pm=pe=pc=pt=r;
while(r)
{
if(r->data.cgrade<60) countc[0]++;
if(r->data.mgrade<60) countm[0]++;
if(r->data.egrade<60) counte[0]++;
if(r->data.cgrade>=60&&r->data.cgrade<70) countc[1]++;
if(r->data.mgrade>=60&&r->data.mgrade<70) countm[1]++;
if(r->data.egrade>=60&&r->data.egrade<70) counte[1]++;
if(r->data.cgrade>=70&&r->data.cgrade<80) countc[2]++;
if(r->data.mgrade>=70&&r->data.mgrade<80) countm[2]++;
if(r->data.egrade>=70&&r->data.egrade<80) counte[2]++;
if(r->data.cgrade>=80&&r->data.cgrade<90) countc[3]++;
if(r->data.mgrade>=80&&r->data.mgrade<90) countm[3]++;
if(r->data.egrade>=80&&r->data.egrade<90) counte[3]++;
if(r->data.cgrade>=90) countc[4]++;
if(r->data.mgrade>=90) countm[4]++;
if(r->data.egrade>=90) counte[4]++;
if(r->data.cgrade>=pc->data.cgrade) pc=r;
if(r->data.mgrade>=pm->data.mgrade) pm=r;
if(r->data.egrade>=pe->data.egrade) pe=r;
if(r->data.total>=pt->data.total) pt=r;
r=r->next;
}
printf("
- - - - - - - - - - - - - - - -the TongJi result- - - - - - - - - - - - - - - -
");
printf("C Language 0~59:%d (ren)
",countc[0]);
printf("C Language 60~69:%d (ren)
",countc[1]);
printf("C Language 70~79:%d (ren)
",countc[2]);
printf("C Language 80~89:%d (ren)
",countc[3]);
printf("C Language 90~100:%d (ren)
",countc[4]);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
");
printf("Math 0~59:%d (ren)
",countm[0]);
printf("Math 60~69:%d (ren)
",countm[1]);
printf("Math 60~79:%d (ren)
",countm[2]);
printf("Math 80~89:%d (ren)
",countm[3]);
printf("Math 90~100:%d (ren)
",countm[4]);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
");
printf("English 0~59:%d (ren)
",counte[0]);
printf("English 60~69:%d (ren)
",counte[1]);
printf("English 70~79:%d (ren)
",counte[2]);
printf("English 80~89:%d (ren)
",counte[3]);
printf("English 90~100:%d (ren)
",counte[4]);
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
");
printf("The highest student by total score num:%s total score:%d
",pt->data.num,pt->data.total);
printf("The highest student by English score num:%s total score:%d
",pe->data.num,pe->data.egrade);
printf("The highest student by Math score num:%s total score:%d
",pm->data.num,pm->data.mgrade);
printf("The highest student by C score num:%s total score:%d
",pc->data.num,pc->data.cgrade);
printf("
press any key to return");
getchar();
}
//利用选择排序法实现单链表的按总分字段从高到低的降序排序
void Sort(Link l)
{
struct student temp;
Node *p1,*p2,*p; //p1,p2指向要排序节点,p指向两个节点比较中总分较高的节点
int i=0;
if(l->next==NULL)
{
system("cls");
printf("
===== > No student record!
");
getchar();
return;
}
system("cls");
Disp(l); //显示排序前的所有学生记录
p1=l->next;
while(p1) //若p1所指节点为NULL外循环结束
{
p2=p1->next;
p=p1;
while(p2) //若p2所指节点为NULL,本次循环比较结束
{
if(p2->data.total>=p->data.total)
p=p2; //若两个节点比较中,p指向总分较高的节点
p2=p2->next;
}
temp=p->data; //一轮比较后,将总分最高的节点的数据域与本轮最前面的节点的数据域互换
p1->data=temp;
p1=p1->next; //p1向链表后移一个节点
}
p=l->next; //已排好序的头指针赋给p,准备填写名次
while(p!=NULL) //当p不为空时,进行下列操作
{
i++; //节点序号
p->data.mingci=i; //将名次赋值
p=p->next; //指针后移
}
Disp(l);
saveflag=1;
printf("
= = = = > sort complete!
");
}
//数据存盘,若用户没有专门进行此操作且对数据有修改,在退出系统时,会提示用户存盘
void Save(Link l)
{
FILE *fp;
Node *p;
int count=0;
fp=fopen("c:\student","wb"); //以只写方式打开二进制文件
if(fp==NULL) //打开文件失败
{
printf("
===== > open file error!");
getchar();
return;
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1) //每次写一条记录或一个节点信息至文件
{
p=p->next;
count++;
}
else
break;
}
if(count>0)
{
getchar();
printf("
===== > save file complete,total saved's record number is: %d
",count);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("the current link is empty,no student record is saved!
");
getchar();
}
fclose(fp); //关闭此文件
}
void main()
{
Link l; //定义链表
FILE *fp; //文件指针
int select; //保存选择结果变量
char ch; //保存(y,Y,n,N)
int count=0; //保存文件中的记录条数或节点个数
Node *p,*r; //定义记录指针变量
l=(Node*)malloc(sizeof(Node));
if(!l)
{
printf("
allocate memory failure"); //如果没有申请到,打印提示信息
return; //返回主界面
}
l->next=NULL;
r=l;
fp=fopen("C:\student","ab+"); //以追加方式打开一个二进制文件,可读可写,若此文件不存在,会创建此文件
if(fp==NULL)
{
printf("
==== > can not open file!
");
exit(0);
}
while(!feof(fp))
{
p=(Node*)malloc(sizeof(Node));
if(!p)
{
printf("memory malloc failure!
"); //没有申请成功
exit(0); //退出
}
if(fread(p,sizeof(Node),1,fp)==1) //一次从文件中读取一条学生成绩记录
{
p->next=NULL;
r->next=p;
r=p; //r指针向后移一个位置
count++;
}
}
fclose(fp); //关闭文件
printf("
===== > open file success,the total records number is %d.
",count);
printf("
===== > Press any key to enter the Menu!");
getchar();
while(l)
{
system("cls");
menu();
p=r;
printf("
Please enter your choice(0~9):"); //显示提示信息
scanf("%d",&select);
if(select==0)
{
if(saveflag==1) //若对链表的数据有修改且未进行存盘操作,则此标记为1
{
getchar();
printf("
===== > Whether save the modified record to file? (y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(l);
}
printf("==== > thank you for useness!");
getchar();
break;
}
switch(select)
{
case 1:Add(l);break; //增加学生记录
case 2:Del(l);break; //删除学生记录
case 3:Qur(l);break; //查询学生记录
case 4:Modify(l);break; //修改学生记录
case 5:Insert(l);break; //插入学生记录
case 6:Count(l);break; //统计学生记录
case 7:Sort(l);break; //排序学生记录
case 8:Save(l);break; //保存学生记录
case 9:system("cls");Disp(l);break;//显示学生记录
default:Wrong();getchar();break;//按键有误,必须为数值0~9
}
}
}