自己写的学生成绩管理,但是运行添加成员时会停止,求大神指点错误
小白依旧是小白,暑假的时候自学c后一点一点编写的,但是选择添加后,输入完全部成绩,按回车就会停止运行,自己检查了好久看不出来,求大神可以指点一下错误在哪里。拜托了!!!!!程序代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 100 #define LINE ------------------------------------------- #define PUT "|学号|姓名|性别|班级|语文|数学|英语|生物|物理|化学|总分|平均分|\n" #define PUT1 "|%d |%s |%c |%d|%.1f |%.1f |%.1f |%.1f |%.1f |%.1f |%.1f |%.1f |\n" #define PUT2 p->grade.num, p->grade.name, p->grade.sex, p->grade.class1, p->grade.ywg, p->grade.sxg, p->grade.yyg, p->grade.swg, p->grade.wlg, p->grade.hxg, p->grade.total, p->grade.ave FILE *fp; int i; int saveflag=0; struct student { int num; char name[30]; char sex; int class1; float ywg; float sxg; float yyg; float swg; float wlg; float hxg; float total; float ave; }; typedef struct node { struct student grade; struct node *next; }LINK; LINK *exam; void Save(LINK *L) { LINK *p; p=L->next; fp=fopen("grades.txt","w+"); if(fp==NULL) { printf("打开失败!\n"); return; } while(p!=NULL) { fwrite(p,sizeof(LINK),1,fp); p=p->next; } fclose(fp); } void menu() { printf("---------------学生成绩管理系统---------------\n"); printf("\t0.退出 1.显示记录\n\t2.添加记录 3.删除记录\n\t4.查询记录 5.修改记录\n\t6.统计成绩 7.插入记录\n\t8.排序并输出 9.保存\n"); printf("----------------------------------------------\n"); } void Read(LINK *L) { LINK *p; p=L->next; if(p=NULL) { printf("不存在!"); return; } printf(PUT); while(p!=NULL) { printf(PUT1,PUT2); } } void Add(LINK *L) { LINK *r,*p,*q; char ch; r=(LINK*)malloc(sizeof(LINK)); if(r==NULL) { printf("分配失败!\n"); exit(1); } printf("是否添加记录(y/s)?\n"); scanf("%s",&ch); if(ch=='y') { printf("请输入数据:\n"); printf("学号:");scanf("%d",&r->grade.num); for(p=L->next;p!=NULL;p=p->next) if(p->grade.num==r->grade.num) {printf("此学生已经存在!");return;} printf("姓名:");scanf("%s",&r->grade.name); printf("性别:");scanf("%s",&r->grade.sex); printf("班级:");scanf("%d",&r->grade.class1); printf("语文成绩:"); scanf("%f",&r->grade.ywg); printf("数学成绩:"); scanf("%f",&r->grade.sxg); printf("英语成绩:"); scanf("%f",&r->grade.yyg); printf("生物成绩:"); scanf("%f",&r->grade.swg); printf("物理成绩:"); scanf("%f",&r->grade.wlg); printf("化学成绩:"); scanf("%f",&r->grade.hxg); r->grade.total=r->grade.ywg+r->grade.sxg+r->grade.yyg+r->grade.swg+r->grade.wlg+r->grade.hxg+r->grade.swg; r->grade.ave=r->grade.total/6; if(L->next==NULL) { L->next=r; q=r; q->next=NULL; } else { q=L->next; while(q->next!=NULL) { q=q->next; } q->next=r; q=r; q->next=NULL; } } Save(L); } void Del(LINK *L) { int ch; LINK *p,*q,*r; printf("请输入删除的学号:\n"); scanf("%d",&ch); for(p=L;p!=NULL;p=p->next) { q=p; p=p->next; r=p->next; if(p->grade.num==ch) { q->next=r; free(p); break; } else p=q; } if(p==NULL) printf("此记录不存在!\n"); Save(L); } void Query(LINK *L) { LINK *p; int ch; printf("请问是按学号(按1)还是姓名(按2)查询?\n"); scanf("%d",&ch); if(ch==1) { printf("请输入学号:\n"); scanf("%d",&exam->grade.num); for(p=L->next;p!=NULL;p=p->next) if(p->grade.num==exam->grade.num) { printf(PUT); printf(PUT1,PUT2); break; } if(p==NULL) printf("此学号不存在!\n"); } if(ch==2) { printf("请输入姓名:"); scanf("%s",&exam->grade.name); for(p=L->next;p!=NULL;p=p->next) if(!strcmp(p->grade.name,exam->grade.name)) { printf(PUT); printf(PUT1,PUT2); break; } if(p==NULL) printf("此姓名不存在!\n"); } } void Modify(LINK *L) { LINK *p; printf("请输入要修改的学号:"); scanf("%d",&exam->grade.num); for(p=L->next;p!=NULL;p=p->next) { if(p->grade.num==exam->grade.num) { printf("请输入修改后的成绩:\n"); printf("语文成绩:");scanf("%f",&p->grade.ywg); printf("班级:");scanf("%d",&p->grade.class1); printf("语文成绩:");scanf("%f",&p->grade.ywg); printf("数学成绩:");scanf("%f",&p->grade.sxg); printf("英语成绩:");scanf("%f",&p->grade.yyg); printf("生物成绩:");scanf("%f",&p->grade.swg); printf("物理成绩:");scanf("%f",&p->grade.wlg); printf("化学成绩:");scanf("%f",&p->grade.hxg); p->grade.total=p->grade.ywg+p->grade.sxg+p->grade.yyg+p->grade.swg+p->grade.wlg+p->grade.hxg+p->grade.swg; p->grade.ave=p->grade.total/6; break; } } Save(L); } void Tongji(LINK *L) { LINK *p,*r; r=L->next; while(r!=NULL) { if(r->grade.total > r->next->grade.total) { p=r; } else { p=r->next; } r=r->next; } printf("总分最高是:\n"); printf(PUT); printf(PUT1,PUT2); } void Insert(LINK *L) { LINK *p,*r; p=(LINK*)malloc(sizeof(LINK)); printf("请输入插入的学号:"); scanf("%d",&p->grade.num); r=L->next; if(r==NULL) { r=p; p->next=NULL; } else { if(p->grade.num < r->grade.num) { p->next=r; L->next=p; } else { for(;r->next!=NULL;r=r->next) { if(r->grade.num==p->grade.num) { printf("这个学号已经存在!\n"); return; } else if((r->grade.num < p->grade.num)&&(p->grade.num < r->next->grade.num)) { p->next=r->next; r->next=p; } } if(r->next==NULL) { r->next=p;p->next=NULL; } } } printf("姓名:"); scanf("%s",p->grade.name); printf("性别:"); scanf("%s",&p->grade.sex); printf("班级:"); scanf("%d",&p->grade.class1); printf("语文成绩:"); scanf("%f",&p->grade.ywg); printf("数学成绩:"); scanf("%f",&p->grade.sxg); printf("英语成绩:"); scanf("%f",&p->grade.yyg); printf("生物成绩:"); scanf("%f",&p->grade.swg); printf("物理成绩:"); scanf("%f",&p->grade.wlg); printf("化学成绩:"); scanf("%f",&p->grade.hxg); p->grade.total=p->grade.ywg+p->grade.sxg+p->grade.yyg+p->grade.swg+p->grade.wlg+p->grade.hxg+p->grade.swg; p->grade.ave=p->grade.total/6; Save(L); } void Sort(LINK *L) { LINK *p,*q,*r; if(L->next==NULL) printf("无记录!\n"); else { p=L->next; r=p; q->next=r; r->next=NULL; for(p=p->next;p!=NULL;p=p->next) { if(p->grade.num < q->next->grade.num) { p->next=q->next; q->next=p; } r=q->next; while((p->grade.num > q->next->grade.num)&&(r->next!=NULL)) { if(p->grade.num < r->next->grade.num) { p->next=r->next; r->next=p; break; } r=r->next; } if(r->next==NULL) { r->next=p; p->next=NULL; r=p; } } } Save(L); } int main() { LINK *L; LINK *p,*q; int select,m=1; L=(LINK*)malloc(sizeof(LINK)); if(L==NULL) { printf("分配失败!\n"); return 0; } L->next=NULL; q=L; fp=fopen("grades.txt","r"); if(fp==NULL) { fp=fopen("grades.txt","w+"); i=0; } else { while(!feof(fp)) { p=(LINK*)malloc(sizeof(LINK)); if(p==NULL) { printf("分配失败!\n"); return 0; } if(fread(p,sizeof(LINK),1,fp)==1) { p->next=NULL; q->next=p; q=p; i++; } } } fclose(fp); printf("\n打开成功,一共有%d条记录。\n",i); while(m) { menu(); printf("请选择选项:(0~9):\n"); scanf("%d",&select); switch(select) { case 0:if(saveflag==0) m=0; else if(saveflag==1) Save(L); m=0;break; case 1:Read(L);break; case 2:Add(L);break; case 3:Del(L);break; case 4:Query(L);break; case 5:Modify(L);break; case 6:Tongji(L);break; case 7:Insert(L);break; case 8:Sort(L);break; case 9:Save(L);saveflag=0;break; default:printf("请输入0~9的选项:\n");break; } } system("pause"); return 0; }