【正文】
nt1[i]。 student1[i]=student1[j]。 student1[j]=student2。 } i++。 } rewind(fp)。 for(i=0。ik。i++) fwrite(amp。student1[i],sizeof(struct person),1,fp)。 } fclose(fp)。 } 由于記錄顯示操作經(jīng)常進(jìn)行,所以我將這分由獨(dú)立的函數(shù)來實(shí)現(xiàn),減少代碼的重復(fù)。 Listall() { FILE *fp。 char ch。 struct person student1[30]。 int i。 int k。 fp=fopen(,r)。 if(fp==NULL) { printf(can39。t open this file.\n)。 算法與數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì) 學(xué)生成績管理系統(tǒng) 11 printf(wait for a second with patience.\nthe system is exiting.\n)。 getch()。 exit(0)。 } else { do{ rewind(fp)。 printf(name\t\t\tnum\tchinese\t\tEnglish\t\tmath\tsum\taverage\n)。 while(fread(amp。student,sizeof(struct person),1,fp)) { printf(%10s%6ld%%%%%\n,studen,)。 } printf(GO ON(Y/N)?\n)。 ch=getchar()。 getchar()。 }while(ch==39。y39。||ch==39。Y39。)。 } fclose(fp)。 return_confirm()。 } 四、具體程序的實(shí)現(xiàn) include include include include struct person { char name[20]。 long num。 float math。 float English。 float chinese。 float average。 float sum。 }student。 int N=0。 Enter() { FILE *fp。 char ch。 char ch2。 char numstr[20]。 struct person student2。 clrscr()。 fp=fopen(,ab)。 if(fp==NULL) { printf(can39。t open this file.\n)。 printf(wait for a second with patience.\nthe system is exiting.\n)。 getch()。 exit(0)。 算法與數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì) 學(xué)生成績管理系統(tǒng) 12 } else { do{ printf(your name:)。 gets()。 printf(\nyour num:)。 gets(numstr)。 =atol(numstr)。 printf(\nyour math:)。 gets(numstr)。 =atof(numstr)。 printf(\nyour English:)。 gets(numstr)。 =atof(numstr)。 printf(\nyour chinese:)。 gets(numstr)。 =atof(numstr)。 =++。 =。 fwrite(amp。student,sizeof(struct person),1,fp)。 N++。 printf(Do you continue adding other students(Y/N)?\n)。 ch=getchar()。 getchar()。 }while(ch==39。y39。||ch==39。Y39。)。 } printf(Show all(Y/N)?\n)。 ch2=getchar()。 getchar()。 if(ch2==39。y39。||ch2==39。Y39。) Listall()。 fclose(fp)。 return_confirm()。 clrscr()。 } Search() { char choice。 char ch1。 char ch2。 char ch3。 char ch4。 char name[20]。 long num。 char numstr[20]。 clrscr()。 算法與數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì) 學(xué)生成績管理系統(tǒng) 13 printf(***WAYS OF SEARCHING***\n)。 printf(\n)。 printf(** **\n)。 printf(** **\n)。 printf(\n)。 printf(give your choice:\n)。 choice=getchar()。 getchar()。 while(choice39。239。||choice39。139。) { printf( choice is wrong.\nplease give a correct one:\n)。 choice=getchar()。 getchar()。 } switch(choice) { case 39。139。:do{ Loop1: printf(input the name of students you want to search:\n)。 gets(name)。 Search1(name)。 printf(GO ON SEARCHING BY NAEM(Y/N)?\n)。 ch2=getchar()。 getchar()。 }while(ch2==39。y39。||ch2==39。Y39。)。 printf(Search by other ways(Y/N)?\n)。 ch3=getchar()。 getchar()。 if(ch3==39。y39。||ch3==39。Y39。) goto Loop2。 break。 case 39。239。: do{ Loop2: printf(input the num of students you want to search:\n)。 gets(numstr)。 num=atol(numstr)。 Search2(num)。 printf(GO ON SEARCHING BY NUM(Y/N)?\n)。 ch1=getchar()。 getchar()。 }while(ch1==39。y39。||ch1==39。Y39。)。 } } Search2(long num) { 算法與數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì) 學(xué)生成績管理系統(tǒng) 14 FILE *fp。 char ch。 int flag=0。 int t。 clrscr()。 fp=fopen(,rb)。 if(fp==NULL) { printf(can39。t open this file.\n)。 printf(wait for a second with patience.\nthe system is exiting.\n)。 getch()。 exit(0)。 } else { flag=0。 t=0。 while(fread(amp。student,sizeof(struct person),1,fp)) if(num==) { printf(name\t\tnum\tchinese\tEnglish\tmath\sum\taverage\n)。 printf(%10s%6ld%%%%%\n,stude,)。 t=1。 break。 } if(t==0) printf( student you are searching not existed.\n)。 rewind(fp)。 } fclose(fp)。 } Search1(char *name) { FILE *fp。 char ch。 int flag=0。 int t。 clrscr()。 fp=fopen(,rb)。 if(fp==NULL) { printf(can39。t open this file.\n)。 printf(wait for a second with patience.\nthe system is exiting.\n)。 getch()。 算法與數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì) 學(xué)生成績管理系統(tǒng) 15 exit(0)。 } else { flag=0。 t=0。 while(fread(amp。student,sizeof(struct person),1,fp)) if((strcmp(name,))==0) { printf(name\t\tnum\tchinese\tEnglish\tmath\sum\taverage\n)。 printf(%10s%6ld%%%%%\n,stude