C语言如何实现简单学生管理系统
作者
这篇文章给大家分享的是有关C语言如何实现简单学生管理系统的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
花了一个月的时间,学习了链表,文件,多文件编程,然后就开始写学生管理系统,比较简单的那种,实现了增,添,改,查,多种排序,输入的时候的限定,成绩分析的功能。遇到的问题很多,也尝试解决了,下面写点写学生管理系统的要注意的地方,希望对别人有帮助。
1.一定要先写好主函数的框架,然后再往上面加东西,这样说有点笼统,我把我的主函数在下面,可以借鉴下哈。我的主函数是一个while(1)的无限循环,里面一个switch选择,利用一个菜单函数,让其返回值当成switch选择的值,然后进入不同的功能主函数,其他功能主函数和主函数的套路一样。
intmain(intargc,char*argv[]){ login();//这是个的介绍自己的函数 Sleep(1000); //睡眠函数 while(1) { switch(menu()) //主菜单选择函数 { case1: system("CLS"); //清屏函数 printf("\t\t________学生成绩添加_______"); head=luru(); cr(head); intchoice; scanf("%d",&choice); if(choice==0) break; case2: system("CLS"); //printf("\t\t_________学生成绩排序和删除___\d"); paxu(); intend; scanf("%d",&end); if(end==0) break; case3: system("CLS"); printf("\t\t__________成绩的修改__________"); xg(); inta; scanf("%d",&a); if(a==0) break; case4: system("CLS"); printf("\t\t___________学生信息分析_______"); fx(); intb; scanf("%d",&b); if(b==0) break; case5: system("CLS"); printf("\t\t___________谢谢使用哈_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return0; }
2.输入模块一定要注意输入的格式,我采用的尾插法,每次处理好一个节点后就让用户判定是否继续输入,在格式控制方面,我当用户输入一次,就判定它是否符号规范,如果不规范,就重新输入。最后存入文件中。
3.一定要把函数写的利用率高一点,这样就可以减少代码的重复书写,这个全凭自己的写代码的风格了,我自己在这点也没有做太好,反省中,以前刚开始学c的时候有人说c语言的主体是函数,不太懂,等写完这个,感觉说的真的很对,c语言的函数写好后,在该用的时候直接引用,很方便。
完整代码
主函数
main.c
#include<stdio.h> #include<stdlib.h> #include"student.h" #include<string.h> /*runthisprogramusingtheconsolepauseroraddyourowngetch,system("pause")orinputloop*/ intmain(intargc,char*argv[]){ login(); //structstudent*head; Sleep(1000); while(1) { switch(menu()) { case1: system("CLS"); printf("\t\t________学生成绩添加_______"); head=luru(); cr(head); intchoice; scanf("%d",&choice); if(choice==0) break; case2: system("CLS"); //printf("\t\t_________学生成绩排序和删除___\d"); paxu(); intend; scanf("%d",&end); if(end==0) break; case3: system("CLS"); printf("\t\t__________成绩的修改__________"); xg(); inta; scanf("%d",&a); if(a==0) break; case4: system("CLS"); printf("\t\t___________学生信息分析_______"); fx(); intb; scanf("%d",&b); if(b==0) break; case5: system("CLS"); printf("\t\t___________谢谢使用哈_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return0; }
函数声明
student.h
#include<stdio.h> structstudent*temp; structstudent*head; structstudent{ intnum; charname[6]; intyw; intsx; intyy; structstudent*next; }; intmenu(); voidlogin(); structstudent*luru(); voidcr(structstudent*h); structstudent*cc(); intmenu1(); voidpaxu(); voidpx1(); voidpx2(); voidpx3(); voidpx4(); voidsanch(); voidtj(); voidxg(); voidfx();
函数定义
student.c
#include<stdio.h> #include"student.h" #include<stdlib.h> #include<string.h> intmenu() { charn; do{ system("cls"); printf("\t\t\t|-------------------------------------------------|\n"); printf("\t\t\t|*****学生管理系统*****|\n"); printf("\t\t\t|-------------------------------------------------|\n"); printf("\t\t\t|1.学生成绩录入|\n"); printf("\t\t\t|2.学生成绩排序和删除|\n"); printf("\t\t\t|3.成绩的修改|\n"); printf("\t\t\t|4.学生成绩分析|\n"); printf("\t\t\t|5.退出程序|\n"); printf("\t\t\t---------------------------------------------------\n"); printf("请选择1-5:"); n=getch(); }while(n<'0'||n>'5'); return(n-48); } voidlogin() { printf("\n\n\n\t\t\t学生信息管理系统\n\n"); printf("\t\t\t版本号:0.2\n\n"); printf("\n\n\n\n\t\t\t2017年5月10日\n\n"); printf("\n\n\t\t\tsakurakid\n"); } structstudent*luru() { structstudent*rhead,*r,*t,*stu; rhead=(structstudent*)malloc(sizeof(structstudent)); t=rhead; rhead->next=NULL; charxx; intflag=1; printf("\t\t_____________________________\n"); printf("\t\t|学生管理系统|\n"); printf("\t\t_____________________________\n"); printf("\t\t||\n"); printf("\t\t|输入1添加|\n"); printf("\t\t|输入0退出|\n"); printf("\t\t|___________________________|\n"); while(xx!='1'&&xx!='0') { xx=getch(); } if(xx=='1') { system("CLS"); do{ system("CLS"); stu=(structstudent*)malloc(sizeof(structstudent)); printf("\t\t_____________________________\n"); printf("\t\t|学生管理系统|\n"); printf("\t\t|___________________________|\n"); printf("\t\t||\n"); printf("\t\t|输入1添加|\n"); printf("\t\t|输入0退出|\n"); printf("\t\t|___________________________|\n"); printf("\t\t学号:");scanf("%d",&stu->num); if(stu->num>99999999||stu->num<10000000) { printf("\t\t______________________\n"); printf("\t\t_请输入8位非负数__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入学号:");scanf("%d",&stu->num); } printf("\t\t姓名:");scanf("%s",stu->name); if(stu->name[0]>0) { printf("\t\t______________________\n"); printf("\t\t______姓名请输入汉字__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入姓名:");scanf("%s",stu->name); } printf("\t\t语文:");scanf("%d",&stu->yw); printf("\t\t数学:");scanf("%d",&stu->sx); printf("\t\t英语:");scanf("%d",&stu->yy); if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100) { printf("\t\t______________________\n"); printf("\t\t_成绩请输入0-100之间__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入成绩\n"); printf("\t\t语文:");scanf("%d",&stu->yw); printf("\t\t数学:");scanf("%d",&stu->sx); printf("\t\t英语:");scanf("%d",&stu->yy); } t->next=stu; t=stu; xx=getch(); printf("继续输入请按1,退出请按0:"); while(xx!='1'&&xx!='0') { xx=getch(); } }while(xx=='1'); t->next=NULL; } return(rhead); } voidcr(structstudent*h) { structstudent*stu; FILE*fp; if((fp=fopen("学生信息.txt","wt"))==NULL) { printf("文件出错233,按任意键退出!"); getch(); exit(1); } for(stu=h->next;stu!=NULL;stu=stu->next) { fprintf(fp,"%d%s%d%d%d\n",stu->num,stu->name,stu->yw,stu->sx,stu->yy); } fclose(fp); } structstudent*cc() { structstudent*ahead,*r,*stu; FILE*f1; f1=fopen("学生信息.txt","rt"); ahead=(structstudent*)malloc(sizeof(structstudent)); ahead->next=NULL; r=ahead; while(!feof(f1)) { stu=(structstudent*)malloc(sizeof(structstudent)); fscanf(f1,"%d%s%d%d%d\n",&stu->num,stu->name,&stu->yw,&stu->sx,&stu->yy); r->next=stu; r=stu; } r->next=NULL; fclose(f1); returnahead; } intmenu1() { system("CLS"); charn; do{ system("CLS"); printf("\t\t__________________________________________________\n"); printf("\t\t||\n"); printf("\t\t|1.顺序排序2.按英语排序3.按语文排序|\n"); printf("\t\t|4.按数学排序5.删除学生6.添加学生|\n"); printf("\t\t||\n"); printf("\t\t|______________按0退出___________________________|\n"); printf("\t\t请选择0-6:"); n=getch(); }while(n<'0'||n>'6'); return(n-48); } voidpaxu() { system("CLS"); charn; do{ switch(menu1()) { case1: px1(); break; case2: system("CLS"); px2(); break; case3: system("CLS"); px3(); break; case4: system("CLS"); px4(); break; case5: system("CLS"); sanch(); break; case6: system("CLS"); tj(); break; } printf("继续输入请按1,退出请按0:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } voidpx1() { structstudent*t,*h; h=cc(); printf("\n"); printf("\t\t%10s%10s%10s%10s%10s\n","学号","姓名","语文","数学","英语","平均分","总分"); printf("\t\t--------------------------------------------------------------\n"); for(t=h->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } voidpx2() { structstudent*p,*q,*head,*t; head=cc(); inttnum; chartname[6]; inttyw; inttsx; inttyy; for(p=head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->yy<q->yy) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t英语排序\n"); printf("\t\t%10s%10s%10s%10s%10s\n","学号","姓名","语文","数学","英语","平均分","总分"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } voidpx3() { structstudent*p,*q,*head,*t; head=cc(); inttnum; chartname[6]; inttyw; inttsx; inttyy; for(p=head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->yw<q->yw) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t语文排序\n"); printf("\t\t%10s%10s%10s%10s%10s\n","学号","姓名","语文","数学","英语","平均分","总分"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } voidpx4() { structstudent*p,*q,*head,*t; head=cc(); inttnum; chartname[6]; inttyw; inttsx; inttyy; for(p=head->next;p->next!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->sx<q->sx) { tnum=p->num; p->num=q->num; q->num=tnum; strcpy(tname,p->name); strcpy(p->name,q->name); strcpy(q->name,tname); tyw=p->yw; p->yw=q->yw; q->yw=tyw; tsx=p->sx; p->sx=q->sx; q->sx=tsx; tyy=p->yy; p->yy=q->yy; q->yy=tyy; } } } printf("\t\t数学排序\n"); printf("\t\t%10s%10s%10s%10s%10s\n","学号","姓名","语文","数学","英语","平均分","总分"); printf("\t\t--------------------------------------------------------------\n"); for(t=head->next;t!=NULL;t=t->next) { printf("\t\t%10d%10s%10d%10d%10d%\n",t->num,t->name,t->yw,t->sx,t->yy); } } voidsanch() { px1(); structstudent*p,*q,*phead,*t,*m; phead=cc(); intn; intflag=0; printf("\t\t____________________________________________________\n"); printf("\t\t|______________删除学生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t请输入要删除的学生学号:"); scanf("%d",&n); p=phead; for(m=phead;m!=NULL;m=m->next) { if(m->num==n) { printf("\t\t已经查到改学生的信息\n"); flag=0; break; } else flag=1; } if(flag==1) printf("\t\t抱歉,没有查到该学生的信息\n"); if(flag==0) { if(phead->num==n) { phead=phead->next; } else { while(p->num!=n&&p->next!=NULL) { t=p; p=p->next; } if(p->num==n) { t->next=p->next; } } printf("\t\t该学生信息已删除\n"); } cr(phead); } voidtj() { structstudent*phead,*stu; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________添加学生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); stu=(structstudent*)malloc(sizeof(structstudent)); printf("\t\t学号:");scanf("%d",&stu->num); if(stu->num>99999999||stu->num<10000000) { printf("\t\t______________________\n"); printf("\t\t_请输入8位非负数__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入学号:");scanf("%d",&stu->num); } printf("\t\t姓名:");scanf("%s",stu->name); if(stu->name[0]>0) { printf("\t\t______________________\n"); printf("\t\t______姓名请输入汉字__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入姓名:");scanf("%s",stu->name); } printf("\t\t语文:");scanf("%d",&stu->yw); printf("\t\t数学:");scanf("%d",&stu->sx); printf("\t\t英语:");scanf("%d",&stu->yy); if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100) { printf("\t\t______________________\n"); printf("\t\t_成绩请输入0-100之间__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入成绩\n"); printf("\t\t语文:");scanf("%d",&stu->yw); printf("\t\t数学:");scanf("%d",&stu->sx); printf("\t\t英语:");scanf("%d",&stu->yy); } stu->next=phead->next; phead->next=stu; printf("\t\t学生信息已存入\n"); cr(phead); } voidxg() { system("CLS"); charn; do{ system("CLS"); px1(); structstudent*p,*q,*phead,*t,*m; intflag=0; inttimp; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________修改学生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t请输入要修改的学生学号:"); scanf("%d",&timp); p=phead; for(m=phead;m!=NULL;m=m->next) { if(m->num==timp) { printf("\t\t已经查到改学生的信息\n"); flag=0; break; } else flag=1; } if(flag==1) printf("\t\t抱歉,没有查到该学生的信息\n"); if(flag==0) { printf("\t\t该学生的原信息如下\n"); printf("\t\t学号:%d\n",m->num); printf("\t\t姓名:%s\n",m->name); printf("\t\t语文:%d\n",m->yw); printf("\t\t数学:%d\n",m->sx); printf("\t\t英语:%d\n\n",m->yy); printf("\t\t请重新输入该学生的信息\n"); printf("\t\t学号:");scanf("%d",&m->num); if(m->num>99999999||m->num<10000000) { printf("\t\t______________________\n"); printf("\t\t_请输入8位非负数__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入学号:");scanf("%d",&m->num); } printf("\t\t姓名:");scanf("%s",m->name); if(m->name[0]>0) { printf("\t\t______________________\n"); printf("\t\t______姓名请输入汉字__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入姓名:");scanf("%s",m->name); } printf("\t\t语文:");scanf("%d",&m->yw); printf("\t\t数学:");scanf("%d",&m->sx); printf("\t\t英语:");scanf("%d",&m->yy); if(m->sx<0||m->sx>100||m->yw<0||m->yw>100||m->yy<0||m->yy>100) { printf("\t\t______________________\n"); printf("\t\t_成绩请输入0-100之间__\n"); printf("\t\t______________________\n"); printf("\t\t重新输入成绩\n"); printf("\t\t语文:");scanf("%d",&m->yw); printf("\t\t数学:");scanf("%d",&m->sx); printf("\t\t英语:");scanf("%d",&m->yy); } printf("\t\t该学生信息已修改\n"); cr(phead); } printf("继续修改请按1,退出请按0:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } voidfx() { structstudent*p,*q,*phead,*t,*m; intflag=0; intjyw=0; intjsx=0; intjyy=0; inttimp; intn=0; phead=cc(); p=phead; for(t=p->next;t!=NULL;t=t->next) { n++; if(t->sx>=60) jsx++; if(t->yw>=60) jyw++; if(t->yy>=60) jyy++; } printf("\t\t____________________________________________________\n"); printf("\t\t|______________学生成绩分析_________________________|\n"); printf("\t\t|___________________________________________________|\n"); printf("\t\t一共录入了%d个学生成绩\n",n); printf("\t\t语文及格人数为%d\n",jyw); printf("\t\t数学及格人数为%d\n",jsx); printf("\t\t英语及格人数为%d\n",jyy); printf("\t\t分析完毕\n"); printf("退出请按0:"); }
感谢各位的阅读!关于“C语言如何实现简单学生管理系统”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~