回复 10楼 爱是相反梦
可以~link->next照样可以引用~里面的struct node已经被link代替了~
[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 100 #define LINE ------------------------------------------- #define PUT "|学号 | 姓名 | 性别 | 班级 | 语文 | 数学 | 英语 | 生物 | 物理 | 化学 | 总分 | 平均分 |\n" #define PUT1 "|%11d|%6s|%6c|%6d|%6.1f|%6.1f|%6.1f|%6.1f|%6.1f|%6.1f|%6.1f|%8.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("不存在!\n"); return; } printf(PUT); while(p!=NULL) { printf(PUT1,PUT2); p=p->next; } } LINK *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 L;} 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; } } return L; } LINK *Del(LINK *L)/*删除信息*/ { int ch;/*保存要删除的学号*/ LINK *p,*q,*r; q=L; printf("请输入删除的学号:\n"); scanf("%d",&ch); for(p=L->next;p!=NULL;p=p->next) { r=p->next; if(p->grade.num==ch) { q->next=r; free(p); p=NULL; return L; } else q=p; } if(p==NULL) printf("此记录不存在!\n"); return L; } void Query(LINK *L)/*查询信息*/ { LINK *p,*exam; exam=NULL; 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==NULL) { printf("记录为空!\n"); return; } if(p->grade.num==exam->grade.num) { printf(PUT); printf(PUT1,PUT2); return; } } if(p==NULL) printf("此学号不存在!\n"); return; } if(ch==2) { printf("请输入姓名:"); scanf("%s",exam->grade.name); for(p=L->next;p!=NULL;p=p->next) { if(p==NULL) { printf("记录为空!\n"); return; } if(!strcmp(p->grade.name,exam->grade.name)) { printf(PUT); printf(PUT1,PUT2); return;; } } if(p==NULL) printf("此姓名不存在!\n"); return; } } LINK *Modify(LINK *L)/*修改信息*/ { LINK *p; printf("请输入要修改的学号:"); scanf("%d",&exam->grade.num); for(p=L->next;p!=NULL;p=p->next) { if(p==NULL) { printf("记录为空!\n"); return L; } 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; return L; } } if(p==NULL) { printf("这个记录不存在!\n"); } return 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); } LINK *Insert(LINK *L)/*插入信息*/ { LINK *p,*r; p=(LINK*)malloc(sizeof(LINK));/*保存临时的信息*/ p=NULL; 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"); free(p); return L; } else if((r->grade.num < p->grade.num)&&(p->grade.num < r->next->grade.num)) { p->next=r->next; r->next=p; break; } } 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; free(p); return 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; } } } } 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; }
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 100 #define LINE ------------------------------------------- #define PUT "|学号 | 姓名 | 性别 | 班级 | 语文 | 数学 | 英语 | 生物 | 物理 | 化学 | 总分 | 平均分 |\n" #define PUT1 "|%11d|%6s|%6c|%6d|%6.1f|%6.1f|%6.1f|%6.1f|%6.1f|%6.1f|%6.1f|%8.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("不存在!\n"); return; } printf(PUT); while(p!=NULL) { printf(PUT1,PUT2); p=p->next; } } LINK *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 L;} 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; } } return L; } LINK *Del(LINK *L)/*删除信息*/ { int ch;/*保存要删除的学号*/ LINK *p,*q,*r; q=L; printf("请输入删除的学号:\n"); scanf("%d",&ch); for(p=L->next;p!=NULL;p=p->next) { r=p->next; if(p->grade.num==ch) { q->next=r; free(p); p=NULL; return L; } else q=p; } if(p==NULL) printf("此记录不存在!\n"); return L; } void Query(LINK *L)/*查询信息*/ { LINK *p,q; exam=&q; 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==NULL) { printf("记录为空!\n"); return; } if(p->grade.num==exam->grade.num) { printf(PUT); printf(PUT1,PUT2); return; } } if(p==NULL) printf("此学号不存在!\n"); return; } if(ch==2) { printf("请输入姓名:"); scanf("%s",exam->grade.name); for(p=L->next;p!=NULL;p=p->next) { if(p==NULL) { printf("记录为空!\n"); return; } if(!strcmp(p->grade.name,exam->grade.name)) { printf(PUT); printf(PUT1,PUT2); return;; } } if(p==NULL) printf("此姓名不存在!\n"); return; } } LINK *Modify(LINK *L)/*修改信息*/ { LINK *p,q; exam=&q; printf("请输入要修改的学号:"); scanf("%d",&exam->grade.num); for(p=L->next;p!=NULL;p=p->next) { if(p==NULL) { printf("记录为空!\n"); return L; } if(p->grade.num==exam->grade.num) { printf("请输入修改后的成绩:\n"); 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; return L; } } if(p==NULL) { printf("这个记录不存在!\n"); } return L; } void Tongji(LINK *L)/*统计信息*/ { LINK *p,*r; r=L->next; p=r; if(L->next==NULL) { printf("无记录!\n"); return; } while(r!=NULL && r->next!=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); } LINK *Insert(LINK *L)/*插入信息*/ { LINK *p,*r; p=(LINK*)malloc(sizeof(LINK));/*保存临时的信息*/ printf("请输入插入的学号:"); scanf("%d",&p->grade.num); for(r=L->next;r!=NULL;r=r->next) if(r->grade.num==p->grade.num) { printf("这个学号已经存在!\n"); free(p); return L; } 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; r=L->next; if(r==NULL)/*链表为空*/ { L->next=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)&&(p->grade.num < r->next->grade.num)) { p->next=r->next; r->next=p; return L; } } if(r->next==NULL) { r->next=p;p->next=NULL; } } } return L; } LINK *Sort(LINK *L)/*按学号排序*/ { LINK *p,*q,*r; q=(LINK*)malloc(sizeof(LINK)); r=(LINK*)malloc(sizeof(LINK)); if(L->next==NULL) printf("无记录!\n"); else { p=L->next; r=p; q->next=r; 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 > r->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; } } } L=q; return 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; }