1、 char num5; char name20; int score; struct student *next;struct student *createlist() /创建链表的头指针 struct student *head; head = (struct student *) malloc(sizeof(struct student); head-next = NULL; return head;int SCANF(struct student * head,struct student * newnode) /*输入学生信息子函数 并在此做出异常输入情况的判断*/ int LENT
2、H; /LENTH为输入的学生学号的长度 struct student * p; p=head-next; printf (请输入学生学号:n scanf (%s,newnode-num); while(p) /第一种情况,输入的学号在系统中已经存在 if (strcmp(p-num,newnode-num)=0) printf(您输入的学号在该系统中已经存在,请按任意键重新输入!n getch(); return 1; else p=p- LENTH=strlen(newnode- /以下第二种情况,输入的学号位数不是4位 if(LENTH!=4) printf(您输入的学生学号必须为4位请
3、按任意键重新输入!nnn getch(); return 1; /返回值为真,方便下面的调用请输入学生姓名:name);请输入学生成绩:%d,&newnode-score); return 0; /返回值为假void Insert(struct student * head, struct student * newnode) /*将学生信息按照学号由高到低排序并插入到链表中*/ struct student * pre = head; /pre前一个节点 node后一个 把newnode插入 struct student * node = pre- while (node != NULL)
4、if (strcmp(node-num, newnode-num) break; pre-next = newnode; /找到了结点的位置 将结点插入到链表中 newnode-next = node;int ADD(struct student *head) /* 添加学生信息*/ struct student* p; p=(struct student *) malloc(sizeof(struct student); while(SCANF(head,p) /异常学号输入情况的处理 system(cls struct student *p1 = head; struct student
5、*p2 = p1- Insert(head,p);如果需要继续输入请按y,返回主菜单请按其它任意键n char i; i = getch(); if ( i=y ) system ( ADD(head); void DELETE(struct student *head) /* 删除学生信息*/ struct student *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/ char s256; struct student *p;学生信息如下:nn for(p=head-p;p=p-next) /*辅助输出学生信息*/ |学号:%8s|姓名:%8s| 成绩:%6d|n
6、, p-num,p-name,p- 请输入要删除的学号 Please enter the deleted num: n如果要删除全部学生信息,请按0000n scanf(,s); p1=head- p2=head; if(strcmp(s,0000)!=0) while( p1 ! if(strcmp(p1-num,s) p2=p1; /*p2为其前驱指针*/ p1=p1- if(p1!=NULL) p2-next=p1-您输入的学生删除成功n请按任意键返回主菜单n getch(); free(void *)p1); if(p1=NULL)n没有该学生 There is no num %s s
7、tudent on the list.n请按任意键返回主菜单重新选择n)=0) head-next=NULL; printf(全部学生信息删除成功n printf( getch();void MODIFY(struct student *head) /* 修改学生信息*/ char num256;请输入要修改的学生的学号:,num); struct student * p2 = p1- while (p2) if (strcmp(p2-num, num) != 0) p1 = p2; p2 = p2- if (!p2) else, p2-num,p2-name,p2- printf (请重新输
8、入该学生信息:,p2-p2-void QUERY(struct student *head) /* 查询学生信息,三种方式查询*/ char s5;请选择查询方式:1,按学号查找;2,按姓名查找;3.按成绩查找n i=getch(); if(i=1)请输入学生学号来查找. Please enter num for searching.n您要查询的学生信息如此下: for(p=head;next) if(strcmp(p-num,s)=0 )| %4s | %4s | %3d |n查询完毕,请按任意键返回n if(p=NULL) int j=0; if(i=2请输入姓名来查找. Please e
9、nter name for searching.nname,s)=0 ) j+; if(p=NULL)&(j=0)n没有该学生 There is no name %s student on the list.n if(p=NULL) int y=0;3 int SCORE;请输入成绩来查找. Please enter score for searching.nSCORE); if(p-score=SCORE) y+; if(p=NULL)&(y=0)n没有对应该成绩的学生nvoid PRINT(struct student *head) /* 打印全部学生信息*/ float average=
10、0.00000000; int ITOTAL=1; 学生信息如下 nn*STUDENT LIST*n-n while(p) | %d | 学号:%8s | 姓名:%8s | 成绩:%6d |n,ITOTAL+, p- average=average+p-score; p=p-*END*nn if(ITOTAL!=1)学生总人数为:%dn,ITOTAL-1); average=average/(ITOTAL-1);学生平均成绩为:%fn,average); if(ITOTAL=1)学生人数为0,不存在平均成绩n请按数字键1系统将按成绩由高到低打印学生信息n char I; I=getch();
11、if(I= int n=ITOTAL-1; / n为学生总人数 struct student stu256,stu1; int i,j; p1=head; p2=p1- int X=0; for(i=0;p2!=NULL;i+) strcpy(stui.num,p2- strcpy(stui.name,p2- stui.score=p2- X+; p1=p2; p2=p2- for(j=0;jX-1;j+)istui+1.score) stu1=stui; stui=stui+1; stui+1=stu1; for(i=n-1;i=0;i-) printf(学号 姓名 成绩n%5s%5s%5d
12、n,stui.num,stui.name,stui.score); FILE *MyOpenFile(char *fname,char s5) /* 打开文件子函数并返回文件头指针*/ FILE *fp; fp=fopen(fname,s); if(fp=NULL)cannot open file return NULL; return fp;void SAVE(struct student *head) /* 保存学生信息*/ FILE *fp;请选择存储方式:1,文本文件格式;2,二进制文件格式n i=getch(); 请输入你想存储的路径及文件名n char fname256; scanf(,fname); fp=MyOpenFile(fname,w struct student *p; p =head- while(p!=NULL) fprintf(fp,%s %s %dn /*把学生信息写入文本*/ p=p- fclose(fp);文本格式文件保存成功nn请按任意键返回主菜单n fp=MyOpenFile(fname,wb
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2