久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1678|回復: 4
打印 上一主題 下一主題
收起左側

C語言finput這個函數(shù)只會寫入stu[1],輸入一個覆蓋一個。

[復制鏈接]
跳轉到指定樓層
樓主
ID:881546 發(fā)表于 2021-11-25 14:35 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
還有平均成績的輸出也只能輸出一個,這是什么問題求大佬指點?

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>   //提供 exit() 函數(shù)支持  
  4. #define SIZE 100000
  5. //#include"conio.h"

  6. //聲明函數(shù)原型
  7. void finput();    //存入數(shù)據(jù)
  8. void flist();     //輸出數(shù)據(jù)
  9. void fmax();      //取最大成績
  10. void ftaxis();    //數(shù)據(jù)排序
  11. void stat();      //統(tǒng)計數(shù)據(jù)
  12. //定義結構體
  13. struct student
  14. {
  15.         int num;        //存放
  16.         char name[10];  //定義名字數(shù)組變量
  17.         int En;         //英語課成績
  18.         int Math;       //數(shù)學課成績
  19.     int C;          //計算機語言成績
  20.         int Mz;          //馬哲成績
  21.         int Ei;          //電子技術
  22.         float ave;      //三門課平均成績
  23. } stu[SIZE];        //在定義結構體時同時聲明結構體變量

  24. volatile int length = 0;

  25. int main()
  26. {
  27.         int n;
  28.         for(;;)          //使程序能夠循環(huán),在循環(huán)內部判斷何時結束
  29.         {
  30.                 printf("\n\n");        //與上次的輸出結果保持間隔,便于美觀
  31.                 printf("        _________________________________\n");//菜單輸出
  32.                 printf("        |           MENU                |\n");
  33.                 printf("        |       1.輸入成績              |\n");               
  34.                 printf("        |       2.輸出最大成績          |\n");
  35.                 printf("        |       3.輸出按成績大小排列    |\n");
  36.                 printf("        |       4.輸入數(shù)字查看人數(shù)占比  |\n");
  37.                 printf("        |       5.  退出                |\n");
  38.                 printf("        |_______________________________|\n");
  39.                 printf("  Please choose your function:(1-5):");          //功能選擇
  40.                 scanf("%d",&n);
  41.                 printf("\n");
  42.         if(n>0&&n<=8) {
  43.                         switch(n) {
  44.                    case 1:
  45.                                         finput();
  46.                                         break;
  47.                    case 2:
  48.                     flist();
  49.                                         fmax();
  50.                                         break;
  51.                                    case 3:
  52.                                         ftaxis();
  53.                                         break;
  54.                                    case 4:
  55.                                         stat();
  56.                                 case 5:                 
  57.                 printf("\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");     //退出提示
  58.                 printf("             Goodbye!         \n");
  59.                 printf("    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  60.                                         exit(0);//將程序退出
  61.                         }
  62.                 } else //沒有按規(guī)范選擇功能,要進行錯誤提示     //錯誤提示
  63.                 {
  64.                 printf("\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  65.                 printf("           error input!       \n");
  66.                 printf("    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  67. //                        break; //應該將這一句話去掉才能使在輸錯的情況下也進行菜單循環(huán)
  68.                 }
  69.         }
  70.         return 0;
  71. }
  72. //part 1 : 進行信息錄入

  73. void finput()
  74. {
  75.         FILE *fp;            //聲明指針變量
  76.         int i;
  77.         fp=fopen("grades","wb");          //首次打開文件, 若沒有文件將會自動創(chuàng)建新文件
  78.         for(i=0; i<SIZE; i++)
  79.         {
  80.                 printf("1 to continue and 0 to end input: ");        //詢問是否繼續(xù)輸入
  81.                 scanf("%d",&i);
  82.                 if(i==0)
  83.                 {
  84.                         fclose(fp);
  85.                         return;
  86.                 }
  87.                   else {
  88.                         printf("Please inut your school number, your name,English grade, Math grade, C grade, Mazhe grade, Electronic grade:\n");
  89.                         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);
  90.                         stu[i].ave=(stu[i].C+stu[i].Math+stu[i].En+stu[i].Mz+stu[i].Ei)/5.0;
  91.                         fwrite(&stu[i],sizeof(struct student),1,fp);
  92.                         length++;
  93.                 }
  94.         fclose(fp);
  95.         }
  96. }

  97. //part 2: 將結果輸出

  98. void fmax()
  99. {
  100.           int res[256];
  101.         int max;
  102.         int maximum;
  103.         size_t i;
  104.         for(i = 0; i < length; i++){
  105.                 res[i] = stu[i].C+stu[i].Math+stu[i].En+stu[i].Mz+stu[i].Ei;
  106.         }
  107.         printf("%d", length);
  108.         max = res[0];
  109.         for(i = 0; i < length; i++){
  110.                 if(res[i] >= max) {
  111.                         max = res[i];
  112.                         maximum = i;
  113.                 }
  114.         }
  115.         printf("The most niubility student information is:\n");
  116.         printf("Student ID\t Name\t English\t Math\t C \t Electronic\t Maze\t \n");
  117.         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);
  118. }

  119. void flist()
  120. {
  121.         FILE *fp;
  122.         int i;
  123.         fp=fopen("grades","rb");
  124.         printf("____________________________________________________________________________________________________\n");
  125.         printf("|                                                  Report                                           |\n");
  126.         printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
  127.         printf("|        ID      |    name   |   English grade    |    Math grade   |   Computer grade   | Ma  zhe  | Electron grade |  Average  |\n");
  128.         printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
  129.         for(i=0; fread(&stu[i],sizeof(struct student),1,fp)==1; i++)
  130.                 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);
  131.                 /*
  132.                 此處采用字寬輸入比加空格更好, 因為若是使用空格輸入,當學號或者名字前后輸入的長度不一致時,格式會亂掉,導致無法對其格式
  133.                 */
  134.         printf("|___________________________________________________________________________________________________|\n\n");
  135.         fclose(fp);
  136. }//part 3:按平均成績進行排序
  137. void ftaxis()
  138. {
  139.         int i,j,n;
  140.         FILE *fp;
  141.         struct student t;
  142.         fp=fopen("grades","rb");
  143.         printf("____________________________________________________________________________________________________ \n");
  144.         printf("|                                                  Report                                           |\n");
  145.         printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
  146.         printf("|        ID      |    name   |   English grade    |    Math grade   |   C grade   |    Ma  zhe  |   Electron grade |    Average  |\n");
  147.         printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
  148.         for(i=0; fread(&stu[i],sizeof(struct student),1,fp)==1; i++)
  149.                 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);
  150.         printf("|___________________________________________________________________________________________________|\n\n");
  151.         fclose(fp);
  152.         n=i;
  153.         for(i=0; i<n; i++)
  154.                 for(j=i+1; j<n; j++)
  155.                         if(stu[i].ave<stu[j].ave)
  156.                         {
  157.                                 t=stu[i];
  158.                                 stu[i]=stu[j];
  159.                                 stu[j]=t;
  160.                         }
  161.         fp=fopen("grades","wb");
  162.         printf("\n\n\n");
  163.         printf("_____________________________________________________________________________________________________\n");
  164.         printf("|                                                  Report                                           |\n");
  165.     printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
  166.     printf("|        ID      |    name   |   English grade    |    Math grade   |   C grade   |    Ma  zhe  |   Electron grade |    Average  |\n");
  167.     printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
  168.         for(i=0; i<n; i++)
  169.         {
  170.                 fwrite(&stu[i],sizeof(struct student),1,fp);
  171.                 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);
  172.         }
  173.         printf("|___________________________________________________________________________________________________|\n");
  174.         fclose(fp);
  175. }
  176. //part 4: 統(tǒng)計指定分數(shù)之上的人數(shù)
  177. void stat()
  178. {
  179.         FILE *fp;
  180.         int i,flag=0,count=0,b;//flag 用來判斷是否需要進行錯誤提示, count用來統(tǒng)計個數(shù)
  181.         float score_;
  182.         fp=fopen("grades","rb");
  183.         rewind(fp);
  184.         printf("Do you want to know the percentage of the total score?\nEnter your number: ");
  185.         scanf("%f",&score_);
  186.         
  187.         for(i=0; i<=SIZE; i++)
  188.         {
  189.                 if(score_ < stu[i].ave )
  190.                 {
  191.                         count++;
  192.                         flag=1;
  193.                 }
  194.         }
  195.         if(flag == 1)
  196.                 printf("%d in total above your number\n",count);
  197.         if(flag==0)
  198.         {
  199.                 printf("\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  200.                 printf("           error input!       \n");
  201.                 printf("    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  202.         }
  203. //        printf("___________________________________________________________________________________________________________\n\n");
  204.         fclose(fp);
  205. }
復制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:883242 發(fā)表于 2021-11-25 17:44 | 只看該作者
你讀了一次就把文件關閉了,下次重新打開當然還是第一條記錄。
回復

使用道具 舉報

板凳
ID:881546 發(fā)表于 2021-11-25 18:40 | 只看該作者
Hephaestus 發(fā)表于 2021-11-25 17:44
你讀了一次就把文件關閉了,下次重新打開當然還是第一條記錄。

那應該怎么改
回復

使用道具 舉報

地板
ID:139866 發(fā)表于 2021-11-30 15:23 | 只看該作者
好家伙,聲明個變量8000000個字節(jié)
回復

使用道具 舉報

5#
ID:844772 發(fā)表于 2021-11-30 15:40 | 只看該作者
1.fclose(fp); 要放到for循環(huán)之外,否則第一條就關閉的文件指針,就不能繼續(xù)了。
2.你應該使用鏈表結構,而不是定義一個巨大的數(shù)組。
3.你的存儲函數(shù)有問題,沒有考慮插入問題,如果二次運行就會混亂了。
另外,能解釋一下這個程序我們單片機論壇的關系嗎?
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 在线观看第一页 | 日本精品视频一区二区 | 黑人巨大精品欧美一区二区免费 | www免费视频 | 欧美性网| 国产色网站 | 午夜久久久久久久久久一区二区 | 中文字幕一区二区三区日韩精品 | 91不卡| 一区二区三区国产好 | 新91| 日本精品视频一区二区 | av网站推荐 | 成人免费视频网 | 人人九九精 | 欧美黄色片在线观看 | 91视频电影 | 久久精品福利视频 | 日韩不卡一区二区 | 我想看一级黄色毛片 | 日韩亚洲视频 | 国产极品粉嫩美女呻吟在线看人 | 一区二区三区免费在线观看 | 国内久久精品 | 狠狠操狠狠干 | 国产精选一区 | jdav视频在线观看免费 | 日韩小视频在线 | 麻豆91精品91久久久 | 欧美日韩国产一区二区三区 | 国产久 | 黄色大片网| 精品国产乱码久久久久久88av | 日韩免费一二三区 | 黄网站在线观看 | 中文字幕一级 | 欧美激情视频一区二区三区在线播放 | 黄色av网站在线观看 | 亚洲精品电影网在线观看 | 久久精品免费 | 成人亚洲视频 |