鱼C论坛

 找回密码
 立即注册
查看: 1800|回复: 17

[已解决]学生成绩管理系统保存信息

[复制链接]
发表于 2021-11-4 07:32:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

这个只要输入两个学生信息之后,再次打开就会打不开,而且文件变得很大,搞不懂是怎么回事。保存在(35~53)打开文件在(431~483)

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<math.h>


  5. struct Student
  6. {
  7.         char name[40];
  8.         int num;
  9.         int English_mark;
  10.         int Math_mark;
  11.         int c_mark;
  12.         int avermark;
  13.         char sex;
  14.         int sum;
  15. };

  16. struct Line
  17. {
  18.         struct Student student;
  19.         struct Line *next;
  20. };

  21. int fmax(int a,int b)
  22. {
  23.         return a>b?a:b;
  24. }

  25. int fmin(int a,int b)
  26. {
  27.         return a>b?b:a;
  28. }

  29. void saveStudent(struct Line *head)
  30. {
  31.         FILE *fp;
  32.         struct Line *student;
  33.         fp=fopen("score.txt","wb+");
  34.         if(fp==NULL)
  35.         {
  36.                 printf("打开文件失败!!!\n");
  37.                 return;
  38.         }
  39.         student=head;
  40.         while(student!=NULL)
  41.         {
  42.                 fwrite(&student->student,sizeof(struct Student),1,fp);
  43.                 student=student->next;
  44.         }
  45.         fclose(fp);
  46.         printf("数据保存成功!!!\n");
  47. }

  48. void saveStudent2(struct Line *head)
  49. {
  50.         FILE *fp;
  51.         struct Line *student;
  52.         fp=fopen("score2.txt","wb+");
  53.         if(fp==NULL)
  54.         {
  55.                 printf("打开文件失败!!!\n");
  56.                 return;
  57.         }
  58.         student=head;
  59.         rewind(fp);
  60.         while(student!=NULL)
  61.         {
  62.                 fwrite(&student->student,sizeof(struct Student),1,fp);
  63.                 student=student->next;
  64.         }
  65.         fclose(fp);
  66.         printf("数据保存成功!!!\n");
  67. }

  68. void getInput(struct Line *student,struct Line *head)
  69. {
  70.         int count=0;
  71.         struct Line *temp;
  72.         temp=head;
  73.         printf("该学生的名字为:");
  74.         scanf("%s",student->student.name);
  75.         getchar();
  76.         while(student->student.sex!='M'&&student->student.sex!='F'&&student->student.sex!='f'&&student->student.sex!='m')
  77.         {       
  78.                 printf("该学生的性别为(M/F):");
  79.                 scanf("%c",&student->student.sex);
  80.                 getchar();
  81.         }
  82.         printf("该学生的学号为:");
  83.         scanf("%d",&student->student.num);
  84.         getchar();
  85.         while(1)
  86.         {
  87.                 count=0;
  88.                 while(student->student.num<0||student->student.num>999||student->student.num==' ')
  89.                 {       
  90.                         if(count>0)
  91.                         {
  92.                                 printf("该学生的学号为:");
  93.                                 scanf("%d",&student->student.num);
  94.                                 count++;
  95.                         }
  96.                         else if(count==0)
  97.                         {       
  98.                         count++;
  99.                         }
  100.                 }
  101.                 while((temp!=NULL)&&(temp->student.num!=student->student.num))
  102.                 {
  103.                         temp=temp->next;
  104.                 }
  105.                 if(temp==NULL)
  106.                 {
  107.                         break;
  108.                 }
  109.                 else
  110.                 {
  111.                         printf("该学生的学号重复,请重新输入!\n");
  112.                         printf("该学生的学号为:");
  113.                         scanf("%d",&student->student.num);
  114.                 }
  115.         }
  116.         printf("该学生的英语成绩为:");
  117.         scanf("%d",&student->student.English_mark);
  118.         getchar();
  119.         count=0;
  120.         while(student->student.English_mark<0||student->student.English_mark>100||student->student.English_mark==' ')
  121.         {
  122.                 if(count==0)
  123.                 {
  124.                         count++;
  125.                 }
  126.                 else
  127.                 {
  128.                         printf("该学生的英语成绩为:");
  129.                         scanf("%d",&student->student.English_mark);
  130.                         getchar();
  131.                 }
  132.         }
  133.         printf("该学生的高数成绩为:");
  134.         scanf("%d",&student->student.Math_mark);
  135.         getchar();
  136.         count=0;
  137.         while(student->student.Math_mark<0||student->student.Math_mark>100||student->student.Math_mark==' ')
  138.         {
  139.                 if(count==0)
  140.                 {
  141.                         count++;
  142.                 }
  143.                 else
  144.                 {
  145.                         printf("该学生的高数成绩为:");
  146.                         scanf("%d",&student->student.Math_mark);
  147.                         getchar();
  148.                 }
  149.         }
  150.         printf("该学生C语言程序设计的成绩为:");
  151.         scanf("%d",&student->student.c_mark);
  152.         getchar();
  153.         count=0;
  154.         while(student->student.c_mark<0||student->student.c_mark>100||student->student.c_mark==' ')
  155.         {
  156.                 if(count==0)
  157.                 {
  158.                         count++;
  159.                 }
  160.                 else
  161.                 {
  162.                         printf("该学生的C语言程序设计成绩为:");
  163.                         scanf("%d",&student->student.c_mark);
  164.                         getchar();
  165.                 }
  166.         }
  167.         student->student.avermark=(student->student.c_mark+student->student.English_mark+student->student.Math_mark)/3;
  168.         student->student.sum=student->student.c_mark+student->student.English_mark+student->student.Math_mark;
  169. }
  170. void addStudent(struct Line **head)
  171. {
  172.         struct Line *student,*temp,*previous,*current;
  173.         student=(struct Line*)malloc(sizeof(struct Line));
  174.         if(student==NULL)
  175.         {
  176.                 printf("内存分配失败!!");
  177.                 exit(1);
  178.         }
  179.         getInput(student,*head);
  180.        
  181.         if(*head==NULL)
  182.         {
  183.                 *head=student;
  184.                 student->next=NULL;
  185.         }
  186.         else
  187.         {
  188.                 current=*head;
  189.                 previous=NULL;
  190.                 while(current!=NULL&&current->student.avermark>student->student.avermark)
  191.                 {
  192.                         previous=current;
  193.                         current=current->next;
  194.                 }
  195.                 if(current==NULL)
  196.                 {
  197.                         previous->next=student;
  198.                         student->next=NULL;
  199.                 }
  200.                 else if(current!=NULL&&current!=*head)
  201.                 {
  202.                         student->next=current;
  203.                         previous->next=student;
  204.                 }
  205.                 else if(current==*head)
  206.                 {
  207.                         student->next=*head;
  208.                         *head=student;
  209.                 }
  210.                
  211.         }

  212. }

  213. struct Line *searchStudent(struct Line *head)
  214. {
  215.         char input[20];
  216.         int a;
  217.         int ch;
  218.         struct Line *student;
  219.         student=head;
  220.         printf("*************************\n");
  221.         printf("1.按照姓名查询\n");
  222.         printf("2.按照学号查询\n");
  223.         printf("3.返回菜单\n");
  224.         scanf("%d",&ch);
  225.         switch(ch)
  226.         {
  227.                 case 1:printf("请输入该学生的姓名:");
  228.                            scanf("%s",input);
  229.                            break;
  230.                 case 2:printf("请输入该学生的学号:");
  231.                            scanf("%d",&a);
  232.                            break;       
  233.                 case 3:return 0;             
  234.         }
  235.         while(student!=NULL)
  236.         {
  237.                 if(!strcmp(student->student.name,input)||student->student.num==a)
  238.                 {       
  239.                         break;       
  240.                 }
  241.                 student=student->next;
  242.         }
  243.         return student;
  244. }


  245. void printStudent(struct Line *student)
  246. {
  247.         if(student==NULL)
  248.         {
  249.                 printf("很抱歉查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n");
  250.                 return;
  251.         }
  252.         printf("姓名:%s\n",student->student.name);
  253.         printf("学号:%d\n",student->student.num);
  254.         printf("高数成绩:%d\n",student->student.Math_mark);
  255.         printf("英语成绩:%d\n",student->student.English_mark);
  256.         printf("c语言程序设计成绩:%d\n",student->student.c_mark);
  257.         printf("三科平均分为:%d\n",student->student.avermark);
  258. }

  259. void printMark(struct Line *head)
  260. {
  261.         struct Line *student;
  262.         int count=1;
  263.         printf("====================================\n");
  264.         printf("\t学生成绩表\t\n");
  265.         printf("\t姓名\t性别\t学号\t英语成绩\t高数成绩\tC语言程序设计成绩\t总分\t平均成绩\t排名\t\n");
  266.         student=head;
  267.         while(student!=NULL)
  268.         {
  269.                 printf("\t%s\t%c\t%d\t%d\t\t%d\t%d\t%d\t%d\t%d\t\n",student->student.name,student->student.sex,student->student.num,student->student.English_mark,student->student.Math_mark,student->student.c_mark,student->student.sum,student->student.avermark,count);
  270.                 student=student->next;
  271.                 count++;
  272.         }
  273.         printf("====================================\n");
  274.         system("pause");
  275. }

  276. void delStudent(struct Line **head)
  277. {
  278.         struct Line *previous,*current,*p1;
  279.         previous=NULL;
  280.         current=*head;
  281.         char input[20];
  282.         char ch;
  283.         int a;
  284.         printf("=====================================\n");
  285.         printf("1.按照名字查找需要删除的学生\n");
  286.         printf("2.按照学号查找需要删除的学生\n");
  287.         printf("3.返回菜单\n");
  288.         printf("请输入你的选择:");
  289.         scanf("%d",&ch);
  290.         switch(ch)
  291.         {
  292.                 case 1:printf("请输入该学生的名字:");
  293.                scanf("%s",input);
  294.                for(previous = NULL , current = * head ; current && strcmp(current -> student . name , input) ;  previous = current , current = current -> next) ;
  295.                if(current)
  296.                            {
  297.                     if(current == * head)
  298.                                                 {
  299.                             * head = current -> next ;
  300.                         } else
  301.                                                 {
  302.                             previous -> next = current -> next ;
  303.                         }
  304.                 } else
  305.                                 {
  306.                     printf("查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n")                                                                                     ;
  307.                 }
  308.                             break;
  309.                           
  310.                 case 2:printf("请输入该学生的学号:");
  311.                scanf("%d",&a);
  312.                for(previous = NULL , current = * head ; current && !(current->student.num==a) ;  previous = current , current = current -> next) ;
  313.                if(current)
  314.                            {
  315.                     if(current == * head)
  316.                                                 {
  317.                             * head = current -> next ;
  318.                         }
  319.                                                 else
  320.                                                 {
  321.                             previous -> next = current -> next ;
  322.                         }
  323.                 }
  324.                                 else
  325.                                 {
  326.                     printf("查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n")                                                                                     ;
  327.                 }
  328.                 break;
  329.         case 3:return;        
  330.         }
  331. }

  332. void changeStudent(struct Line **head)
  333. {
  334.         struct Line *temp;
  335.         struct Line *student;
  336.         int ch;
  337.         temp=searchStudent(*head);
  338.                                         if(temp==NULL)
  339.                                         {
  340.                                                 printStudent(temp);
  341.                                                 return;
  342.                                         }
  343.         printf("===============================\n");
  344.         printf("1.改变学生姓名\n");
  345.         printf("2.改变学生的性别\n");
  346.         printf("3.改变学生学号\n");
  347.         printf("4.改变学生高数成绩\n");
  348.         printf("5.改变学生英语成绩\n");
  349.         printf("6.改变学生C语言程序设计成绩\n");
  350.         printf("0.返回主菜单\n");
  351.         printf("===============================\n");
  352.         printf("请输入你的选择:");
  353.         scanf("%d",&ch);
  354.         switch(ch)
  355.         {
  356.                 case 1: printf("请输入你要改变后的名字:");
  357.                                 scanf("%s",temp->student.name);
  358.                                 break;
  359.                 case 2:printf("请输入你要改变后的性别:");
  360.                            scanf("%c",temp->student.sex);
  361.                            break;
  362.                 case 3:printf("请输入你要改变后的学号:");
  363.                            scanf("%d",&temp->student.num);
  364.                            break;
  365.                 case 4:printf("请输入你要改变后的高数成绩:");
  366.                            scanf("%d",&temp->student.Math_mark);       
  367.                            temp->student.avermark=(temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark)/3;
  368.                            temp->student.sum=temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark;
  369.                            break;
  370.                 case 5:printf("请输入你要改变后的英语成绩:");
  371.                            scanf("%d",&temp->student.English_mark);
  372.                            temp->student.avermark=(temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark)/3;
  373.                            temp->student.sum=temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark;
  374.                            break;
  375.                 case 6:printf("请输入你要改变后的C语言程序设计成绩:");
  376.                            scanf("%d",&temp->student.c_mark);
  377.                            temp->student.avermark=(temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark)/3;
  378.                            temp->student.sum=temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark;
  379.                            break;
  380.                 case 0:return;                                                   
  381.         }
  382. }

  383. void heart()
  384. {printf("      ****       ****\n");
  385.    printf("   *********   *********\n");
  386.    printf("************* *************\n");
  387.    int i,j;
  388.    for(i=0;i<3;i++)
  389.    {
  390.      for(j=0;j<29;j++)
  391.      {
  392.       printf("*");
  393.     }
  394.      printf("\n");
  395.   }
  396.   for(i=0;i<7;i++)
  397.    {
  398.      for(j=0;j<2*(i+1)-1;j++)
  399.      {
  400.       printf(" ");
  401.     }
  402.      for(j=0;j<27-i*4;j++)
  403.      {
  404.       printf("*");
  405.     }
  406.      printf("\n");
  407.     }
  408.   for(i=0;i<14;i++)
  409.   {
  410.     printf(" ");
  411.   }
  412.    printf("*\n") ;
  413. }

  414. void Openfile(struct Line **head)
  415. {
  416.         FILE *fp;
  417.         struct Line *temp,*previous,*current;
  418.         struct Line *student;
  419.         student=(struct Line*)malloc(sizeof(struct Line));
  420.         if(student==NULL)
  421.         {
  422.                 printf("内存分配失败!!!");
  423.                 return;
  424.         }
  425.         fp=fopen("score.txt","rb+");
  426.         if(fp==NULL)
  427.         {
  428.                 printf("打开文件失败!!!\n");
  429.                 return;
  430.         }
  431.         while(!feof(fp))
  432.         {
  433.                 fread(student,sizeof(struct Line ),1,fp);
  434.                 if(*head==NULL)
  435.                 {
  436.                         *head=student;
  437.                         student->next=NULL;
  438.                 }
  439.                 else
  440.                 {
  441.                         current=*head;
  442.                         previous=NULL;
  443.                         while(current!=NULL&&current->student.avermark>student->student.avermark)
  444.                         {
  445.                                 previous=current;
  446.                                 current=current->next;
  447.                         }
  448.                         if(current==NULL)
  449.                         {
  450.                                 previous->next=student;
  451.                                 student->next=NULL;
  452.                         }
  453.                         else if(current!=NULL&&current!=*head)
  454.                         {
  455.                                 student->next=current;
  456.                                 previous->next=student;
  457.                         }
  458.                         else if(current==*head)
  459.                         {
  460.                                 student->next=*head;
  461.                                 *head=student;
  462.                         }
  463.                
  464.                 }
  465.         }
  466.         fclose(fp);       
  467. }

  468. void Openfile2(struct Line **head)
  469. {
  470.         FILE *fp;
  471.         struct Line *temp,*previous,*current;
  472.         struct Line *student;
  473.         student=(struct Line*)malloc(sizeof(struct Line));
  474.         if(student==NULL)
  475.         {
  476.                 printf("内存分配失败!!!");
  477.                 return;
  478.         }
  479.         fp=fopen("score2.txt","rb");
  480.         if(fp==NULL)
  481.         {
  482.                 printf("打开文件失败!!!\n");
  483.                 return;
  484.         }
  485.         while(!feof(fp))
  486.         {
  487.                 fread(student,sizeof(struct Line ),1,fp);
  488.                 if(*head==NULL)
  489.                 {
  490.                         *head=student;
  491.                         student->next=NULL;
  492.                 }
  493.                 else
  494.                 {
  495.                         current=*head;
  496.                         previous=NULL;
  497.                         while(current!=NULL&&current->student.avermark>student->student.avermark)
  498.                         {
  499.                                 previous=current;
  500.                                 current=current->next;
  501.                         }
  502.                         if(current==NULL)
  503.                         {
  504.                                 previous->next=student;
  505.                                 student->next=NULL;
  506.                         }
  507.                         else if(current!=NULL&&current!=*head)
  508.                         {
  509.                                 student->next=current;
  510.                                 previous->next=student;
  511.                         }
  512.                         else if(current==*head)
  513.                         {
  514.                                 student->next=*head;
  515.                                 *head=student;
  516.                         }
  517.                
  518.                 }
  519.        
  520.         }
  521.         fclose(fp);
  522. }

  523. void draw(int i,char ch)
  524. {
  525.         int j;
  526.         if(i==0)
  527.         {
  528.                 printf("%c:NULL\n",ch);
  529.                 return;
  530.         }
  531.         printf("%c:",ch);
  532.         for(j=0;j<i;j++)
  533.         {
  534.                 printf("*");
  535.         }
  536.         printf("\n");
  537. }

  538. void getStudent_mark(struct Line *head)
  539. {
  540.         struct Line *student;
  541.         int math,math_max=-1,math_min=101,math_A=0,math_B=0,math_C=0,math_D=0,math_E=0,english,english_min=101,english_max=-1,english_A=0,english_B=0,english_C=0,english_D=0,english_E=0,c,c_min=101,c_max=-1,c_A=0,c_B=0,c_C=0,c_D=0,c_E=0,avermark,avermark_max=-1,avermark_min=101,avermark_A=0,avermark_B=0,avermark_C=0,avermark_D=0,avermark_E=0,count=0,sum_max=-1,sum_min=101,sum_mark;
  542.         student=head;
  543.         int ch;
  544.         int a;
  545.         while(student!=NULL)
  546.         {
  547.                 math=student->student.Math_mark;
  548.                 english=student->student.English_mark;
  549.                 c=student->student.c_mark;
  550.                 avermark=student->student.avermark;
  551.                 sum_mark=student->student.sum;
  552.                 math_max=fmax(math,math_max);
  553.                 math_min=fmin(math,math_min);
  554.                 english_max=fmax(english,english_max);
  555.                 english_min=fmin(english,english_min);
  556.                 c_max=fmax(c,c_max);
  557.                 c_min=fmin(c,c_min);
  558.                 avermark_max=fmax(avermark,avermark_max);
  559.                 avermark_min=fmin(avermark,avermark_min);
  560.                 sum_max=fmax(sum_mark,sum_max);
  561.                 sum_min=fmin(sum_mark,sum_min);
  562.                 switch(math/10)
  563.                 {
  564.                         case 10:math_A++;break;
  565.                         case 9:math_A++;break;
  566.                         case 8:math_B++;break;
  567.                         case 7:math_C++;break;
  568.                         case 6:math_D++;break;
  569.                         default:math_E++;break;
  570.                 }
  571.                 switch(english/10)
  572.                 {
  573.                         case 10:english_A++;break;
  574.                         case 9:english_A++;break;
  575.                         case 8:english_B++;break;
  576.                         case 7:english_C++;break;
  577.                         case 6:english_D++;break;
  578.                         default:english_E++;break;
  579.                 }
  580.                 switch(c/10)
  581.                 {
  582.                         case 10:c_A++;break;
  583.                         case 9:c_A++;break;
  584.                         case 8:c_B++;break;
  585.                         case 7:c_C++;break;
  586.                         case 6:c_D++;break;
  587.                         default:c_E++;break;
  588.                 }
  589.                 switch(avermark/10)
  590.                 {
  591.                         case 10:avermark_A++;break;
  592.                         case 9:avermark_A++;break;
  593.                         case 8:avermark_B++;break;
  594.                         case 7:avermark_C++;break;
  595.                         case 6:avermark_D++;break;
  596.                         default:avermark_E++;break;
  597.                 }
  598.                 count++;
  599.                 student=student->next;
  600.         }
  601.         printf("1.查询高数成绩的分布\n");
  602.         printf("2.查询英语成绩的分布\n");
  603.         printf("3.查询C语言程序设计成绩分布\n");
  604.         printf("4.查询平均分成绩分布\n");
  605.         printf("5.返回主菜单\n");
  606.         scanf("%d",&ch);
  607.         while(1)       
  608.         {
  609.                         switch(ch)
  610.                 {
  611.                         case 1:printf("该次高数考试共有%d个人参加,其中最高分为%d,最低分为%d\n",count,math_max,math_min);
  612.                                   draw(math_A,'A');
  613.                                    draw(math_B,'B');
  614.                                    draw(math_C,'C');
  615.                                    draw(math_D,'D');
  616.                                    draw(math_E,'E');
  617.                                    printf("是否想知道其他的成绩分布\n");
  618.                                 printf("1.否\n");
  619.                                 printf("2.C语言程序设计成绩分布\n");
  620.                                 printf("3.英语成绩分布\n");
  621.                                 printf("4.平均分成绩分布\n");
  622.                                 printf("请输入你的决定:");
  623.                                 scanf("%d",&a);
  624.                                 switch(a)
  625.                                 {
  626.                                         case 1:return;
  627.                                         case 2:ch=3;break;
  628.                                         case 3:ch=2;break;
  629.                                         case 4:ch=4;break;
  630.                                 }
  631.                                     
  632.                         case 2:printf("该次英语考试共有%d个人参加,其中最高分为%d,最低分为%d\n",count,english_max,english_min);
  633.                                    draw(english_A,'A');
  634.                                    draw(english_B,'B');
  635.                                    draw(english_C,'C');
  636.                                    draw(english_D,'D');
  637.                                    draw(english_E,'E');
  638.                                    printf("是否想知道其他的成绩分布\n");
  639.                                 printf("1.否\n");
  640.                                 printf("2.C语言程序设计成绩分布\n");
  641.                                 printf("3.高数成绩分布\n");
  642.                                 printf("4.平均分成绩分布\n");
  643.                                 printf("请输入你的决定:");
  644.                                 scanf("%d",&a);
  645.                                 switch(a)
  646.                                 {
  647.                                         case 1:return;
  648.                                         case 2:ch=3;break;
  649.                                         case 3:ch=1;break;
  650.                                         case 4:ch=4;break;
  651.                                 }
  652.                         case 3:printf("该次C语言程序与设计考试共有%d个人参加,其中最高分为%d,最低分为%d\n",count,c_max,c_min);
  653.                                    draw(c_A,'A');
  654.                                    draw(c_B,'B');
  655.                                    draw(c_C,'C');
  656.                                    draw(c_D,'D');
  657.                                    draw(c_E,'E');
  658.                                 printf("是否想知道其他的成绩分布\n");
  659.                                 printf("1.否\n");
  660.                                 printf("2.高数成绩分布\n");
  661.                                 printf("3.英语成绩分布\n");
  662.                                 printf("4.平均分成绩分布\n");
  663.                                 printf("请输入你的决定:");
  664.                                 scanf("%d",&a);
  665.                                 switch(a)
  666.                                 {
  667.                                         case 1:return;
  668.                                         case 2:ch=1;break;
  669.                                         case 3:ch=2;break;
  670.                                         case 4:ch=4;break;
  671.                                 }          
  672.                         case 4:printf("该次考试共有%d个人参加,其中平均分最高为%d,最低分为%d\n",count,avermark_max,avermark_min);
  673.                                   draw(avermark_A,'A');
  674.                                    draw(avermark_B,'B');
  675.                                    draw(avermark_C,'C');
  676.                                    draw(avermark_D,'D');
  677.                                    draw(avermark_E,'E');
  678.                                    printf("是否想知道其他的成绩分布\n");
  679.                                 printf("1.否\n");
  680.                                 printf("2.C语言程序设计成绩分布\n");
  681.                                 printf("3.英语成绩分布\n");
  682.                                 printf("4.高数成绩分布\n");
  683.                                 printf("请输入你的决定:");
  684.                                 scanf("%d",&a);
  685.                                 switch(a)
  686.                                 {
  687.                                         case 1:return;
  688.                                         case 2:ch=3;break;
  689.                                         case 3:ch=2;break;
  690.                                         case 4:ch=1;break;
  691.                                 }
  692.                         case 5:return;                           
  693.                 }
  694.         }
  695. }


  696. void menu()
  697. {
  698.         printf("************************\n");
  699.         printf("1.添加学生信息\n");
  700.         printf("2.查看成绩表\n");
  701.         printf("3.查询成绩信息\n");
  702.         printf("4.删除学生信息\n");
  703.         printf("5.修改学生信息\n");
  704.         printf("6.保存学生信息\n");
  705.         printf("7.调用学生信息\n");
  706.         printf("8.获取学生成绩分布信息\n");
  707.         printf("9.操作指南\n");
  708.         printf("0.退出程序\n");
  709.         printf("*************************\n");
  710. }

  711. void menu2()
  712. {
  713.         printf("************************\n");
  714.                 printf("1.查看1班的成绩\n");
  715.                 printf("2.查看2班的成绩\n");
  716.                 printf("3.退出该程序\n");
  717.                 printf("************************\n");
  718. }

  719. int main()
  720. {
  721.           char ch;
  722.           struct Line *head=NULL;
  723.           char sh[20];
  724.           char a[20];
  725.           char identity[20];
  726.           int n,b;
  727.           struct Line *student,*temp;
  728.           char Teacher1[]="teacher1";
  729.           char Teacher2[]="teacher2";
  730.           char Student[]="student";
  731.           char Master[]="master";
  732.           char *input;
  733.         heart();
  734.         printf("欢迎使用学生成绩管理系统 \n");
  735.         printf("welcome to student manage system\n");
  736.         printf("====power by swaggy boi!!!====\n");
  737.         system("pause");
  738.         printf("请输入你的身份:");
  739.         scanf("%s", identity);
  740.         if(!strcmp(identity,Teacher1))
  741.         {
  742.                        
  743.                 while(1)
  744.                         {
  745.                                 menu();
  746.                                 printf("选哪个程序xd:");
  747.                                 scanf("%d",&ch);
  748.                                 getchar();
  749.                                 switch(ch)
  750.                                 {
  751.                                         case 1: addStudent(&head);
  752.                                                     char ch;
  753.                                                
  754.                                                         while(1)
  755.                                                         {
  756.                                                                 printf("学生信息录入成功!!!\n");
  757.                                                                 printf("是否继续输入(Y/N):");
  758.                                                                 ch=getchar();
  759.                                                                 getchar();
  760.                                                                 if(ch=='Y'||ch=='y')
  761.                                                                 {
  762.                                                                         addStudent(&head);                                                       
  763.                                                                 }
  764.                                                                 else if(ch=='N'||ch=='n')
  765.                                                                 {
  766.                                                                         break ;
  767.                                                                 }
  768.                                                         }
  769.                                                         break;
  770.                                         case 2:printMark(head);break;
  771.                                         case 3:temp=searchStudent(head);
  772.                                                         if(temp==NULL)
  773.                                                         {
  774.                                                                 printStudent(temp);
  775.                                                                
  776.                                                         }
  777.                                                         if(temp==0)
  778.                                                         {
  779.                                                                 continue;
  780.                                                         }
  781.                                                    printStudent(temp);
  782.                                                    system("pause");
  783.                                                    break;       
  784.                                         case 4:delStudent(&head);break;
  785.                                         case 5: changeStudent(&head);
  786.                                         case 6:saveStudent(head);break;
  787.                                         case 7: Openfile(&head);
  788.                                                        
  789.                                                    saveStudent(head);       
  790.                                                    break;
  791.                                         case 8:getStudent_mark(head);break;
  792.                                         case 9:printf("1.本程序可以帮助班主任统计学生的成绩\n");
  793.                                                    printf("2.可以快速排序学生的成绩\n");
  794.                                                    printf("3.如果想获取之前编写的信息可以使用“调用学生信息”\n");
  795.                                                    printf("4.使用“获取学生成绩分布”可以使学生的成绩分布一目了然\n");
  796.                                                    printf("5.A成绩表示成绩在90~100,B成绩表示成绩在80~89,C成绩表示成绩在70~79,D成绩表示成绩在60~69,E成绩表示成绩在60以下\n");
  797.                                                    printf("===希望大家用的开心!===\n");
  798.                                                    break;          
  799.                                         case 0:remove("score.txt");
  800.                                                    saveStudent(head);
  801.                                                    exit(1);       
  802.                                 }       
  803.                         }
  804.         }
  805.         else if(!strcmp(identity,Teacher2))
  806.         {
  807.                         while(1)
  808.                         {
  809.                                 menu();
  810.                                 printf("选哪个程序xd:");
  811.                                 scanf("%d",&ch);
  812.                                 getchar();
  813.                                 switch(ch)
  814.                                 {
  815.                                         case 1: addStudent(&head);
  816.                                                     char ch;
  817.                                                
  818.                                                         while(1)
  819.                                                         {
  820.                                                                 printf("学生信息录入成功!!!\n");
  821.                                                                 printf("是否继续输入(Y/N):");
  822.                                                                 ch=getchar();
  823.                                                                 getchar();
  824.                                                                 if(ch=='Y'||ch=='y')
  825.                                                                 {
  826.                                                                         addStudent(&head);                                                       
  827.                                                                 }
  828.                                                                 else if(ch=='N'||ch=='n')
  829.                                                                 {
  830.                                                                         break ;
  831.                                                                 }
  832.                                                         }
  833.                                                         break;
  834.                                         case 2:printMark(head);break;
  835.                                         case 3:temp=searchStudent(head);
  836.                                                         if(temp==NULL)
  837.                                                         {
  838.                                                                 printStudent(temp);
  839.                                                                
  840.                                                         }
  841.                                                         if(temp==0)
  842.                                                         {
  843.                                                                 continue;
  844.                                                         }
  845.                                                    printStudent(temp);
  846.                                                    system("pause");
  847.                                                    break;       
  848.                                         case 4:delStudent(&head);break;
  849.                                         case 5:changeStudent(&head);
  850.                                         case 6:saveStudent2(head);break;
  851.                                         case 7:Openfile2(&head);
  852.                                                    saveStudent2(head);
  853.                                                    break;
  854.                                         case 8:getStudent_mark(head);break;
  855.                                         case 9:printf("1.本程序可以帮助班主任统计学生的成绩\n");
  856.                                                    printf("2.可以快速排序学生的成绩\n");
  857.                                                    printf("3.如果想获取之前编写的信息可以使用“调用学生信息”\n");
  858.                                                    printf("4.使用“获取学生成绩分布”可以使学生的成绩分布一目了然\n");
  859.                                                    printf("5.A成绩表示成绩在90~100,B成绩表示成绩在80~89,C成绩表示成绩在70~79,D成绩表示成绩在60~69,E成绩表示成绩在60以下\n");
  860.                                                    printf("===希望大家用的开心!===\n");
  861.                                                    break;          
  862.                                         case 0:saveStudent2(head);
  863.                                                    exit(1);       
  864.                                 }       
  865.                         }
  866.         }
  867.         else if(!strcmp(identity,Student))
  868.         {
  869.                 printf("请输入你的名字:");
  870.                 scanf("%s",a);
  871.                 do
  872.                 {
  873.                         printf("请输入你的班级:");
  874.                         scanf("%d",&n);       
  875.                 }while(n>2||n<0);
  876.                 printf("请稍等.....\n");
  877.                 if(n==1)
  878.                 {
  879.                         Openfile(&head);
  880.                         student=head;
  881.                         if(!strcmp(student->student.name,a))
  882.                         {
  883.                                 printStudent(student);
  884.                                 saveStudent(head);
  885.                                 exit(1);
  886.                         }
  887.                         while(student!=NULL&&!strcmp(student->student.name,a))
  888.                         {
  889.                                 student=student->next;
  890.                         }
  891.                         if(student!=NULL)
  892.                         {
  893.                                 printStudent(student);
  894.                                 saveStudent(head);
  895.                                 exit(1);       
  896.                         }
  897.                         else
  898.                         {       
  899.                                 saveStudent(head);
  900.                         }
  901.                 }
  902.                 else if(n==2)
  903.                 {
  904.                         Openfile2(&head);
  905.                         student=head;
  906.                         if(!strcmp(student->student.name,a))
  907.                         {
  908.                                 printStudent(student);
  909.                                 saveStudent2(head);
  910.                                 exit(1);
  911.                         }
  912.                         while(student!=NULL&&!strcmp(student->student.name,a))
  913.                         {
  914.                                 student=student->next;
  915.                         }
  916.                         if(student!=NULL)
  917.                         {
  918.                                 printStudent(student);
  919.                                 saveStudent2(head);
  920.                                 exit(1);       
  921.                         }
  922.                         else
  923.                         {       
  924.                                 saveStudent2(head);
  925.                         }               
  926.                 }
  927.                 printf("老师还未录入你的成绩,再等等吧!\n");       
  928.         }
  929.         else if(!strcmp(identity,Master))
  930.         {
  931.                 while(1)
  932.                 {       
  933.                         menu2();
  934.                         scanf("%d",&b);
  935.                         switch(b)
  936.                         {
  937.                                 case 1: Openfile(&head);
  938.                                             getStudent_mark(head);
  939.                                             saveStudent(head);
  940.                                             break;
  941.                                 case 2: Openfile2(&head);
  942.                                            getStudent_mark(head);
  943.                                            saveStudent2(head);
  944.                                            break;
  945.                                 case 3: return 0;                     
  946.                         }
  947.                 }
  948.         }
  949. return 0;
  950. }
复制代码
最佳答案
2021-11-4 10:12:00
本帖最后由 jackz007 于 2021-11-4 12:28 编辑

        试试这个代码:
  1. void Openfile(struct Line ** head)
  2. {
  3.         FILE * fp                                                              ;
  4.         const char fn[] = "student.dat"                                        ;
  5.         struct Line * student , * previous                                     ;
  6.         size_t k , m                                                           ;
  7.         if(fp = fopen(fn , "rb")) {
  8.                 fseek(fp , 0 , SEEK_END)                                       ;
  9.                 m = ftell(fp) / sizeof(struct Line)                            ;
  10.                 fseek(fp , 0 , SEEK_SET)                                       ;
  11.                 for(previous = NULL , k = 0 ; k < m ; k ++) {
  12.                         if(student = (struct Line *) malloc(sizeof(struct Line))) {
  13.                                 fread(student , sizeof(struct Line ) , 1 , fp) ;
  14.                                 student -> next = NULL                         ;
  15.                                 if(! previous) * head = student                ;
  16.                                 else previous -> next = student                ;
  17.                                 previous = student                             ;
  18.                         } else {
  19.                                 fprintf(stderr , "内存分配失败!\n")           ;
  20.                                 break                                          ;
  21.                         }
  22.                 }
  23.                 fclose(fp)                                                     ;
  24.         } else {
  25.                 fprintf(stderr , "文件:[%s] 不存在\n" , fn)                   ;
  26.         }
  27. }

  28. void Savefile(struct Line * head)
  29. {
  30.         FILE * fp
  31.         const char fn[] = "student.dat"                                        ;
  32.         struct Line * student                                                  ;
  33.         if(fp = fopen(fn , "wb")) {
  34.                 for(student = head ; student ; student = student -> next) {
  35.                         fwrite(student , sizeof(struct Line) , 1 , fp)         ;
  36.                 }
  37.                 fclose(fp)                                                     ;
  38.         } else {
  39.                 fprintf(stderr , "文件:[%s] 创建失败\n" , fn)                 ;
  40.         }        
  41. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 09:34:14 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 09:34:44 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 09:35:23 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 09:36:34 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 09:37:04 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 09:38:51 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 10:12:00 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-11-4 12:28 编辑

        试试这个代码:
  1. void Openfile(struct Line ** head)
  2. {
  3.         FILE * fp                                                              ;
  4.         const char fn[] = "student.dat"                                        ;
  5.         struct Line * student , * previous                                     ;
  6.         size_t k , m                                                           ;
  7.         if(fp = fopen(fn , "rb")) {
  8.                 fseek(fp , 0 , SEEK_END)                                       ;
  9.                 m = ftell(fp) / sizeof(struct Line)                            ;
  10.                 fseek(fp , 0 , SEEK_SET)                                       ;
  11.                 for(previous = NULL , k = 0 ; k < m ; k ++) {
  12.                         if(student = (struct Line *) malloc(sizeof(struct Line))) {
  13.                                 fread(student , sizeof(struct Line ) , 1 , fp) ;
  14.                                 student -> next = NULL                         ;
  15.                                 if(! previous) * head = student                ;
  16.                                 else previous -> next = student                ;
  17.                                 previous = student                             ;
  18.                         } else {
  19.                                 fprintf(stderr , "内存分配失败!\n")           ;
  20.                                 break                                          ;
  21.                         }
  22.                 }
  23.                 fclose(fp)                                                     ;
  24.         } else {
  25.                 fprintf(stderr , "文件:[%s] 不存在\n" , fn)                   ;
  26.         }
  27. }

  28. void Savefile(struct Line * head)
  29. {
  30.         FILE * fp
  31.         const char fn[] = "student.dat"                                        ;
  32.         struct Line * student                                                  ;
  33.         if(fp = fopen(fn , "wb")) {
  34.                 for(student = head ; student ; student = student -> next) {
  35.                         fwrite(student , sizeof(struct Line) , 1 , fp)         ;
  36.                 }
  37.                 fclose(fp)                                                     ;
  38.         } else {
  39.                 fprintf(stderr , "文件:[%s] 创建失败\n" , fn)                 ;
  40.         }        
  41. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-4 10:27:20 | 显示全部楼层

这个在每次调用一次就会在打印成绩表的时候多打印一行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-4 11:12:42 | 显示全部楼层
superswagy2002 发表于 2021-11-4 10:27
这个在每次调用一次就会在打印成绩表的时候多打印一行

    8 楼的代码已经修改,请再次测试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-4 13:52:56 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 13:54:17 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 13:54:56 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-4 14:14:46 | 显示全部楼层

回帖奖励 +2 鱼币

进来学习

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-4 20:49:03 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-4 20:57:39 | 显示全部楼层
jackz007 发表于 2021-11-4 11:12
8 楼的代码已经修改,请再次测试。

感谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-5 21:10:51 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-5 21:19:09 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-25 16:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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