C语言实现简单学生管理系统
作者:sakurakider
花了一个月的时间,学习了链表,文件,多文件编程,然后就开始写学生管理系统,比较简单的那种,实现了增,添,改,查,多种排序,输入的时候的限定,成绩分析的功能。遇到的问题很多,也尝试解决了,下面写点写学生管理系统的要注意的地方,希望对别人有帮助。
1.一定要先写好主函数的框架,然后再往上面加东西,这样说有点笼统,我把我的主函数在下面,可以借鉴下哈。我的主函数是一个while(1)的无限循环,里面一个switch选择,利用一个菜单函数,让其返回值当成switch选择的值,然后进入不同的功能主函数,其他功能主函数和主函数的套路一样。
int main(int argc, char *argv[]) { login(); //这是个的介绍自己的函数 Sleep(1000); //睡眠函数 while(1) { switch(menu()) //主菜单选择函数 { case 1: system("CLS"); //清屏函数 printf("\t\t________学生成绩添加_______"); head=luru(); cr(head); int choice; scanf("%d",&choice); if(choice==0) break; case 2: system("CLS"); //printf("\t\t_________学生成绩排序和删除___\d"); paxu(); int end; scanf("%d",&end); if(end==0) break; case 3: system("CLS"); printf("\t\t__________成绩的修改__________"); xg(); int a; scanf("%d",&a); if(a==0) break; case 4: system("CLS"); printf("\t\t___________学生信息分析_______"); fx(); int b; scanf("%d",&b); if(b==0) break; case 5: system("CLS"); printf("\t\t___________谢谢使用哈_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return 0; }
2.输入模块一定要注意输入的格式,我采用的尾插法,每次处理好一个节点后就让用户判定是否继续输入,在格式控制方面,我当用户输入一次,就判定它是否符号规范,如果不规范,就重新输入。最后存入文件中。
3.一定要把函数写的利用率高一点,这样就可以减少代码的重复书写,这个全凭自己的写代码的风格了,我自己在这点也没有做太好,反省中,以前刚开始学c的时候有人说c语言的主体是函数,不太懂,等写完这个,感觉说的真的很对,c语言的函数写好后,在该用的时候直接引用,很方便。
完整代码
主函数
main.c
#include <stdio.h> #include <stdlib.h> #include "student.h" #include <string.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { login(); //struct student *head; Sleep(1000); while(1) { switch(menu()) { case 1: system("CLS"); printf("\t\t________学生成绩添加_______"); head=luru(); cr(head); int choice; scanf("%d",&choice); if(choice==0) break; case 2: system("CLS"); //printf("\t\t_________学生成绩排序和删除___\d"); paxu(); int end; scanf("%d",&end); if(end==0) break; case 3: system("CLS"); printf("\t\t__________成绩的修改__________"); xg(); int a; scanf("%d",&a); if(a==0) break; case 4: system("CLS"); printf("\t\t___________学生信息分析_______"); fx(); int b; scanf("%d",&b); if(b==0) break; case 5: system("CLS"); printf("\t\t___________谢谢使用哈_______\n"); Sleep(1000); exit(-1); break; } system("PAUSE"); } return 0; }
函数声明
student.h
#include <stdio.h> struct student *temp; struct student *head; struct student{ int num; char name[6]; int yw; int sx; int yy; struct student*next; }; int menu(); void login(); struct student* luru(); void cr(struct student *h); struct student*cc(); int menu1(); void paxu(); void px1(); void px2(); void px3(); void px4(); void sanch(); void tj(); void xg(); void fx();
函数定义
student.c
#include <stdio.h> #include "student.h" #include <stdlib.h> #include <string.h> int menu() { char n; 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); } void login() { printf("\n\n\n\t\t\t 学生信息管理系统\n\n"); printf("\t\t\t 版本号:0.2\n\n"); printf("\n\n\n\n\t\t\t 2017年5月10日\n\n"); printf("\n\n\t\t\t sakurakid\n"); } struct student *luru() { struct student*rhead ,*r,*t,*stu; rhead=(struct student*)malloc(sizeof(struct student)); t=rhead; rhead->next=NULL; char xx; int flag=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=(struct student*)malloc(sizeof(struct student)); 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); } void cr(struct student *h) { struct student *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); } struct student*cc() { struct student*ahead,*r,*stu; FILE*f1; f1=fopen("学生信息.txt","rt"); ahead=(struct student*)malloc(sizeof(struct student)); ahead->next=NULL; r=ahead; while(!feof(f1)) { stu=(struct student*)malloc(sizeof(struct student)); 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); return ahead; } int menu1() { system("CLS"); char n; 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); } void paxu() { system("CLS"); char n; do{ switch(menu1()) { case 1: px1(); break; case 2: system("CLS"); px2(); break; case 3: system("CLS"); px3(); break; case 4: system("CLS"); px4(); break; case 5: system("CLS"); sanch(); break; case 6: system("CLS"); tj(); break; } printf("继续输入请按1,退出请按0:"); n=getch(); while(n!='1'&&n!='0') { n=getch(); } }while(n=='1'); if(n=='0') return; } void px1() { struct student *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); } } void px2() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; 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); } } void px3() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; 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); } } void px4() { struct student *p,*q,*head,*t; head=cc(); int tnum; char tname[6]; int tyw; int tsx; int tyy; 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); } } void sanch() { px1(); struct student *p,*q,*phead,*t,*m; phead=cc(); int n; int flag=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); } void tj() { struct student *phead,*stu; phead=cc(); printf("\t\t____________________________________________________\n"); printf("\t\t|______________添加学生信息_________________________|\n"); printf("\t\t|___________________________________________________|\n"); stu=(struct student*)malloc(sizeof(struct student)); 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); } void xg() { system("CLS"); char n; do{ system("CLS"); px1(); struct student *p,*q,*phead,*t,*m; int flag=0; int timp; 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; } void fx() { struct student *p,*q,*phead,*t,*m; int flag=0; int jyw=0; int jsx=0; int jyy=0; int timp; int n=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:"); }
总结
以上就是我要说的了哈,忙了一个月,学的东西也都用上了,只要链表,文件会的话,写个简单的学生管理系统很简单的,希望对大家有帮助,自己也要学新东西了,如果有问题的话,欢迎支持大家指出,其实还有很多功能自己没写,比如多端登陆,图形界面等等,跟别人的差距蛮大的。
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。