学生信息管理系统(c语言实训)

C语言实训,老师让写一个学生信息管理系统,要求如下:

注:本文非ACM题目,用到最多的东西是关于链表和指针的操作。

对于链表的排序我用的是交换节点的内容选择排序。本来想着用改变指针的方法,写了一半脑子就乱了T_T

我的代码:

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define N 1000using namespace std;void start(){cout<<endl<<"==================================================================="<<endl;cout<<endl<<" | ★ ★ ★ ★★ ★★★ ★★ |"<<endl;cout<<endl<<" |欢迎来到 **东北林业高中** 教育信息平台|"<<endl;cout<<endl<<" ||"<<endl;cout<<endl<<" |Author—计算机(2)班 王小二|"<<endl;cout<<endl<<"================================================================== "<<endl;cout<<endl<<"您可以根据以下提示进行您所需要的操作:"<<endl;cout<<endl<<"增加学生信息—————-1显示学生信息—————-2"<<endl;cout<<endl<<"修改学生信息—————-3查询学生信息—————-4"<<endl;cout<<endl<<"删除学生信息—————-5对学生信息进行排序———-6"<<endl;cout<<endl<<"保存学生信息至记录文件——7从记录文件读取学生信息——8"<<endl;cout<<endl<<"新建学生信息文件————9运行结束——————-10"<<endl<<endl;}struct student{int num;char name[20];char sex[20];int math,chinese,english,computer,PE;double ave;int sum;int rank;student *next;};student *h,*t;void creatLink(){void sort();student *p1,*p2;int n=0;p1=(struct student *)malloc(sizeof(struct student));h=NULL;p1->next=NULL;printf("请输入信息(输入0,结束输入):(例如:lvshubao man 20130000)\n");scanf("%s",&p1->name);if(p1->name[0]=='0')return;scanf("%s%d",&p1->sex,&p1->num);printf("请输入成绩依次是:math,chinese,english,computer,PE\n");scanf("%d%d%d%d%d",&p1->math,&p1->chinese,&p1->english,&p1->computer,&p1->PE);h=p1;// printf("%d\n",h->num);// printf("%d\n",p1->num);p2=p1;p2->next=NULL;p1->ave=(p1->math+p1->chinese+p1->english+p1->computer+p1->PE)/5;p1->sum=p1->math+p1->chinese+p1->english+p1->computer+p1->PE;//printf("%d\n",p1->sum);p1=(struct student *)malloc(sizeof(struct student));p2->next=p1;printf("请再次输入信息:\n");scanf("%s",&p1->name);while(p1->name[0]!='0'){scanf("%s%d",&p1->sex,&p1->num);printf("请再次输入成绩\n");scanf("%d%d%d%d%d",&p1->math,&p1->chinese,&p1->english,&p1->computer,&p1->PE);p1->ave=(p1->math+p1->chinese+p1->english+p1->computer+p1->PE)/5;p1->sum=p1->math+p1->chinese+p1->english+p1->computer+p1->PE;p2=p1;p1=(struct student *)malloc(sizeof(struct student));p2->next=p1;printf("请再次输入信息:\n");scanf("%s",&p1->name);}t=p2;p2->next=NULL;sort();/*for(p1=h;p1!=NULL;p1=p1->next){printf("%s\n",p1->name);}*/}void show1(){student *p;for(p=h; p!=NULL; p=p->next){printf("姓名:%s 性别:%s 学号:%d\n",p->name,p->sex,p->num);printf("数学:%d 语文:%d 英语:%d 计算机:%d 体育:%d\n",p->math,p->chinese,p->english,p->computer,p->PE);printf("总成绩: %d 平均成绩: %.2f 排名:%d\n\n",p->sum,p->ave,p->rank);}}void sw(student *x,student *y){swap(x->name,y->name);swap(x->sex,y->sex);swap(x->num,y->num);swap(x->math,y->math);swap(x->english,y->english);swap(x->chinese,y->chinese);swap(x->computer,y->computer);swap(x->ave,y->ave);swap(x->PE,y->PE);swap(x->sum,y->sum);swap(x->rank,y->rank);}void sort()//名次递增{student *p,*q,*m;int maxx;for(p=h; p!=NULL; p=p->next){maxx=p->sum;for(q=p->next; q!=NULL; q=q->next){if(q->sum>maxx){maxx=q->sum;m=q;}}if(maxx>p->sum)sw(p,m);}int ip=1;for(p=h; p!=NULL; p=p->next){p->rank=ip++;}}void sort3()//名次递减{student *p,*q,*m;int maxx;for(p=h; p!=NULL; p=p->next){maxx=p->sum;for(q=p->next; q!=NULL; q=q->next){if(q->sum<maxx){maxx=q->sum;m=q;}}if(maxx<p->sum)sw(p,m);}}void sort1()//学号递减{student *p,*q,*m;int maxx;for(p=h; p!=NULL; p=p->next){maxx=p->num;for(q=p->next; q!=NULL; q=q->next){if(q->num<maxx){maxx=q->num;m=q;}}if(maxx<p->num)sw(p,m);}}void sort2()//学号递增{student *p,*q,*m;int maxx;for(p=h; p!=NULL; p=p->next){maxx=p->num;for(q=p->next; q!=NULL; q=q->next){if(q->num>maxx){maxx=q->num;m=q;}}if(maxx>p->num)sw(p,m);}}void sort5()//姓名递增{student *p,*q,*m;char maxx[30];for(p=h; p!=NULL; p=p->next){memset(maxx,0,sizeof(maxx));strcpy(maxx,p->name);for(q=p->next; q!=NULL; q=q->next){if(strcmp(maxx,q->name)>0){memset(maxx,0,sizeof(maxx));strcpy(maxx,q->name);m=q;}}if(strcmp(maxx,p->name)<0)sw(p,m);}}void sort6()//姓名递减{student *p,*q,*m;char maxx[30];for(p=h; p!=NULL; p=p->next){memset(maxx,0,sizeof(maxx));strcpy(maxx,p->name);for(q=p->next; q!=NULL; q=q->next){if(strcmp(maxx,q->name)<0){memset(maxx,0,sizeof(maxx));strcpy(maxx,q->name);m=q;}}if(strcmp(maxx,p->name)>0)sw(p,m);}}void show(){student *p;p=h;// printf("%d\n",h->num);//printf("%d\n",p->num);printf("按学号查询请输入1,按姓名查询请输入2,按名次查询请输入3\n");int x,y;scanf("%d",&x);if(x==1){printf("请输入学号: ");scanf("%d",&y);p=h;while(y!=p->num){p=p->next;}}else if(x==3){printf("请输入名次: ");scanf("%d",&y);p=h;while(y!=p->rank){p=p->next;}}else{printf("请输入姓名: ");char s[20];scanf("%s",s);p=h;while(strcmp(s,p->name)){p=p->next;}}printf("\n查询结果如下:\n");printf(" 姓名:%s 性别:%s 学号:%d\n",p->name,p->sex,p->num);printf(" 数学:%d 语文:%d 英语:%d 计算机:%d 体育:%d\n",p->math,p->chinese,p->english,p->computer,p->PE);printf(" 总成绩: %d 平均成绩: %.2f 排名:%d\n\n",p->sum,p->ave,p->rank);}void create_file(){FILE *fp;student *p;fp=fopen("d:\\data.txt","w");p=h;while(1){fprintf(fp,"%s %s %d %d %d %d %d %d %.2f %d %d\n",p->name,p->sex,p->num,p->math,p->chinese,p->english,p->computer,p->PE,p->ave,p->sum,p->rank);if(p->next==NULL)break;p=p->next;}fclose(fp);}void read_file(){FILE *fp;student *p1,*p2;fp=fopen("d:\\data.txt","r");int d=1;h=p2=(struct student *)malloc(sizeof(struct student));fscanf(fp,"%s %s %d %d %d %d %d %d %lf %d %d\n",p2->name,p2->sex,&p2->num,&p2->math,&p2->chinese,&p2->english,&p2->computer,&p2->PE,&p2->ave,&p2->sum,&p2->rank);while(!feof(fp)){p1=(struct student *)malloc(sizeof(struct student));fscanf(fp,"%s %s %d %d %d %d %d %d %lf %d %d\n",p1->name,p1->sex,&p1->num,&p1->math,&p1->chinese,&p1->english,&p1->computer,&p1->PE,&p1->ave,&p1->sum,&p1->rank);p2->next=p1;p2=p1;}p2->next=NULL;fclose(fp);}void delet(){student *p,*q;printf("请输入要删除学生的学号:\n");int x;scanf("%d",&x);int flag=1;if(h->num==x){h=h->next;for(p=h;p!=NULL;p=p->next)–(p->rank);return;}else{for(p=h; p->next!=NULL; p=p->next){if(p->next->num==x){flag=0;break;}}if(flag){printf("对不起,没有该学生!\n");}else{if(p->next->next==NULL){p->next=NULL;return;}p->next=p->next->next;for(p=p->next;p->next!=NULL; p=p->next)p->rank=p->rank-1;}}}void add(){printf("温馨提示: 请注意一次只能新增加一个学生,若增加多个请在主菜单再次选择“1” :)\n\n");student *p1,*p;int x;for(p=h;p!=NULL;p=p->next)t=p;p1=(struct student *)malloc(sizeof(struct student));t->next=p1;printf("请输入信息(输入0,,结束输入):(例如:lvshubao man 20130000)\n");scanf("%s",&p1->name);scanf("%s%d",&p1->sex,&p1->num);printf("请输入成绩依次是:math,chinese,english,computer,PE\n");scanf("%d%d%d%d%d",&p1->math,&p1->chinese,&p1->english,&p1->computer,&p1->PE);p1->next=NULL;// printf("%d\n",h->num);// printf("%d\n",p1->num);p1->ave=(p1->math+p1->chinese+p1->english+p1->computer+p1->PE)/5;p1->sum=p1->math+p1->chinese+p1->english+p1->computer+p1->PE;sort();}void xiugai(){student *p,*q,*p1;printf("请输入要修改学生的学号:\n");int x;scanf("%d",&x);int flag=1;for(p=h; p!=NULL; p=p->next){if(p->num==x){flag=0;break;}}if(flag){printf("对不起,没有该学生!\n");}else{p1=p;printf("重新输入该学生的信息(例如:lvshubao man 20130000)\n\n");scanf("%s",&p1->name);scanf("%s%d",&p1->sex,&p1->num);printf("请输入成绩依次是:math,chinese,english,computer,PE\n");scanf("%d%d%d%d%d",&p1->math,&p1->chinese,&p1->english,&p1->computer,&p1->PE);p1->ave=(p1->math+p1->chinese+p1->english+p1->computer+p1->PE)/5;p1->sum=p1->math+p1->chinese+p1->english+p1->computer+p1->PE;}}int main(){start();int x;int flag;printf("您当前的位置是主菜单,请输入您所需要的操作所对应的数字:\n");while(~scanf("%d",&x)){flag=1;if(x==1)add();else if(x==2)show1();else if(x==3)xiugai();else if(x==4)show();else if(x==5)delet();else if(x==6){printf("请根据提示选择您想要的排序要求:\n");cout<<endl<<"学号升序—————-1学号降序—————-2"<<endl;cout<<endl<<"名次降序—————-3名次升序—————-4"<<endl;cout<<endl<<"姓名升序—————-5姓名降序—————-6"<<endl;int y;scanf("%d",&y);if(y==1)sort1();else if(y==2)sort2();else if(y==3)sort3();else if(y==4)sort();else if(y==5)sort5();else if(y==6)sort6();else{printf("输入错误,将返回主菜单!\n");}}else if(x==7)create_file();else if(x==8)read_file();else if(x==9)creatLink();else if(x==10)break;else{printf("输入有误,请重新输入!\n");flag=0;continue;}if(flag==1)printf("您当前的位置是主菜单,请输入您所需要的操作所对应的数字:\n");}return 0;}本文__Yran原创,转载请标明出处

只做第一个我,不做第二个谁。

学生信息管理系统(c语言实训)

相关文章:

你感兴趣的文章:

标签云: