鱼C论坛

 找回密码
 立即注册
查看: 1889|回复: 4

[已解决]学生成绩管理系统中的学号查重问题

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

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

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

x
我这个程序在学号查重时发生错误,只要打正常值(0~999)就会出错。学号查重在(40~73行)
  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. struct Line *head;

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

  166. }

  167. /*struct Line *searchStudent(struct Line *head,char input)
  168. {
  169.         struct Line *student;
  170.         student=head;
  171.         while(student!=NULL)
  172.         {
  173.                 if(!strcmp(student->student.name,input)||!strcmp(student->student.c_mark,input)||!strcmp(student->student.English_mark,input)||!strcmp(student->student.Math_mark,input)||!strcmp(student->student.num,input))
  174.                 {
  175.                         break;
  176.                 }
  177.                 student=student->next;
  178.         }
  179.         return student;
  180. }*/


  181. void printStudent(struct Line *head)
  182. {
  183.         printf("姓名:%s\n",head->student.name);
  184.         printf("学号:%d\n",head->student.num);
  185.         printf("高数成绩:%d\n",head->student.Math_mark);
  186.         printf("英语成绩:%d\n",head->student.English_mark);
  187.         printf("c语言程序设计成绩:%d",head->student.c_mark);
  188.         printf("三科平均分为:%d",head->student.avermark);
  189. }

  190. void printMark(struct Line *head)
  191. {
  192.         struct Line *student;
  193.         int count=1;
  194.         printf("====================================\n");
  195.         printf("\t学生成绩表\t\n");
  196.         printf("\t姓名\t性别\t学号\t英语成绩\t高数成绩\tC语言程序设计成绩\t总分\t平均成绩\t排名\t\n");
  197.         student=head;
  198.         while(student!=NULL)
  199.         {
  200.                 printf("\t%s\t%c\t%d\t%d\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);
  201.                 student=student->next;
  202.                 count++;
  203.         }
  204.         printf("====================================\n");
  205.         system("pause");
  206. }


  207. void heart()
  208. {printf("      ****       ****\n");
  209.    printf("   *********   *********\n");
  210.    printf("************* *************\n");
  211.    int i,j;
  212.    for(i=0;i<3;i++)
  213.    {
  214.      for(j=0;j<29;j++)
  215.      {
  216.       printf("*");
  217.     }
  218.      printf("\n");
  219.   }
  220.   for(i=0;i<7;i++)
  221.    {
  222.      for(j=0;j<2*(i+1)-1;j++)
  223.      {
  224.       printf(" ");
  225.     }
  226.      for(j=0;j<27-i*4;j++)
  227.      {
  228.       printf("*");
  229.     }
  230.      printf("\n");
  231.     }
  232.   for(i=0;i<14;i++)
  233.   {
  234.     printf(" ");
  235.   }
  236.    printf("*\n") ;
  237. }

  238. void menu()
  239. {
  240.         printf("************************\n");
  241.         printf("1.添加学生信息\n");
  242.         printf("2.查看成绩表\n");
  243.         printf("3.查询成绩信息\n");
  244.         printf("4.删除学生信息\n");
  245.         printf("5.修改学生信息\n");
  246.         printf("0.退出程序\n");
  247.         printf("*************************\n");
  248. }
  249. int main()
  250. {
  251.           char ch;
  252.           struct Line *head=NULL;
  253.           char sh[20];
  254.           struct Line *student;
  255.         heart();
  256.         printf("欢迎使用学生成绩管理系统 \n");
  257.         printf("welcome to student manage system\n");
  258.         printf("====power by swaggy boi!!!====\n");
  259.         while(1)
  260.         {
  261.                 menu();
  262.                 printf("选哪个程序xd:");
  263.                 scanf("%d",&ch);
  264.                 getchar();
  265.                 switch(ch)
  266.                 {
  267.                         case 1: addStudent(&head);
  268.                                     char ch;
  269.                                
  270.                                         while(1)
  271.                                         {
  272.                                                 printf("学生信息录入成功!!!\n");
  273.                                                 printf("是否继续输入(Y/N):");
  274.                                                 ch=getchar();
  275.                                                 getchar();
  276.                                                 if(ch=='Y'||ch=='y')
  277.                                                 {
  278.                                                         addStudent(&head);                                                       
  279.                                                 }
  280.                                                 else if(ch=='N'||ch=='n')
  281.                                                 {
  282.                                                         break ;
  283.                                                 }
  284.                                         }
  285.                                         break;
  286.                         case 2:printMark(head);break;
  287.                         case 3:;                          
  288.                         case 4:;
  289.                         case 5:;
  290.                         case 0:;       
  291.                 }       
  292.         }  
  293. return 0;
  294. }
复制代码
最佳答案
2021-11-1 16:12:59
本帖最后由 jhq999 于 2021-11-1 16:14 编辑
  1. while((temp!=NULL)&&(temp->student.num!=student->student.num))\\把||换成&&
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-1 16:12:59 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +10 鱼币

本帖最后由 jhq999 于 2021-11-1 16:14 编辑
  1. while((temp!=NULL)&&(temp->student.num!=student->student.num))\\把||换成&&
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-1 16:13:28 | 显示全部楼层
while((temp!=NULL)&&(temp->student.num!=student->student.num))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-1 16:24:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-2 16:17:52 | 显示全部楼层
学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-15 17:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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