小宇 2023-03-07 11:43 采纳率: 50%
浏览 61
已结题

学生成绩管理系统,入录学生信息之后,进行学号或姓名查询时,输入完1或2,并未跳转到“请输入学生学号或姓名”,但是直接输入姓名或学号可以得到学生信息,如何改错呢?


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student{   //每个学生对应一个结构体 
 char ID[20];//学号 
 char Name[10];//姓名 
 float Mark1;//语文成绩 
 float Mark2;//数学成绩 
 float Mark3;//英语成绩 
 float Mark4;//计算机成绩 
 float All; //总分 
 float Average;//平均成绩 
}students[1000];
int num=0; //计数器
void Copy(struct Student *arr,int i,int j)
{
 strcpy(arr[i].ID,arr[j].ID);
 strcpy(arr[i].Name,arr[j].Name);
 arr[i].Mark1 = arr[j].Mark1;
 arr[i].Mark2 = arr[j].Mark2;
 arr[i].Mark3 = arr[j].Mark3;
 arr[i].Mark4 = arr[j].Mark4;
 arr[i].All = arr[j].All;
 arr[i].Average = arr[j].Average;
}
int Student_SearchByName(char name[])//通过姓名来检索学生
{
     int i;
     for (i=0;i<num;i++)
     {
         if (strcmp(students[i].Name,name)==0)  //通过strcmp函数来对比学生姓名,找到返回在数组的位置 
         {
              return i;
       }
     }
     return -1;    //未找到返回 -1 
}
int Student_SearchByIndex(char id[])//通过学号来检索学生信息
{
     int i;
     for (i=0;i<num;i++)
     {
         if (strcmp(students[i].ID,id)==0)  //通过strcmp函数来对比学生id,找到返回位置 
         {
              return i;
         }  
     }
     return -1;   //未找到返回 -1 
}
void Student_DisplaySingle(int index)//输出表头
{
     printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","语文","数学","英语","计算机","总成绩","平均成绩");
     printf("-------------------------------------------------------------\n");
     printf("%10s%10s%8.2f%8.2f%8.2f%8.2f\n",students[index].ID,students[index].Name,
              students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Mark4);
}
void inputt()//利用循环录入学生信息
{
     while(1)
     {
         printf("请输入学号:");
         scanf("%s",&students[num].ID);
         getchar();
         printf("请输入姓名:");
         scanf("%s",&students[num].Name);
         getchar();
         printf("请输入成绩:");
         scanf("%f",&students[num].Mark1);
         getchar();
         printf("请输入成绩:");
         scanf("%f",&students[num].Mark2);
         getchar();
         printf("请输入成绩:");
         scanf("%f",&students[num].Mark3);
         getchar();
         printf("请输入成绩:");
         scanf("%f",&students[num].Mark4);   //依次输入各项数据 
         getchar();
         if(Student_SearchByIndex(students[num].ID) ==-1)//修改的位置000000 
         {
          num++;  //移向下一个位置 
   }
   else
   {
    printf("学号重复,输入数据无效 !!!\n");
   }
   
    printf("是否继续?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
void modify()//修改成绩
{
   while(1)
     {
         char id[20];
         int index;
         printf("请输入要修改的学生的学号:");
         scanf("%s",&id);
         getchar();
         index=Student_SearchByIndex(id);  //调用搜查id函数,根据其返回值确定位置 
         if (index==-1)
         {
              printf("学生不存在!\n");
         }
         else
         {
              printf("你要修改的学生信息为:\n");
              Student_DisplaySingle(index);
              printf("-- 请输入新值--\n");
              printf("请输入学号:");
              scanf("%s",&students[index].ID);
              getchar();
              printf("请输入姓名:");
              scanf("%s",&students[index].Name);
              getchar();
              printf("请输入语文成绩:");
              scanf("%f",&students[index].Mark1);
      getchar();
              printf("请输入数学成绩:");
              scanf("%f",&students[index].Mark2);
              getchar();
              printf("请输入英语成绩:");
              scanf("%f",&students[index].Mark3);
              getchar();
              printf("请输入计算机成绩:");
              scanf("%f",&students[index].Mark4);  //重新录入一套新的数据替代 
              getchar();
              students[index].All=students[index].Mark1+students[index].Mark2+students[index].Mark3+students[index].Mark4;
           students[index].Average=(students[index].Mark1+students[index].Mark2+students[index].Mark3+students[index].Mark4)/4;   
         }
         printf("是否继续?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
void deletee()//删除学生信息
{
   int i;
     while(1)
     {
         char id[20];
         int index;
         printf("请输入要删除的学生的学号:");
         scanf("%s",&id);
         getchar();
         index=Student_SearchByIndex(id);   //调用搜查id函数,根据其返回值确定位置 
         if (index==-1)
         {
              printf("学生不存在!\n");
         }
         else
         {
              printf("你要删除的学生信息为:\n");
              Student_DisplaySingle(index);
              printf("是否真的要删除?(y/n)");
              if (getchar()=='y')
              {
                   for (i=index;i<num-1;i++)
                   {
                       Copy(students,i,i+1);
                       //students[i]=students[i+1];    //把后边的对象都向前移动
                   }
                   num--;
              }
             getchar();
         }
         printf("是否继续?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
void display()//打印已录入的学生信息
{
  int a;
     printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","语文","数学","英语","计算机","总成绩","平均成绩");
     printf("-------------------------------------------------------------\n");
     for (a=0;a<num;a++)
     {
         printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",students[a].ID,students[a].Name,
              students[a].Mark1,students[a].Mark2,students[a].Mark3,students[a].Mark4,students[a].All,students[a].Average);
     }
}
void insert()//指定位置插入学生信息
{
  int a,b,c;
  printf("请输入你要插入的位置");
  scanf("%d",&a);
  if(a>num) {
  printf("输入的位置有误,请重新输入,当前共%d条数据\n",num); 
  scanf("%d",&a);}
 
      b=num-1;
      for(;b>=a-1;b--)
      {
       //strcpy(students[b+1].ID,students[b].ID);
       //strcpy(students[b+1].Name,students[b].Name);
       //students[b+1].Mark1=students[b].Mark1;
       //students[b+1].Mark2=students[b].Mark2;
       //students[b+1].Mark3=students[b].Mark3;
       //students[b+1].Mark4=students[b].Mark4;
       //students[b+1].All=students[b].All;
       //students[b+1].Average=students[b].Average;  
   Copy(students,b+1,b); //根据其输入的位置,将其及以后的数据向后移动一个位置 
       
   }
   num++;
         printf("请输入学号:");
         scanf("%s",&students[a-1].ID);
         getchar();
         printf("请输入姓名:");
         scanf("%s",&students[a-1].Name);
         getchar();
         printf("请输入语文成绩:");
         scanf("%f",&students[a-1].Mark1);
         getchar();
         printf("请输入数学成绩:");
         scanf("%f",&students[a-1].Mark2);
         getchar();
         printf("请输入英语成绩:");
         scanf("%f",&students[a-1].Mark3);
         getchar();
         printf("请输入计算机成绩:");
         scanf("%f",&students[a-1].Mark4);  //输入新数据 
         getchar();  
         students[a-1].All=students[a-1].Mark1+students[a-1].Mark2+students[a-1].Mark3+students[a-1].Mark4;
         students[a-1].Average=(students[a-1].Mark1+students[a-1].Mark2+students[a-1].Mark3+students[a-1].Mark4)/4;
 
}
void search()//查询学生信息
{
    int elect=0;
    printf("输入1姓名查询,输入2学号查询\n");
    scanf("%d\n",&elect);
    if(elect==1){
    
  while(1)
     {
         char name[20];
         int index;
         printf("请输入要查询的学生的姓名:");
         scanf("%s",&name);
         getchar();
         index=Student_SearchByName(name);   //调用搜查name函数,根据其返回值确定位置 
         if (index==-1)
         {
              printf("学生不存在!\n");
         }
         else
         {
              printf("你要查询的学生信息为:\n");
              Student_DisplaySingle(index);
         }
         printf("是否继续?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
}
    else
 {
  while(1)
     {
         char id[20];
         int index;
         printf("请输入要查询的学生的学号:");
         scanf("%s",&id);
         getchar();
         index=Student_SearchByIndex(id);   //调用搜查id函数,根据其返回值确定位置 
         if (index==-1)
         {
              printf("学生不存在!\n");
         }
         else
         {
              printf("你要查询的学生信息为:\n");
              Student_DisplaySingle(index);
         }
         printf("是否继续?(y/n)");
         if (getchar()=='n')
         {
              break;
         }
     }
 }
} 
void sort()//根据平均分排序
{
  int i,j;
  //struct students tmp;
     for (i=0;i<num;i++)
     {
      students[i].Average=(students[i].Mark1+students[i].Mark2+students[i].Mark3+students[i].Mark4)/4;
     }
     for (i=0;i<num;i++)
     {
         for (j=1;j<num-i;j++)
         {
              if (students[j-1].Average<students[j].Average)
              {
                  
       Copy(students,num,j-1);
       Copy(students,j-1,j);
       Copy(students,j,num); 
       //tmp=students[j-1];
                   //students[j-1]=students[j];
                   //students[j]=tmp;      //冒泡排序 
            }  
         }
     }
     int a;
     printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","语文","数学","英语","计算机","总成绩","平均成绩");
     printf("-------------------------------------------------------------\n");
     for (a=0;a<num;a++)
     {
         printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",students[a].ID,students[a].Name,
              students[a].Mark1,students[a].Mark2,students[a].Mark3,students[a].Mark4,students[a].All,students[a].Average);
     }
} 
 
 
void SearchLow()//搜索不及格的并输出 
{
 int a;
 printf("           语文不及格的有%10s%10s%8s\n","学号","姓名","语文"); 
 for(a=0;a<num;a++)
 { 
  if(students[a].Mark1<60)
  printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark1);    //从头搜索到尾,若小于60就输出 
  } 
  
 printf("           数学不及格的有%10s%10s%8s\n","学号","姓名","数学"); 
 for(a=0;a<num;a++)
 { 
  if(students[a].Mark2<60)
  printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark2);
  } 
  
  printf("           英语不及格的有%10s%10s%8s\n","学号","姓名","英语");
 for(a=0;a<num;a++)
 { 
  if(students[a].Mark3<60)
  printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark3);
  } 
  
  printf("           计算机不及格的有%10s%10s%8s\n","学号","姓名","计算机");
 for(a=0;a<num;a++)
 { 
  if(students[a].Mark4<60)
  printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark4);
  } 
  system("pause");
}
 
void SearchHigh()//搜索成绩最高者输出 
{
 int a;
 int max ;
 printf("           语文最高分为%10s%10s%8s\n","学号","姓名","语文"); 
 max=students[0].Mark1;
 for(a=1;a<num;a++)
 { 
  if(students[a].Mark1>max)
  max=students[a].Mark1;
 }
 for(a=0;a<num;a++)
 { 
  if(max==students[a].Mark1)
  printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark1);
 }
 
 printf("           数学最高分为%10s%10s%8s\n","学号","姓名","数学"); 
 max=students[0].Mark2;
 for(a=1;a<num;a++)
 { 
  if(students[a].Mark2>max)
  max=students[a].Mark2;
 }
 for(a=0;a<num;a++)
 { 
  if(max==students[a].Mark2)
  printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark2);
 }
 
 printf("           英语最高分为%10s%10s%8s\n","学号","姓名","英语"); 
 max=students[0].Mark3;
 for(a=1;a<num;a++)
 { 
  if(students[a].Mark3>max)
  max=students[a].Mark3;
 }
 for(a=0;a<num;a++)
 { 
  if(max==students[a].Mark3)
  printf("           %10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark3);
 }
 
 printf("           计算机最高分为%10s%10s%8s\n","学号","姓名","计算机"); 
 max=students[0].Mark4;
 for(a=1;a<num;a++)
 { 
  if(students[a].Mark4>max)
  max=students[a].Mark4;
 }
 for(a=0;a<num;a++)
 { 
  if(max==students[a].Mark4)
  printf("           %10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark4);
 }
  system("pause");
}
void Save()
{
 FILE*fp = fopen("temp.txt","w+");
 fprintf(fp,"%d\n",num);
 for(int i = 0 ; i< num ;i++)
 {
  fprintf(fp,"%s %s %f %f %f %f %f %f\n",students[i].ID,students[i].Name,students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Mark4,students[i].All,students[i].Average);
 }
 fclose(fp);
}
void Load()
{
 FILE*fp = fopen("temp.txt","r");
 fscanf(fp,"%d",&num);
 for(int i = 0 ; i< num ;i++)
 {
  fscanf(fp,"%s %s %f %f %f %f %f %f\n",students[i].ID,students[i].Name,&students[i].Mark1,&students[i].Mark2,&students[i].Mark3,&students[i].Mark4,&students[i].All,&students[i].Average);
 }
 fclose(fp);
} 
//主程序 
int main(){
 int i;
 while(1){
   Load();//避免代码重复 
   printf("\t\t\t\t\t-------- 学生成绩管理系统-------\n\n\n\n");     //菜单 
         printf("\t\t\t\t\t1. 输入学生成绩\n\n");
         printf("\t\t\t\t\t2. 修改学生记录\n\n");
         printf("\t\t\t\t\t3. 删除学生记录\n\n");
         printf("\t\t\t\t\t4. 插入学生记录\n\n");
         printf("\t\t\t\t\t5. 显示所有记录\n\n");
         printf("\t\t\t\t\t6. 查询学生记录\n\n");
         printf("\t\t\t\t\t7. 按平均成绩排序\n\n");
         printf("\t\t\t\t\t8. 输出各科目不及格学生\n\n");
         printf("\t\t\t\t\t9. 输出各科目最高分\n\n");
         printf("\t\t\t\t\t0. 退出\n\n\n");
         printf("请选择(0-9):");
    scanf("%d",&i);
   switch(i){
   case 1:inputt();break;
   case 2:modify();break;
    case 3:deletee();break;
    case 4:insert();break;
    case 5:display();break;
   case 6:search();break;
   case 7:sort();break;
   case 8:SearchLow();break;
   case 9:SearchHigh();break;
   case 0:exit(0);
   default:  ;
 }
}
  return 0; 
} 
  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2023-03-07 13:13
    关注

    242行 scanf("%d\n",&elect); 中的 \n删除掉。这会要求你输入1或2后,回车两次才能输出“请输入学生学号或姓名”提示信息

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 3月7日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?