找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 964|回复: 4
收起左侧

C语言finput这个函数只会写入stu[1],输入一个覆盖一个。

[复制链接]
ID:881546 发表于 2021-11-25 14:35 | 显示全部楼层 |阅读模式
还有平均成绩的输出也只能输出一个,这是什么问题求大佬指点?

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

  6. //声明函数原型
  7. void finput();    //存入数据
  8. void flist();     //输出数据
  9. void fmax();      //取最大成绩
  10. void ftaxis();    //数据排序
  11. void stat();      //统计数据
  12. //定义结构体
  13. struct student
  14. {
  15.         int num;        //存放
  16.         char name[10];  //定义名字数组变量
  17.         int En;         //英语课成绩
  18.         int Math;       //数学课成绩
  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(;;)          //使程序能够循环,在循环内部判断何时结束
  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.输入数字查看人数占比  |\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 //没有按规范选择功能,要进行错误提示     //错误提示
  63.                 {
  64.                 printf("\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  65.                 printf("           error input!       \n");
  66.                 printf("    ~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  67. //                        break; //应该将这一句话去掉才能使在输错的情况下也进行菜单循环
  68.                 }
  69.         }
  70.         return 0;
  71. }
  72. //part 1 : 进行信息录入

  73. void finput()
  74. {
  75.         FILE *fp;            //声明指针变量
  76.         int i;
  77.         fp=fopen("grades","wb");          //首次打开文件, 若没有文件将会自动创建新文件
  78.         for(i=0; i<SIZE; i++)
  79.         {
  80.                 printf("1 to continue and 0 to end input: ");        //询问是否继续输入
  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: 统计指定分数之上的人数
  177. void stat()
  178. {
  179.         FILE *fp;
  180.         int i,flag=0,count=0,b;//flag 用来判断是否需要进行错误提示, count用来统计个数
  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. }
复制代码


回复

使用道具 举报

ID:883242 发表于 2021-11-25 17:44 | 显示全部楼层
你读了一次就把文件关闭了,下次重新打开当然还是第一条记录。
回复

使用道具 举报

ID:881546 发表于 2021-11-25 18:40 | 显示全部楼层
Hephaestus 发表于 2021-11-25 17:44
你读了一次就把文件关闭了,下次重新打开当然还是第一条记录。

那应该怎么改
回复

使用道具 举报

ID:139866 发表于 2021-11-30 15:23 | 显示全部楼层
好家伙,声明个变量8000000个字节
回复

使用道具 举报

ID:844772 发表于 2021-11-30 15:40 | 显示全部楼层
1.fclose(fp); 要放到for循环之外,否则第一条就关闭的文件指针,就不能继续了。
2.你应该使用链表结构,而不是定义一个巨大的数组。
3.你的存储函数有问题,没有考虑插入问题,如果二次运行就会混乱了。
另外,能解释一下这个程序我们单片机论坛的关系吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表