還有平均成績的輸出也只能輸出一個,這是什么問題求大佬指點?
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h> //提供 exit() 函數(shù)支持
- #define SIZE 100000
- //#include"conio.h"
- //聲明函數(shù)原型
- void finput(); //存入數(shù)據(jù)
- void flist(); //輸出數(shù)據(jù)
- void fmax(); //取最大成績
- void ftaxis(); //數(shù)據(jù)排序
- void stat(); //統(tǒng)計數(shù)據(jù)
- //定義結構體
- struct student
- {
- int num; //存放
- char name[10]; //定義名字數(shù)組變量
- int En; //英語課成績
- int Math; //數(shù)學課成績
- int C; //計算機語言成績
- int Mz; //馬哲成績
- int Ei; //電子技術
- float ave; //三門課平均成績
- } stu[SIZE]; //在定義結構體時同時聲明結構體變量
- volatile int length = 0;
- int main()
- {
- int n;
- for(;;) //使程序能夠循環(huán),在循環(huán)內部判斷何時結束
- {
- printf("\n\n"); //與上次的輸出結果保持間隔,便于美觀
- printf(" _________________________________\n");//菜單輸出
- printf(" | MENU |\n");
- printf(" | 1.輸入成績 |\n");
- printf(" | 2.輸出最大成績 |\n");
- printf(" | 3.輸出按成績大小排列 |\n");
- printf(" | 4.輸入數(shù)字查看人數(shù)占比 |\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 //沒有按規(guī)范選擇功能,要進行錯誤提示 //錯誤提示
- {
- printf("\n ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- printf(" error input! \n");
- printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
- // break; //應該將這一句話去掉才能使在輸錯的情況下也進行菜單循環(huán)
- }
- }
- return 0;
- }
- //part 1 : 進行信息錄入
- void finput()
- {
- FILE *fp; //聲明指針變量
- int i;
- fp=fopen("grades","wb"); //首次打開文件, 若沒有文件將會自動創(chuàng)建新文件
- for(i=0; i<SIZE; i++)
- {
- printf("1 to continue and 0 to end input: "); //詢問是否繼續(xù)輸入
- 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: 統(tǒng)計指定分數(shù)之上的人數(shù)
- void stat()
- {
- FILE *fp;
- int i,flag=0,count=0,b;//flag 用來判斷是否需要進行錯誤提示, count用來統(tǒng)計個數(shù)
- 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);
- }
復制代碼
|