/* 顺序显示链表各表元 */ void displaylist(struct node *h) { while(h!=NULL) { displaystu((struct record *)h); printf("\n Press ENTER to continue...\n"); while(getchar()!='\n'); h=h->next; } return; } /* 按学生姓名查找学生记录 */ int retrievebyn(char *fname, char *key) { FILE *fp; int c; struct record s; if((fp=fopen(fname,"r"))==NULL) { printf("Can't open file %s.\n",fname); return 0; } c=0; while(readrecord(fp,&s)!=0) { if(strcmp(s.name,key)==0) { displaystu(&s); c++; } } fclose(fp); if(c==0) printf("The student %s is not in the file %s.\n",key,fname); return 1; }
/* 按学生学号查找学生记录 */ int retrievebyc(char *fname, char *key) { FILE *fp; int c; struct record s; if((fp=fopen(fname,"r"))==NULL) { printf("Can't open file %s.\n",fname); return 0; } c=0; while(readrecord(fp,&s)!=0) { if(strcmp(s.code,key)==0) { displaystu(&s); c++; break; } } fclose(fp); if(c==0) printf("The student %s is not in the file %s.\n",key,fname); return 1; }
main() { int i,j,n; char c; char buf[BUFLEN]; FILE *fp; struct record s; clrscr(); printf("Please input the students marks record file's name: "); scanf("%s",stuf); if((fp=fopen(stuf,"r"))==NULL) { printf("The file %s doesn't exit, do you want to creat it? (Y/N) ",stuf); getchar(); c=getchar(); if(c=='Y'||c=='y') { fp=fopen(stuf,"w"); printf("Please input the record number you want to write to the file: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Input the student's name: "); scanf("%s",&s.name); printf("Input the student's code: "); scanf("%s",&s.code); for(j=0;j<SWN;j++) { printf("Input the %s mark: ",schoolwork[j]); scanf("%d",&s.marks[j]); } writerecord(fp,&s); } fclose(fp); } } fclose(fp); getchar(); /*clrscr();*/ puts("Now you can input a command to manage the records."); puts("m : mean of the marks."); puts("t : total of the marks."); puts("n : search record by student's name."); puts("c : search record by student's code."); puts("l : list all the records."); puts("s : sort and list the records by the total."); puts("q : quit!"); while(1) { puts("Please input command:"); scanf(" %c",&c); /* 输入选择命令 */ if(c=='q'||c=='Q') { puts("\n Thank you for your using."); break; /* q,结束程序运行 */ } switch(c) { case 'm': /* 计算平均分 */ case 'M': if((n=totalmark(stuf))==0) { puts("Error!"); break; } printf("\n"); for(i=0;i<SWN;i++) printf("%-15s's average is: %.2f.\n",schoolwork[i],(float)total[i]/n); break; case 't': /* 计算总分 */ case 'T': if((n=totalmark(stuf))==0) { puts("Error!"); break; } printf("\n"); for(i=0;i<SWN;i++) printf("%-15s's total mark is: %d.\n",schoolwork[i],total[i]); break; case 'n': /* 按学生的姓名寻找记录 */ case 'N': printf("Please input the student's name you want to search: "); scanf("%s",buf); retrievebyn(stuf,buf); break; case 'c': /* 按学生的学号寻找记录 */ case 'C': printf("Please input the student's code you want to search: "); scanf("%s",buf); retrievebyc(stuf,buf); break; case 'l': /* 列出所有学生记录 */ case 'L': liststu(stuf); break; case 's': /* 按总分从高到低排列显示 */ case 'S': if((head=makelist(stuf))!=NULL) displaylist(head); break; default: break; } } }