鱼C论坛

 找回密码
 立即注册
查看: 1526|回复: 2

[已解决]学生管理系统删除学生信息问题

[复制链接]
发表于 2021-11-2 16:27:44 | 显示全部楼层 |阅读模式
15鱼币
我这个改变头节点的办法,使得运行完改变了头节点后,再运行成绩表导致系统崩溃,有没有什么好的办法解决。(删除在第258-308)
  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. void saveStudent(struct Line *head)
  22. {
  23.         FILE *fp;
  24.         struct Line *student;
  25.         fp=fopen("score.txt","w+");
  26.         if(fp==NULL)
  27.         {
  28.                 printf("打开文件失败!!!\n");
  29.                 return;
  30.         }
  31.         student=head;
  32.         while(student!=NULL)
  33.         {
  34.                 fwrite(&student->student,sizeof(struct Student),1,fp);
  35.                 student=student->next;
  36.         }
  37.         fclose(fp);
  38.         printf("数据保存成功!!!\n");
  39. }

  40. void getInput(struct Line *student,struct Line *head)
  41. {
  42.         int count=0;
  43.         struct Line *temp;
  44.         temp=head;
  45.         printf("该学生的名字为:");
  46.         scanf("%s",student->student.name);
  47.         getchar();
  48.         while(student->student.sex!='M'&&student->student.sex!='F'&&student->student.sex!='f'&&student->student.sex!='m')
  49.         {       
  50.                 printf("该学生的性别为(M/F):");
  51.                 scanf("%c",&student->student.sex);
  52.                 getchar();
  53.         }
  54.         printf("该学生的学号为:");
  55.         scanf("%d",&student->student.num);
  56.         getchar();
  57.         while(1)
  58.         {
  59.                 count=0;
  60.                 while(student->student.num<0||student->student.num>999)
  61.                 {       
  62.                         if(count>0)
  63.                         {
  64.                                 printf("该学生的学号为:");
  65.                                 scanf("%d",&student->student.num);
  66.                                 count++;
  67.                         }
  68.                         else if(count==0)
  69.                         {       
  70.                         count++;
  71.                         }
  72.                 }
  73.                 while((temp!=NULL)&&(temp->student.num!=student->student.num))
  74.                 {
  75.                         temp=temp->next;
  76.                 }
  77.                 if(temp==NULL)
  78.                 {
  79.                         break;
  80.                 }
  81.                 else
  82.                 {
  83.                         printf("该学生的学号重复,请重新输入!\n");
  84.                         printf("该学生的学号为:");
  85.                         scanf("%d",&student->student.num);
  86.                 }
  87.         }
  88.         printf("该学生的英语成绩为:");
  89.         scanf("%d",&student->student.English_mark);
  90.         getchar();
  91.         count=0;
  92.         while(student->student.English_mark<0||student->student.English_mark>100)
  93.         {
  94.                 if(count==0)
  95.                 {
  96.                         count++;
  97.                 }
  98.                 else
  99.                 {
  100.                         printf("该学生的英语成绩为:");
  101.                         scanf("%d",&student->student.English_mark);
  102.                         getchar();
  103.                 }
  104.         }
  105.         printf("该学生的高数成绩为:");
  106.         scanf("%d",&student->student.Math_mark);
  107.         getchar();
  108.         count=0;
  109.         while(student->student.Math_mark<0||student->student.Math_mark>100)
  110.         {
  111.                 if(count==0)
  112.                 {
  113.                         count++;
  114.                 }
  115.                 else
  116.                 {
  117.                         printf("该学生的高数成绩为:");
  118.                         scanf("%d",&student->student.Math_mark);
  119.                         getchar();
  120.                 }
  121.         }
  122.         printf("该学生C语言程序设计的成绩为:");
  123.         scanf("%d",&student->student.c_mark);
  124.         getchar();
  125.         count=0;
  126.         while(student->student.c_mark<0||student->student.c_mark>100)
  127.         {
  128.                 if(count==0)
  129.                 {
  130.                         count++;
  131.                 }
  132.                 else
  133.                 {
  134.                         printf("该学生的C语言程序设计成绩为:");
  135.                         scanf("%d",&student->student.c_mark);
  136.                         getchar();
  137.                 }
  138.         }
  139.         student->student.avermark=(student->student.c_mark+student->student.English_mark+student->student.Math_mark)/3;
  140.         student->student.sum=student->student.c_mark+student->student.English_mark+student->student.Math_mark;
  141. }
  142. void addStudent(struct Line **head)
  143. {
  144.         struct Line *student,*temp,*previous,*current;
  145.         student=(struct Line*)malloc(sizeof(struct Line));
  146.         if(student==NULL)
  147.         {
  148.                 printf("内存分配失败!!");
  149.                 exit(1);
  150.         }
  151.         getInput(student,*head);
  152.        
  153.         if(*head==NULL)
  154.         {
  155.                 *head=student;
  156.                 student->next=NULL;
  157.         }
  158.         else
  159.         {
  160.                 current=*head;
  161.                 previous=NULL;
  162.                 while(current!=NULL&&current->student.avermark>student->student.avermark)
  163.                 {
  164.                         previous=current;
  165.                         current=current->next;
  166.                 }
  167.                 if(current==NULL)
  168.                 {
  169.                         previous->next=student;
  170.                         student->next=NULL;
  171.                 }
  172.                 else if(current!=NULL&&current!=*head)
  173.                 {
  174.                         student->next=current;
  175.                         previous->next=student;
  176.                 }
  177.                 else if(current==*head)
  178.                 {
  179.                         student->next=*head;
  180.                         *head=student;
  181.                 }
  182.                
  183.         }

  184. }

  185. struct Line *searchStudent(struct Line *head)
  186. {
  187.         char input[20];
  188.         int a;
  189.         int ch;
  190.         struct Line *student;
  191.         student=head;
  192.         printf("*************************\n");
  193.         printf("1.按照姓名查询\n");
  194.         printf("2.按照学号查询\n");
  195.         printf("3.返回菜单\n");
  196.         scanf("%d",&ch);
  197.         switch(ch)
  198.         {
  199.                 case 1:printf("请输入该学生的姓名:");
  200.                            scanf("%s",input);
  201.                            break;
  202.                 case 2:printf("请输入该学生的学号:");
  203.                            scanf("%d",&a);
  204.                            break;       
  205.                 case 3:return 0;             
  206.         }
  207.         while(student!=NULL)
  208.         {
  209.                 if(!strcmp(student->student.name,input)||student->student.num==a)
  210.                 {       
  211.                         break;       
  212.                 }
  213.                 student=student->next;
  214.         }
  215.         return student;
  216. }


  217. void printStudent(struct Line *student)
  218. {
  219.         if(student==NULL)
  220.         {
  221.                 printf("很抱歉查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n");
  222.                 return;
  223.         }
  224.         printf("姓名:%s\n",student->student.name);
  225.         printf("学号:%d\n",student->student.num);
  226.         printf("高数成绩:%d\n",student->student.Math_mark);
  227.         printf("英语成绩:%d\n",student->student.English_mark);
  228.         printf("c语言程序设计成绩:%d\n",student->student.c_mark);
  229.         printf("三科平均分为:%d\n",student->student.avermark);
  230. }

  231. void printMark(struct Line *head)
  232. {
  233.         struct Line *student;
  234.         int count=1;
  235.         printf("====================================\n");
  236.         printf("\t学生成绩表\t\n");
  237.         printf("\t姓名\t性别\t学号\t英语成绩\t高数成绩\tC语言程序设计成绩\t总分\t平均成绩\t排名\t\n");
  238.         student=head;
  239.         while(student!=NULL)
  240.         {
  241.                 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);
  242.                 student=student->next;
  243.                 count++;
  244.         }
  245.         printf("====================================\n");
  246.         system("pause");
  247. }

  248. void delStudent(struct Line *head)
  249. {
  250.         struct Line *previous,*current,*p1;
  251.         previous=NULL;
  252.         current=head;
  253.         char input[20];
  254.         char ch;
  255.         printf("=====================================\n");
  256.         printf("1.按照名字查找需要删除的学生\n");
  257.         printf("2.按照学号查找需要删除的学生\n");
  258.         printf("3.返回菜单\n");
  259.         printf("请输入你的选择:");
  260.         scanf("%d",&ch);
  261.         switch(ch)
  262.         {
  263.                 case 1:printf("请输入该学生的名字:");
  264.                            scanf("%s",input);
  265.                            if(!strcmp(head->student.name,input))
  266.                            {
  267.                                            p1=head;
  268.                                            head=head->next;
  269.                                            free(p1);
  270.                                            return;
  271.                            }
  272.                            if(!strcmp(current->student.name,input)||current!=NULL)
  273.                            {
  274.                                               previous=current;
  275.                                            current=current->next;
  276.                            }
  277.                            if(current==NULL)
  278.                            {
  279.                                            printf("查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n");
  280.                                            return;
  281.                            }
  282.                            else
  283.                            {
  284.                                            if(previous==NULL)
  285.                                            {
  286.                                                    head=current->next;
  287.                                         }
  288.                                         else
  289.                                         {
  290.                                                 previous->next=current->next;
  291.                                         }
  292.                                         free(current);
  293.                            }
  294.                           
  295.                           
  296.         }
  297.        
  298. }

  299. void heart()
  300. {printf("      ****       ****\n");
  301.    printf("   *********   *********\n");
  302.    printf("************* *************\n");
  303.    int i,j;
  304.    for(i=0;i<3;i++)
  305.    {
  306.      for(j=0;j<29;j++)
  307.      {
  308.       printf("*");
  309.     }
  310.      printf("\n");
  311.   }
  312.   for(i=0;i<7;i++)
  313.    {
  314.      for(j=0;j<2*(i+1)-1;j++)
  315.      {
  316.       printf(" ");
  317.     }
  318.      for(j=0;j<27-i*4;j++)
  319.      {
  320.       printf("*");
  321.     }
  322.      printf("\n");
  323.     }
  324.   for(i=0;i<14;i++)
  325.   {
  326.     printf(" ");
  327.   }
  328.    printf("*\n") ;
  329. }

  330. void menu()
  331. {
  332.         printf("************************\n");
  333.         printf("1.添加学生信息\n");
  334.         printf("2.查看成绩表\n");
  335.         printf("3.查询成绩信息\n");
  336.         printf("4.删除学生信息\n");
  337.         printf("5.修改学生信息\n");
  338.         printf("6.保存学生信息\n");
  339.         printf("0.退出程序\n");
  340.         printf("*************************\n");
  341. }
  342. int main()
  343. {
  344.           char ch;
  345.           struct Line *head=NULL;
  346.           char sh[20];
  347.           struct Line *student,*temp;
  348.           char *input;
  349.         heart();
  350.         printf("欢迎使用学生成绩管理系统 \n");
  351.         printf("welcome to student manage system\n");
  352.         printf("====power by swaggy boi!!!====\n");
  353.         while(1)
  354.         {
  355.                 menu();
  356.                 printf("选哪个程序xd:");
  357.                 scanf("%d",&ch);
  358.                 getchar();
  359.                 switch(ch)
  360.                 {
  361.                         case 1: addStudent(&head);
  362.                                     char ch;
  363.                                
  364.                                         while(1)
  365.                                         {
  366.                                                 printf("学生信息录入成功!!!\n");
  367.                                                 printf("是否继续输入(Y/N):");
  368.                                                 ch=getchar();
  369.                                                 getchar();
  370.                                                 if(ch=='Y'||ch=='y')
  371.                                                 {
  372.                                                         addStudent(&head);                                                       
  373.                                                 }
  374.                                                 else if(ch=='N'||ch=='n')
  375.                                                 {
  376.                                                         break ;
  377.                                                 }
  378.                                         }
  379.                                         break;
  380.                         case 2:printMark(head);break;
  381.                         case 3:temp=searchStudent(head);
  382.                                         if(temp==NULL)
  383.                                         {
  384.                                                 printStudent(temp);
  385.                                                
  386.                                         }
  387.                                         if(temp==0)
  388.                                         {
  389.                                                 continue;
  390.                                         }
  391.                                    printStudent(temp);
  392.                                    system("pause");
  393.                                    break;       
  394.                         case 4:delStudent(head);break;
  395.                         case 5:/*changeStudent(head)*/;
  396.                         case 6:saveStudent(head);break;
  397.                         case 0:exit(1);       
  398.                 }       
  399.         }  
  400. return 0;
  401. }
复制代码
最佳答案
2021-11-2 16:27:45
本帖最后由 jhq999 于 2021-11-2 17:03 编辑
  1. void delStudent(struct Line **head)//因为形参改变,实参不会跟着改变,所以要换成指针的指针,可惜c没有引用,其实引用最简单
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-2 16:27:45 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-11-2 17:03 编辑
  1. void delStudent(struct Line **head)//因为形参改变,实参不会跟着改变,所以要换成指针的指针,可惜c没有引用,其实引用最简单
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-2 20:31:03 | 显示全部楼层
楼主,有完整的学生管理系统代码吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 18:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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