还有平均成绩的输出也只能输出一个,这是什么问题求大佬指点?
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h> //提供 exit() 函数支持
- #define SIZE 100000
- //#include"conio.h"
- //声明函数原型
- void finput(); //存入数据
- void flist(); //输出数据
- void fmax(); //取最大成绩
- void ftaxis(); //数据排序
- void stat(); //统计数据
- //定义结构体
- struct student
- {
- int num; //存放
- char name[10]; //定义名字数组变量
- int En; //英语课成绩
- int Math; //数学课成绩
- int C; //计算机语言成绩
- int Mz; //马哲成绩
- int Ei; //电子技术
- float ave; //三门课平均成绩
- } stu[SIZE]; //在定义结构体时同时声明结构体变量
- volatile int length = 0;
- int main()
- {
- int n;
- for(;;) //使程序能够循环,在循环内部判断何时结束
- {
- printf("\n\n"); //与上次的输出结果保持间隔,便于美观
- printf(" _________________________________\n");//菜单输出
- printf(" | MENU |\n");
- printf(" | 1.输入成绩 |\n");
- printf(" | 2.输出最大成绩 |\n");
- printf(" | 3.输出按成绩大小排列 |\n");
- printf(" | 4.输入数字查看人数占比 |\n");
- printf(" | 5. 退出 |\n");
- printf(" |_______________________________|\n");
- printf(" Please choose your function:(1-5):"); //功能选择
- scanf("%d",&n);
- printf("\n");
- if(n>0&&n<=8) {
- switch(n) {
- case 1:
- finput();
- break;
- case 2:
- flist();
- fmax();
- break;
- case 3:
- ftaxis();
- break;
- case 4:
- stat();
- case 5:
- printf("\n ~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); //退出提示
- printf(" Goodbye! \n");
- printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- exit(0);//将程序退出
- }
- } else //没有按规范选择功能,要进行错误提示 //错误提示
- {
- printf("\n ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf(" error input! \n");
- printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- // break; //应该将这一句话去掉才能使在输错的情况下也进行菜单循环
- }
- }
- return 0;
- }
- //part 1 : 进行信息录入
- void finput()
- {
- FILE *fp; //声明指针变量
- int i;
- fp=fopen("grades","wb"); //首次打开文件, 若没有文件将会自动创建新文件
- for(i=0; i<SIZE; i++)
- {
- printf("1 to continue and 0 to end input: "); //询问是否继续输入
- scanf("%d",&i);
- if(i==0)
- {
- fclose(fp);
- return;
- }
- else {
- printf("Please inut your school number, your name,English grade, Math grade, C grade, Mazhe grade, Electronic grade:\n");
- scanf("%d%s%d%d%d%d%d",&stu[i].num,stu[i].name,&stu[i].En,&stu[i].Math,&stu[i].C,&stu[i].Mz,&stu[i].Ei);
- stu[i].ave=(stu[i].C+stu[i].Math+stu[i].En+stu[i].Mz+stu[i].Ei)/5.0;
- fwrite(&stu[i],sizeof(struct student),1,fp);
- length++;
- }
- fclose(fp);
- }
- }
- //part 2: 将结果输出
- void fmax()
- {
- int res[256];
- int max;
- int maximum;
- size_t i;
- for(i = 0; i < length; i++){
- res[i] = stu[i].C+stu[i].Math+stu[i].En+stu[i].Mz+stu[i].Ei;
- }
- printf("%d", length);
- max = res[0];
- for(i = 0; i < length; i++){
- if(res[i] >= max) {
- max = res[i];
- maximum = i;
- }
- }
- printf("The most niubility student information is:\n");
- printf("Student ID\t Name\t English\t Math\t C \t Electronic\t Maze\t \n");
- printf("%d\t %s\t %d\t %d\t %d\t %d\t %d\t\n", stu[maximum].num,stu[maximum].name,stu[maximum].En,stu[maximum].Math,stu[maximum].C,stu[maximum].Mz,stu[maximum].Ei);
- }
- void flist()
- {
- FILE *fp;
- int i;
- fp=fopen("grades","rb");
- printf("____________________________________________________________________________________________________\n");
- printf("| Report |\n");
- printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
- printf("| ID | name | English grade | Math grade | Computer grade | Ma zhe | Electron grade | Average |\n");
- printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
- for(i=0; fread(&stu[i],sizeof(struct student),1,fp)==1; i++)
- printf("|%11d%15s%13d%17d%18d%16d%14d%19.2f\n",stu[i].num,stu[i].name,stu[i].En,stu[i].Math,stu[i].C,stu[i].Mz,stu[i].Ei,stu[i].ave);
- /*
- 此处采用字宽输入比加空格更好, 因为若是使用空格输入,当学号或者名字前后输入的长度不一致时,格式会乱掉,导致无法对其格式
- */
- printf("|___________________________________________________________________________________________________|\n\n");
- fclose(fp);
- }//part 3:按平均成绩进行排序
- void ftaxis()
- {
- int i,j,n;
- FILE *fp;
- struct student t;
- fp=fopen("grades","rb");
- printf("____________________________________________________________________________________________________ \n");
- printf("| Report |\n");
- printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
- printf("| ID | name | English grade | Math grade | C grade | Ma zhe | Electron grade | Average |\n");
- printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
- for(i=0; fread(&stu[i],sizeof(struct student),1,fp)==1; i++)
- printf("|%11d%15s%13d%17d%18d%16d%14d%19.2f\n",stu[i].num,stu[i].name,stu[i].En,stu[i].Math,stu[i].C,stu[i].Mz,stu[i].Ei,stu[i].ave);
- printf("|___________________________________________________________________________________________________|\n\n");
- fclose(fp);
- n=i;
- for(i=0; i<n; i++)
- for(j=i+1; j<n; j++)
- if(stu[i].ave<stu[j].ave)
- {
- t=stu[i];
- stu[i]=stu[j];
- stu[j]=t;
- }
- fp=fopen("grades","wb");
- printf("\n\n\n");
- printf("_____________________________________________________________________________________________________\n");
- printf("| Report |\n");
- printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
- printf("| ID | name | English grade | Math grade | C grade | Ma zhe | Electron grade | Average |\n");
- printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
- for(i=0; i<n; i++)
- {
- fwrite(&stu[i],sizeof(struct student),1,fp);
- printf("|%11d%15s%13d%17d%18d%16d%14d%19.2f\n",stu[i].num,stu[i].name,stu[i].En,stu[i].Math,stu[i].C,stu[i].Mz,stu[i].Ei,stu[i].ave);
- }
- printf("|___________________________________________________________________________________________________|\n");
- fclose(fp);
- }
- //part 4: 统计指定分数之上的人数
- void stat()
- {
- FILE *fp;
- int i,flag=0,count=0,b;//flag 用来判断是否需要进行错误提示, count用来统计个数
- float score_;
- fp=fopen("grades","rb");
- rewind(fp);
- printf("Do you want to know the percentage of the total score?\nEnter your number: ");
- scanf("%f",&score_);
-
- for(i=0; i<=SIZE; i++)
- {
- if(score_ < stu[i].ave )
- {
- count++;
- flag=1;
- }
- }
- if(flag == 1)
- printf("%d in total above your number\n",count);
- if(flag==0)
- {
- printf("\n ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf(" error input! \n");
- printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- }
- // printf("___________________________________________________________________________________________________________\n\n");
- fclose(fp);
- }
复制代码
|