⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 051150055.cpp

📁 学生信息管理系统,本程序共有八个功能与数据结构说明: 每一条记录包括一个学生的学号、姓名、3门课成绩、平均成绩。 1.学生记录的输入
💻 CPP
字号:
#include<stdio.h>       //I/O函数
#include<string.h>      //字符串函数
#include<stdlib.h>      //文件函数
#include<conio.h>       //屏幕操作函数
int i,n;              //n为链表结点的个数,也就是学生的个数
struct message       //定义结构体,包含学生学号,姓名,三科成绩及平均分
{char num[10];    //学生学号
char name[15];   //姓名
int score[3];
float ave;
message *next; //定义指向结构体的指针,用于链表的正确连接
};
message *p,*q,*head;

int choice()//实现显示整个主菜单及提示用户输入自己的选择项
{int c;
 char s[10];
 clrscr();//清屏
 printf("\n\n\n         ~***************MENU***************~\n");
 printf("         ||                               ||              * *     \n");
 printf("         || 1 Input the student's records ||               ^       \n");
 printf("         ||                               ||\n");
 printf("         || 2 Show all records            ||\n");
 printf("         ||                               ||              the\n");
 printf("         || 3 Search the record           ||\n");
 printf("         ||                               ||            records' \n");
 printf("         || 4 Sort the records            ||\n");
 printf("         ||                               ||            management\n");
 printf("         || 5 Insert a student's record   ||\n");
 printf("         ||                               ||             ^   ^    \n");
 printf("         || 6 Delete a student's record   ||               @      \n");
 printf("         ||                               ||\n");
 printf("         || 7    save the file            ||\n");
 printf("         ||                               ||\n");
 printf("         || 8    Load the file            ||              ~ ~   \n");
 printf("         ||                               ||               &     \n"); 
 printf("         || 9       Quit                  ||\n");
 printf("         ||                               ||\n");
 printf("         #@*******************************@#\n");
  do
  {printf("\nplease enter you choice(1-9):");
   scanf("%s",&s);//输入你选择的序号
   c=atoi(s);//将输入的字符串转化为整数,不然有可能输入字符而进入死循环
  }while(c<1||c>9);
  return(c);   //返回你输入的数值,用于主函数之用
} 


void input()//输入学生的信息,包括学号,姓名,三科成绩及算出学生的平均成绩
{ clrscr();//清屏
  printf("\ninput the number of student:");scanf("%d",&n);
  q=head;
  for( i=0;i<n;i++)//输入学生信息
{ p=new message;
  printf("Please input the message of student %d :\n",i+1);
  printf("NO.:");
  scanf("%s",p->num);
  printf("name:");
  scanf("%s",p->name);
  for(int j=0;j<3;j++)
  {printf("score %d :",j+1);
   scanf("%d",&p->score[j]);
  }
  p->ave=(p->score[0]+p->score[1]+p->score[2])/3.0;
  q->next=p;
  q=p;
}p->next=0;
}


void display()//显示函数,用来显示链表信息即学生信息
{ clrscr();//清屏
  p=head->next;
  printf("   NO.     name   score1   score2   score3   average\n");
  while(p!=0)
{ printf("%5s",p->num);
  printf("%10s",p->name);
  for(int j=0;j<3;j++)
    printf("%9d",p->score[j]);
  printf("%8.2f\n",p->ave);
  p=p->next;}
}


void find()//查找函数,用来实现查找链表中学生的信息,在函数中输入要查找的学生的姓名
{char temp[15];clrscr();//清屏
 p=head->next;
 printf("\nplease input the student's name you want to find:");
 scanf("%s",temp);
for(i=0;i<n;i++)
 {if(strcmp(p->name,temp)==0)break;else p=p->next;}
 if(i==n)printf("!!!~Error~!!!This student no exist!");//避免没找到而没反应
 else //找到就把该学生信息显示出来
 { printf("   NO.     name   score1   score2   score3   average\n");
  printf("%5s",p->num);
  printf("%10s",p->name);
  for(int j=0;j<3;j++)
    printf("%9d",p->score[j]);
  printf("%8.2f\n",p->ave); 
  }
}


void dele()//删除函数,用来实现删除链表中学生的信息,在函数中给出要删除的学生的名字
{char temp[15];clrscr();//清屏
 p=head->next;
 q=p->next;
 printf("\nyou want to delete the student whose name is:");
 scanf("%s",temp);
if(strcmp(p->name,temp)==0){head->next=p->next;delete p;n--;}/*当第一个结点就是你要删掉的学生时采用if语句,而且还要将n自减1以便取出文件时,链表结点个数准确*/
else        //当第一个结点不是要删掉的学生时做else语句
{while(q!=0)
  if(strcmp(q->name,temp)==0)break;else{p=q;q=q->next;}/*要删掉的学生的地址在q中,而通过这样,就可以使用到该学生的前一个学生的地址,在p中*/
 if(q==0)printf("!!!~Error~!!!This student no exist!");else 
{q=p->next;//找到你所要删掉的学生名字所在的结点
p->next=q->next;
 delete q;
 n--;}//把n自减1是为了等下存盘把n存进去,以便取出时n能正确得到整个链表的个数
}
}


void sort()//排序函数,用来实现根据学生的平均分数对学生信息的排序,从分数高到低
{char a[10],b[15];int c;float d;
 p=head->next;
  while(p!=0)
{ q=p->next;
  while(q!=0)
  {if(p->ave<q->ave)
     {strcpy(a,p->num);//字符串复制,实现学号互换
     strcpy(p->num,q->num);
     strcpy(q->num,a);
     strcpy(b,p->name);//名字互换
     strcpy(p->name,q->name);
     strcpy(q->name,b);
     c=p->score[0];
     p->score[0]=q->score[0];
     q->score[0]=c;
     c=p->score[1];
     p->score[1]=q->score[1];
     q->score[1]=c;
     c=p->score[2];
     p->score[2]=q->score[2];
     q->score[2]=c;
     d=p->ave;
     p->ave=q->ave;
     q->ave=d;
      }
   q=q->next;
   }
p=p->next;
}//用双重循环来实现排序功能,把前一个结点放在p中,后一个结点放在q中
display();
}


void insert()/*插入函数,用来实现用户插入新学生信息的功能,在函数中指出所插入学生位于哪个学生后面*/
{char temp[15],y;clrscr();//清屏
 printf("\n you want to insert a student after the student whose name is:");  
 scanf("%s",temp);
 p=head->next;//将链表的头指针给p
 while(p!=0)
{if(strcmp(p->name,temp)==0)break;else p=p->next;}//找到所要插入学生位于该学生后面
if(p==0)printf("\n!!!Error!!!This student no exist!");/*如果是p=0,则表示整个链表都找完,没找到*/
else 
 {q=new message; //新建一个结点存放新输入学生的信息
 printf("NO.:");
  scanf("%s",q->num);
  printf("name:");
  scanf("%s",q->name);
  for(int j=0;j<3;j++)
  {printf("score %d :",j+1);
   scanf("%d",&q->score[j]);
  }
 
 q->ave=(q->score[0]+q->score[1]+q->score[2])/3.0;
 q->next=p->next;//交换地址以利于链表正确连上
 p->next=q;
  printf("\n\nDo you want to sort? y/n? ");//是否排序
   scanf("%s",&y);
   if(y=='Y'||y=='y')sort();else display();
 n++;}//把n自加1是为了存盘一起存进去,使文件读出时,链表个数正确
}


void save()//文件的存盘
{ FILE *fp;//文件指针
 p=head->next;//在存盘时候把链表首地址先给p
 if((fp=fopen("student.cpp","wb"))==NULL||p==0)/*如果链表已经被删完了,即p=0,则不能存进去,结束整个程序*/
 {printf("Error,cannot open file\n");
  exit(0);}
fwrite(&n,sizeof(int),1,fp);//把n一同存入盘中
 while(p!=0)  //直到链表结束,存盘也结束
  {if(fwrite(p,sizeof(struct message),1,fp)!=1)
  printf("File write error\n");p=p->next;}
  fclose(fp);
clrscr();//清屏
}


void load()//文件的读出
{FILE *fp;
head=new message;//文件取出时候要新建一个链表,把该链表头指针放在head->next中
p=new message;//链表的第一个结点
head->next=p;//把第一个结点的地址放在head->next中,正确把链表连接起来
if((fp=fopen("student.cpp","rb"))==NULL)
 {printf("Error,cannot open file\n");
 exit(0);}
fread(&n,sizeof(int),1,fp);
for(i=0;i<n-1;i++)
  {if(fread(p,sizeof(struct message),1,fp)!=1)
  printf("File read error\n");q=new message;p->next=q;p=q;}//读到链表倒数前一个
 fread(p,sizeof(struct message),1,fp);//读出链表最后一个结点
 p->next=0;//把最后一个结点的存放地址处放0结束标志
 fclose(fp);
 display();
}


void main()//主函数
{head=new message;/*在主函数里产生一个结构体的空间,把整个链表的首地址放在head->next中*/
  
 for(;;)//这是个无限循环,在循环体里有结束的条件,直到按9才结束
  { 
    { switch(choice())//在choice返回值为整数时做为switch的开关条件
      { case 0: printf("\n\nPlease,choose the number from 1 to 9\n");break;
        case 1: input();break;//等于1时候进行输入学生信息
        case 2: display();break;//等于2时候进行显示学生信息
        case 3: find();break;/*等于3时候进行查找学生信息,在该函数中要给出你要找的学生的姓名*/
        case 4: sort();break;//等于4时候进行排序
        case 5: insert();break;/*等于5时候进行插入一个学生信息,在该函数中要给出你要插入该学生于哪个学生后面*/
        case 6: dele();break;/*等于6时候进行删除一个学生信息,在该函数中要给出你要删除学生的名字*/
        case 7: save();break;//等于7时候进行文件的存盘
        case 8: load();break;//等于8时候进行文件的读出
        case 9:  exit(0); }//等于9退出整个程序
     printf("\n\n\npress any key to the menu......\n");/*如果没有退出整个程序,则进行完一次选择后按任意键回到主菜单*/
     }
      getch();//读入任意字符
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -