//学生成绩管理系统 //用户名:enjoy65 //密码: enjoy65 #include<stdio.h> #include<string.h> #include<stdlib.h> //定义全局变量x,i,j,n char x; int i,j, n; struct student //定义结构体 { int id ; char name[20]; float Math; float English; float Chinese; float average; }stu[5]; /*---------------------------------- ------------------------------------*/ void my_add() //获取数据 并输出,最后退出 { FILE *fp; //文件指针 char t; int i; if((fp = fopen("stu_dat","w")) == NULL) { printf("cannot open file \n"); return ; } for(i=0; i<5; i++) //输入数据 { printf("Please enter %d student ID:", i+1); scanf("%d",&stu[i].id); printf("Please enter %d student name:", i+1); scanf("%s",stu[i].name); printf("Please enter %d student Math score:", i+1); scanf("%f",&stu[i].Math); printf("Please enter %d student English score:", i+1); scanf("%f",&stu[i].English); printf("Please enter %d student Chinese score:", i+1); scanf("%f",&stu[i].Chinese); stu[i].average = (stu[i].Math + stu[i].English + stu[i].Chinese)/3; printf("\n"); fwrite(&stu[i], sizeof(struct student), 1, fp); //将stu的数据写入文件中保存 } fclose(fp); //关闭文件 system("cls"); //清屏 printf("num\tID\tname\tMath\tEnglish\t Chinese\taverage\n"); for(i=0 ; i<5; i++) { printf("%d\t%d\t%s\t%.2f\t%.2f\t %.2f\t\t%.2f\n", i+1 ,stu[i].id,stu[i].name,stu[i].Math,stu[i].English, stu[i].Chinese,stu[i].average); printf("\n"); } //按格式显示所输入数据 printf("Press any key to exit : "); getchar(); scanf("%c",&x); } /*---------------------------------- ------------------------------------*/ void my_View() //将所得数据按平均分进行排序 从大到小 并输出排序后的数据,最后退出 { char x; struct student temp; FILE *fp; if((fp = fopen("stu_dat","w")) == NULL) { printf("cannot open file\n"); return; } fread(stu, sizeof(struct student), 5, fp); //读取硬盘文件中数据到内存 for(i=0; i<4; i++) for(j=0; j<4-i; j++) //进行冒泡排序 小的在最右边 从大到小 if(stu[j].average<stu[j+1].average) { temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } fwrite(stu, sizeof(struct student), 5, fp); //将更改后的数据写入硬盘文件 fclose(fp); //关闭文件 system("cls"); //清空屏幕 printf("num\tID\tname\tMath\tEnglish\tChinese\t\tAve\n"); //按格式显示修改之后的数据 for(i=0; i<5; i++) { printf("%d\t%d\t%s\t%.2f\t%.2f\t%.2f\t\t%.2f\n", i+1 ,stu[i].id,stu[i].name,stu[i].Math, stu[i].English,stu[i].Chinese,stu[i].average); } printf("\n"); printf("press any key to exit \n"); getchar(); scanf("%c",&x); } /*---------------------------------- ------------------------------------*/ void average(struct student mt[5]) //求得并输出数学、英语、语文的成绩平均值,最后退出 { int i; char x; float sum1 = 0, sum2 = 0, sum3 = 0; float ave1, ave2, ave3; for(i=0; i<5; i++) { sum1 = sum1 + mt[i].Math; sum2 = sum2 + mt[i].English; sum3 = sum3 + mt[i].Chinese; } //分别求数学、英语、语文的成绩总和 ave1 = sum1 / 5; ave2 = sum2 / 5; ave3 = sum3 / 5; //分别求数学、英语、语文的成绩平均值 printf("Average score of the class:\n"); printf("Math : %.2f\n", ave1); printf("English : %.2f\n", ave2); printf("Chinese : %.2f\n", ave3); printf("\n"); //分别输出数学、英语、语文的成绩平均值 printf("Press any key to exit : "); getchar(); scanf("%c",&x); } /*---------------------------------- ------------------------------------*/ void my_modify_function( int n ) // 只对某位学号为n的学生的基本数据进行修改,并输出 //修改后的总数据,如没有学号为n的学生则报错,最后退出 { FILE *fp; FILE *ofp; if((fp = fopen("stu_dat","w+")) == NULL) { printf("cannot open file \n"); return ; } fread(stu, sizeof(struct student), 5, fp); //读取文件中的数据 for(i=0; i<5; i++) { if(n == stu[i].id) // 找到某位学号为n的学生 并对其数据进行修改 { printf("Please enter student name:"); //修改的数据包括:名字,数学、英语、语文成绩,三科平均分 scanf("%s",stu[i].name); printf("Please enter student Math score:"); scanf("%f",&stu[i].Math); printf("Please enter student English score:"); scanf("%f",&stu[i].English); printf("Please enter student Chinese score:"); scanf("%f",&stu[i].Chinese); stu[i].average = (stu[i].Math + stu[i].English + stu[i].Chinese)/3; for(i=0; i<5; i++) fwrite(&stu[i],sizeof(struct student),1,fp); //将修改后的数据重新写入文件中 fclose(fp); //关闭文件 printf("\n"); system("cls"); //清屏 printf("num\tID\tname\tMath\tEng\tChinese\t\tave\n"); //按指定格式输出总数据 for(i=0 ; i<5; i++) printf("%d\t%d\t%s\t%.2f\t%.2f\t%.2f\t\t%.2f\n", i ,stu[i].id,stu[i].name,stu[i].Math,stu[i].English,stu[i].Chinese,stu[i].average); printf("\n"); printf("Press any key to exit : "); //按任意键退出 getchar(); scanf("%c",&x); return; } } fclose(fp); //如果没有学号为n的学生 则关闭文件 printf("cannot find the student ID!"); //报错 printf("\n"); printf("Press any key to exit : "); //退出 getchar(); scanf("%c",&x); } /*---------------------------------- ------------------------------------*/ void my_modefy() //要求输入需要修改的学生学号n,并调用函数my_modify_function 进行具体修改 { int n; while(1) { printf("Please enter you need modify student ID:\n"); scanf("%d",&n); my_modify_function(n); break; } } /*---------------------------------- ------------------------------------*/ void my_find() //根据学号得到某位学生的基本数据并输出,最后退出 { int fd; printf("Please enter the student ID : "); //输入所求学生学号 scanf("%d",&fd); system("cls"); //清屏 for(i=0; fd != stu[i].id; i++) ; // 如果i和学生学号不符合,则不进行任何操作,并让i继续累加 // 直至i与学生学号相等时结束for循环,即根据所输入学号fd找出所 // 求学生stu[i]。 printf("num\tID\tname\tMath\tEnglish\t Chinese\n"); printf("%d\t%s\t%.2f\t%.2f\t%.2f\t\t%.2f\n", //按格式输出该学生的基本数据 stu[i].id,stu[i].name,stu[i].Math, stu[i].English,stu[i].Chinese,stu[i].average); printf("Press any key to exit : "); //退出 getchar(); scanf("%c",&x); } /*---------------------------------- ------------------------------------*/ void my_exit(char *T) //退出 { printf("--------------------------------------\n\n\n\n\n\n\n\n\n\n\n"); printf("Whether out of this link Y/N : "); getchar(); //接收所输入字符 scanf("%c",T); } int my_doc() { system("cls"); //清空屏幕 printf("\t*******************************************************\n"); printf("\t*******************************************************\n"); printf("\t** **\n"); printf("\t** **\n"); printf("\t** STUDENT MANAGEMENT SYSTEM **\n"); printf("\t** **\n"); printf("\t** Please choose! **\n"); printf("\t** 1 : Add five student information **\n"); //调用函数 my_add() printf("\t** 2 : View all results **\n"); //调用函数 my_View() printf("\t** 3 : Average score of the class **\n"); //调用函数 average printf("\t** 4 : Find student score **\n"); //调用函数 my_find() printf("\t** 5 : Modify student information **\n"); //调用函数 my_modefy()、my_modify_function printf("\t** 0 : Exit **\n"); //调用函数 my_exit printf("\t** **\n"); printf("\t** **\n"); printf("\t*******************************************************\n"); printf("\t*******************************************************\n"); printf("\n\n\n\n\n\n\n"); printf("\tyou are choose:"); scanf("%d",&n); //接收选取结果 return n; } /*---------------------------------- ------------------------------------*/ int main() { char name[20]="enjoy65",_name[20]; char password[10]="enjoy65",_password[10]; int f1,f2; int i; char T; printf("\t\t ******* 欢迎进入学生成绩管理系统 *******\n\n"); printf("\t\t 用户请登录\n\n"); for(i=0;i!=5;) { printf("\t 用户名:"); gets(_name); f1=strcmp(_name,name); printf("\t 密码:"); gets(_password); f2=strcmp(_password,password); if(f1==0 && f2==0) { printf("\t\t\t\t 登陆成功!"); break; } else { printf("\n\t\t 用户名或密码错误!"); printf("您还有%d次机会\n\n",4-i); i++; } } if(i==5) {printf("对不起,您的输入次数过多\n"); return 0;} do //先执行do语句 { switch(my_doc()) //switch语句:先调用函数my_doc()得到其返回值n,再判断 { case 1: //如果函数my_doc的返回值n为1,则调用函数my_add(), 并结束switch语句 my_add(); break; case 2: my_View(); break; case 3: average(stu); break; case 4: my_find(); break; case 5: my_modefy(); break; case 0: my_exit(&T); //函数的地址传递,可改变实参T的值 break; default: //如果n与以上情况均不符合,则报错 printf("Error! Please input the new.\n"); } }while(T != ‘y‘ && T != ‘Y‘); // 如果判断返回值n为0调用了my_exit函数,则字符T被赋初值, // 接着判断T是否为‘y’或‘Y’如果是,则不再执行do..while语句 // 反之,如果n值不为0,而是1,2,3,4,5中任何一个,则在 // 调用完对应函数后,while语句为真,返回继续执行do语句。 system("cls"); //清屏 return 0; }
这是闲来无事自己注释一个学生管理系统,个人认为主要是函数调用以及利用文件指针打开保存文件,对其中的一些易错或比较难理解的点进行了较详细的注释。与大家分享!
但在注释过程中发现一个问题,在自定义函数中如my_add、my_view,如果去掉最后几行代码,则在主函数调用时,该函数不可正常工作,目前还不知道具体应该怎么理解。
printf("Press any key to exit : "); getchar(); scanf("%c",&x);
时间: 2025-01-02 01:38:01