学生成绩管理系统V2.0
学生成绩管理系统是一个非常实用的程序,如果能够把用户输入的数据存盘,下次运行时读出,就更有用了。
某班有最多不超过40人(具体人数由键盘输入)参加期末考试,考试科目为数学(MT)、英语(EN)和物理(PH)。编程实现如下菜单驱动的学生成绩管理系统:
(1)录入每个学生的学号、姓名和各科考试成绩;
(2)计算每个学生的总分和平均分;
(3)按每个学生的总分由低到高排出名次表;
(4)按姓名的字典顺序排出成绩表;
(5)按学号查询学生排名及其考试成绩;
(6)按姓名查询学生排名及其考试成绩;
(7)将每个学生的记录信息写入文件;
(8)从文件中读出每个学生的记录信息并显示。
要求程序运行后先显示如下菜单,并提示用户输入选项:
1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by number
6.Search by name
7.Write to a file
8.Read from a file
0.Exit
Please enter your choice:
然后,根据用户输入的选项执行相应的操作。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include <string.h> 4 #define N 40 5 6 typedef struct student { 7 long studentID; //学号 8 char studentName[10]; //姓名 9 int score[3]; //3门课程的成绩 10 int sum; //总分 11 float aver; //平均分 12 } STUDENT; 13 14 void ReadScore(STUDENT stu[],int n,int m); //输入成绩 15 void ScoreSum(STUDENT stu[],int n,int m); //计算总分 16 void ScoreAverage(STUDENT stu[],int n,int m); //计算平均分 17 void PrintSumAver(STUDENT stu[],int n,int m); //输出每个学生的总分和平均分 18 void DataSort(STUDENT stu[],int n,int m); //按总分由低到高排名 19 void PrintDataSort(STUDENT stu[],int n,int m); //按成绩由高到低输出 20 void NameSort(STUDENT stu[],int n,int m); //按姓名的字典顺序排名 21 void PrintNameSort(STUDENT stu[],int n,int m); //按姓名的字典顺序输出 22 void Exchange(STUDENT stu[],int i,int j); //交换学生信息 23 void SearchID(STUDENT stu[],int n,int m); //按学号查询学生排名及其考试成绩 24 void SearchName(STUDENT stu[],int n,int m); //按姓名查询排名及考试成绩 25 void WritetoFile(STUDENT stu[],int n,int m); //将每个学生的记录信息写入文件 26 void ReadfromFile(STUDENT stu[],int *n,int *m); //从文件中读出每个学生的记录信息并显示 27 void PrintScore(STUDENT stu[],int n,int m); //从文件中读出每个学生的记录信息并显示 28 29 int main(void) { 30 int n,m=3; 31 STUDENT stu[N]; 32 do { 33 printf("1.Append record "); 34 printf("2.Caculate total and average score of every student "); 35 printf("3.Sort in ascending order by total score of every student "); 36 printf("4.Sort in dictionary order by name "); 37 printf("5.Search by number "); 38 printf("6.Search by name "); 39 printf("7.Write to a file "); 40 printf("8.Read from a file "); 41 printf("0.Exit "); 42 printf("Please enter your choice: "); 43 scanf("%d",&m); 44 switch (m) { 45 case 1: { 46 printf("Input n:"); 47 scanf("%d",&n); 48 ReadScore(stu,n,3); 49 break; 50 } 51 case 2: { 52 ScoreSum(stu,n,3); 53 ScoreAverage(stu,n,3); 54 PrintSumAver(stu,n,3); 55 break; 56 } 57 case 3: { 58 ScoreSum(stu,n,3); 59 ScoreAverage(stu,n,3); 60 DataSort(stu,n,3); 61 PrintDataSort(stu,n,3); 62 break; 63 } 64 case 4: { 65 ScoreSum(stu,n,3); 66 ScoreAverage(stu,n,3); 67 NameSort(stu,n,3); 68 PrintNameSort(stu,n,3); 69 break; 70 } 71 case 5: { 72 ScoreSum(stu,n,3); 73 ScoreAverage(stu,n,3); 74 SearchID(stu,n,3); 75 break; 76 } 77 case 6: { 78 ScoreSum(stu,n,3); 79 ScoreAverage(stu,n,3); 80 SearchName(stu,n,3); 81 break; 82 } 83 case 7: { 84 ScoreSum(stu,n,3); 85 ScoreAverage(stu,n,3); 86 WritetoFile(stu,n,3); 87 break; 88 } 89 case 8: { 90 ScoreSum(stu,n,3); 91 ScoreAverage(stu,n,3); 92 ReadfromFile(stu,&n,&m); 93 PrintScore(stu,n,3); 94 break; 95 } 96 case 0: { 97 printf("Exit"); 98 break; 99 } 100 default: 101 printf("Please enter it again."); 102 } 103 printf(" "); 104 } while(m!=0); 105 return 0; 106 } 107 108 void ReadScore(STUDENT stu[],int n,int m) { 109 int i,j; 110 for(i=0; i<n; i++) { 111 printf("Input record %d: ",i+1); 112 scanf("%ld",&stu[i].studentID); 113 scanf("%s",&stu[i].studentName); 114 for(j=0; j<m; j++) { 115 scanf("%d",&stu[i].score[j]); 116 } 117 } 118 } 119 120 void ScoreSum(STUDENT stu[],int n,int m) { 121 int i,j,sum; 122 for(i=0; i<n; i++) { 123 sum=0; 124 for(j=0; j<m; j++) { 125 sum+=stu[i].score[j]; 126 } 127 stu[i].sum=sum; 128 } 129 } 130 131 void ScoreAverage(STUDENT stu[],int n,int m) { 132 int i,j,sum; 133 for(i=0; i<n; i++) { 134 sum=0; 135 for(j=0; j<m; j++) { 136 sum+=stu[i].score[j]; 137 } 138 stu[i].aver=(float)sum/m; 139 } 140 } 141 142 void PrintSumAver(STUDENT stu[],int n,int m) { 143 int i,j; 144 for(i=0; i<n; i++) { 145 printf("%10ld%8s",stu[i].studentID,stu[i].studentName); 146 for(j=0; j<m; j++) { 147 printf("%4d",stu[i].score[j]); 148 } 149 printf("%6d ",stu[i].sum); 150 printf("%.2f ",stu[i].aver); 151 } 152 printf(" "); 153 } 154 155 void Exchange(STUDENT stu[],int i,int j) 156 { 157 long k; 158 int temp,x,y; 159 char t[N]; 160 float a; 161 temp=stu[j].sum; 162 stu[j].sum=stu[i].sum; 163 stu[i].sum=temp; 164 k=stu[j].studentID; 165 stu[j].studentID=stu[i].studentID; 166 stu[i].studentID=k; 167 strcpy(t,stu[i].studentName); 168 strcpy(stu[i].studentName,stu[j].studentName); 169 strcpy(stu[j].studentName,t); 170 a=stu[j].aver; 171 stu[j].aver=stu[i].aver; 172 stu[i].aver=a; 173 for(x=0;x<3;x++) 174 { 175 y=stu[j].score[x]; 176 stu[j].score[x]=stu[i].score[x]; 177 stu[i].score[x]=y; 178 } 179 } 180 181 void DataSort(STUDENT stu[],int n,int m) { 182 int i,j,temp,k; 183 char t[N]; 184 for(i=0; i<n-1; i++) { 185 for(j=i+1; j<n; j++) { 186 if(stu[j].sum<stu[i].sum) { 187 Exchange(stu,i,j); 188 } 189 } 190 } 191 } 192 193 void PrintDataSort(STUDENT stu[],int n,int m) { 194 int i,j; 195 for(i=0; i<n; i++) { 196 printf("%10ld%8s",stu[i].studentID,stu[i].studentName); 197 for(j=0; j<m; j++) { 198 printf("%4d",stu[i].score[j]); 199 } 200 printf("%6d ",stu[i].sum); 201 printf("%.2f ",stu[i].aver); 202 } 203 printf(" "); 204 } 205 206 void NameSort(STUDENT stu[],int n,int m) { 207 int i,j,k; 208 char temp[N]; 209 for(i=0; i<n-1; i++) { 210 for(j=i+1; j<n; j++) { 211 if(strcmp(stu[i].studentName,stu[j].studentName)>0) { 212 Exchange(stu,i,j); 213 } 214 } 215 } 216 } 217 218 void PrintNameSort(STUDENT stu[],int n,int m) { 219 int i,j; 220 for(i=0; i<n; i++) { 221 printf("%10ld%8s",stu[i].studentID,stu[i].studentName); 222 for(j=0; j<m; j++) { 223 printf("%4d",stu[i].score[j]); 224 } 225 printf("%6d ",stu[i].sum); 226 printf("%.2f ",stu[i].aver); 227 } 228 printf(" "); 229 } 230 231 void SearchID(STUDENT stu[],int n,int m) { 232 int i,x=-1,count=1; 233 long t; 234 printf("Input ID:"); 235 scanf("%ld",&t); 236 do { 237 x++; 238 } while(t!=stu[x].studentID); 239 for(i=0; i<n; i++) { 240 if(stu[i].score>stu[x].score) 241 count+=1; 242 } 243 printf("%2d%10ld%8s",count,stu[x].studentID,stu[x].studentName); 244 for(i=0; i<m; i++) { 245 printf("%4d",stu[x].score[i]); 246 } 247 printf("%6d ",stu[i].sum); 248 printf("%.2f ",stu[i].aver); 249 } 250 251 void SearchName(STUDENT stu[],int n,int m) { 252 int i,x=-1,count=1; 253 char name[10]; 254 printf("Input Name:"); 255 scanf("%s",&name); 256 do { 257 x++; 258 } while(strcmp(name,stu[x].studentName)!=0); 259 for(i=0; i<n; i++) { 260 if(stu[i].sum>stu[x].sum) 261 count+=1; 262 } 263 printf("%2d%10ld%8s",count,stu[x].studentID,stu[x].studentName); 264 for(i=0; i<m; i++) { 265 printf("%4d",stu[x].score[i]); 266 } 267 printf("%6d ",stu[i].sum); 268 printf("%.2f ",stu[i].aver); 269 } 270 271 void WritetoFile(STUDENT stu[],int n,int m) { 272 FILE *fp; 273 int i,j; 274 if((fp=fopen("test.txt","w"))==NULL) { //以写方式打开文本文件 275 printf("Failure to open test.txt! "); 276 exit(0); 277 } 278 for(i=0; i<n; i++) { 279 fprintf(fp,"%10ld%8s",stu[i].studentID,stu[i].studentName); 280 for(j=0; j<m; j++) { 281 fprintf(fp,"%4d",stu[i].score[j]); 282 } 283 fprintf(fp,"%6d ",stu[i].sum); 284 fprintf(fp,"%.2f ",stu[i].aver); 285 } 286 fclose(fp); 287 } 288 289 void ReadfromFile(STUDENT stu[],int *n,int *m) { 290 FILE *fp; 291 int i,j; 292 if((fp=fopen("test.txt","r"))==NULL) { //以读方式打开文本文件 293 printf("Failure to open test.txt! "); 294 exit(0); 295 } 296 for(i=0; i<*n; i++) { 297 fscanf(fp,"%10ld",&stu[i].studentID); 298 fscanf(fp,"%8s",stu[i].studentName); 299 for(j=0; j<3; j++) { 300 fscanf(fp,"%4d",&stu[i].score[j]); 301 } 302 fscanf(fp,"%6d",&stu[i].sum); 303 fscanf(fp,"%f",&stu[i].aver); //不能使用%.2f格式 304 } 305 fclose(fp); 306 } 307 308 void PrintScore(STUDENT stu[],int n,int m) { 309 int i,j; 310 for(i=0; i<n; i++) { 311 printf("%10ld",stu[i].studentID); 312 printf("%8s",stu[i].studentName); 313 for(j=0; j<3; j++) { 314 printf("%4d",stu[i].score[j]); 315 } 316 printf("%6d ",stu[i].sum); 317 printf("%.2f ",stu[i].aver); 318 } 319 printf(" "); 320 }