本文链接:https://blog.csdn.net/sakurakider/article/details/72823662
花了一个月的时间,学习了链表,文件,多文件编程,然后就开始写学生管理系统,比较简单的那种,实现了增,添,改,查,多种排序,输入的时候的限定,成绩分析的功能。遇到的问题很多,也尝试解决了,下面写点写学生管理系统的要注意的地方,希望对别人有帮助。
一定要先写好主函数的框架,然后再往上面加东西,这样说有点笼统,我把我的主函数在下面,可以借鉴下哈。我的主函数是一个while(1)的无限循环,里面一个switch选择,利用一个菜单函数,让其返回值当成switch选择的值,然后进入不同的功能主函数,其他功能主函数和主函数的套路一样。
代码语言:javascript复制int main(int argc, char *argv[]) {
login(); //这是个的介绍自己的函数
Sleep(1000);
//睡眠函数
while(1)
{
switch(menu())
//主菜单选择函数
{
case 1:
system("CLS");
//清屏函数
printf("tt________学生成绩添加_______");
head=luru();
cr(head);
int choice;
scanf("%d",&choice);
if(choice==0)
break;
case 2:
system("CLS");
//printf("tt_________学生成绩排序和删除___d");
paxu();
int end;
scanf("%d",&end);
if(end==0)
break;
case 3:
system("CLS");
printf("tt__________成绩的修改__________");
xg();
int a;
scanf("%d",&a);
if(a==0)
break;
case 4:
system("CLS");
printf("tt___________学生信息分析_______");
fx();
int b;
scanf("%d",&b);
if(b==0)
break;
case 5:
system("CLS");
printf("tt___________谢谢使用哈_______n");
Sleep(1000);
exit(-1);
break;
}
system("PAUSE");
}
return 0;
}
2.输入模块一定要注意输入的格式,我采用的尾插法,每次处理好一个节点后就让用户判定是否继续输入,在格式控制方面,我当用户输入一次,就判定它是否符号规范,如果不规范,就重新输入。最后存入文件中。
3.一定要把函数写的利用率高一点,这样就可以减少代码的重复书写,这个全凭自己的写代码的风格了,我自己在这点也没有做太好,反省中,以前刚开始学c的时候有人说c语言的主体是函数,不太懂,等写完这个,感觉说的真的很对,c语言的函数写好后,在该用的时候直接引用,很方便。
完整代码
主函数
main.c
代码语言:javascript复制#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("tt________学生成绩添加_______");
head=luru();
cr(head);
int choice;
scanf("%d",&choice);
if(choice==0)
break;
case 2:
system("CLS");
//printf("tt_________学生成绩排序和删除___d");
paxu();
int end;
scanf("%d",&end);
if(end==0)
break;
case 3:
system("CLS");
printf("tt__________成绩的修改__________");
xg();
int a;
scanf("%d",&a);
if(a==0)
break;
case 4:
system("CLS");
printf("tt___________学生信息分析_______");
fx();
int b;
scanf("%d",&b);
if(b==0)
break;
case 5:
system("CLS");
printf("tt___________谢谢使用哈_______n");
Sleep(1000);
exit(-1);
break;
}
system("PAUSE");
}
return 0;
}
函数声明
student.h
代码语言:javascript复制#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
代码语言:javascript复制#include <stdio.h>
#include "student.h"
#include <stdlib.h>
#include <string.h>
int menu()
{
char n;
do{
system("cls");
printf("ttt|-------------------------------------------------|n");
printf("ttt| *****学生管理系统***** |n");
printf("ttt|-------------------------------------------------|n");
printf("ttt| 1.学生成绩录入 |n");
printf("ttt| 2.学生成绩排序和删除 |n");
printf("ttt| 3.成绩的修改 |n");
printf("ttt| 4.学生成绩分析 |n");
printf("ttt| 5.退出程序 |n");
printf("ttt---------------------------------------------------n");
printf("请选择1-5:");
n=getch();
}while(n<'0'||n>'5');
return(n-48);
}
void login()
{
printf("nnnttt 学生信息管理系统nn");
printf("ttt 版本号:0.2nn");
printf("nnnnttt 2017年5月10日nn");
printf("nnttt sakurakidn");
}
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("tt_____________________________n");
printf("tt| 学生管理系统 |n");
printf("tt_____________________________n");
printf("tt| |n");
printf("tt| 输入1添加 |n");
printf("tt| 输入0退出 |n");
printf("tt|___________________________|n");
while(xx!='1'&&xx!='0')
{
xx=getch();
}
if(xx=='1')
{
system("CLS");
do{
system("CLS");
stu=(struct student*)malloc(sizeof(struct student));
printf("tt_____________________________n");
printf("tt| 学生管理系统 |n");
printf("tt|___________________________|n");
printf("tt| |n");
printf("tt| 输入1添加 |n");
printf("tt| 输入0退出 |n");
printf("tt|___________________________|n");
printf("tt学号: ");scanf("%d",&stu->num);
if(stu->num > 99999999||stu->num < 10000000)
{
printf("tt______________________n");
printf("tt_请输入8位非负数__n");
printf("tt______________________n");
printf("tt重新输入学号: ");scanf("%d",&stu->num);
}
printf("tt姓名: ");scanf("%s",stu->name);
if(stu->name[0] > 0)
{
printf("tt______________________n");
printf("tt______姓名请输入汉字__n");
printf("tt______________________n");
printf("tt重新输入姓名: ");scanf("%s",stu->name);
}
printf("tt语文: ");scanf("%d",&stu->yw);
printf("tt数学: ");scanf("%d",&stu->sx);
printf("tt英语: ");scanf("%d",&stu->yy);
if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100)
{
printf("tt______________________n");
printf("tt_成绩请输入0-100之间__n");
printf("tt______________________n");
printf("tt重新输入成绩n");
printf("tt语文: ");scanf("%d",&stu->yw);
printf("tt数学: ");scanf("%d",&stu->sx);
printf("tt英语: ");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 %dn",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 %dn",&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("tt__________________________________________________n");
printf("tt| |n");
printf("tt| 1.顺序排序 2.按英语排序 3.按语文排序 |n");
printf("tt| 4.按数学排序 5.删除学生 6.添加学生 |n");
printf("tt| |n");
printf("tt|______________按0退出___________________________|n");
printf("tt请选择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("ttsssssn","学号","姓名","语文","数学","英语","平均分","总分");
printf("tt--------------------------------------------------------------n");
for(t=h->next;t!=NULL;t=t->next)
{
printf("ttdsddd%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("tt 英语排序 n");
printf("ttsssssn","学号","姓名","语文","数学","英语","平均分","总分");
printf("tt--------------------------------------------------------------n");
for(t=head->next;t!=NULL;t=t->next)
{
printf("ttdsddd%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("tt 语文排序 n");
printf("ttsssssn","学号","姓名","语文","数学","英语","平均分","总分");
printf("tt--------------------------------------------------------------n");
for(t=head->next;t!=NULL;t=t->next)
{
printf("ttdsddd%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("tt 数学排序 n");
printf("ttsssssn","学号","姓名","语文","数学","英语","平均分","总分");
printf("tt--------------------------------------------------------------n");
for(t=head->next;t!=NULL;t=t->next)
{
printf("ttdsddd%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("tt____________________________________________________n");
printf("tt|______________删除学生信息_________________________|n");
printf("tt|___________________________________________________|n");
printf("tt请输入要删除的学生学号:");
scanf("%d",&n);
p=phead;
for(m=phead;m!=NULL;m=m->next)
{
if(m->num==n)
{
printf("tt已经查到改学生的信息n");
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
printf("tt抱歉,没有查到该学生的信息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("tt该学生信息已删除n");
}
cr(phead);
}
void tj()
{
struct student *phead,*stu;
phead=cc();
printf("tt____________________________________________________n");
printf("tt|______________添加学生信息_________________________|n");
printf("tt|___________________________________________________|n");
stu=(struct student*)malloc(sizeof(struct student));
printf("tt学号: ");scanf("%d",&stu->num);
if(stu->num > 99999999||stu->num < 10000000)
{
printf("tt______________________n");
printf("tt_请输入8位非负数__n");
printf("tt______________________n");
printf("tt重新输入学号: ");scanf("%d",&stu->num);
}
printf("tt姓名: ");scanf("%s",stu->name);
if(stu->name[0] > 0)
{
printf("tt______________________n");
printf("tt______姓名请输入汉字__n");
printf("tt______________________n");
printf("tt重新输入姓名: ");scanf("%s",stu->name);
}
printf("tt语文: ");scanf("%d",&stu->yw);
printf("tt数学: ");scanf("%d",&stu->sx);
printf("tt英语: ");scanf("%d",&stu->yy);
if(stu->sx<0||stu->sx>100||stu->yw<0||stu->yw>100||stu->yy<0||stu->yy>100)
{
printf("tt______________________n");
printf("tt_成绩请输入0-100之间__n");
printf("tt______________________n");
printf("tt重新输入成绩n");
printf("tt语文: ");scanf("%d",&stu->yw);
printf("tt数学: ");scanf("%d",&stu->sx);
printf("tt英语: ");scanf("%d",&stu->yy);
}
stu->next=phead->next;
phead->next=stu;
printf("tt学生信息已存入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("tt____________________________________________________n");
printf("tt|______________修改学生信息_________________________|n");
printf("tt|___________________________________________________|n");
printf("tt请输入要修改的学生学号:");
scanf("%d",&timp);
p=phead;
for(m=phead;m!=NULL;m=m->next)
{
if(m->num==timp)
{
printf("tt已经查到改学生的信息n");
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
printf("tt抱歉,没有查到该学生的信息n");
if(flag==0)
{
printf("tt该学生的原信息如下n");
printf("tt学号:%dn",m->num);
printf("tt姓名:%sn",m->name);
printf("tt语文:%dn",m->yw);
printf("tt数学:%dn",m->sx);
printf("tt英语:%dnn",m->yy);
printf("tt请重新输入该学生的信息n");
printf("tt学号: ");scanf("%d",&m->num);
if(m->num > 99999999||m->num < 10000000)
{
printf("tt______________________n");
printf("tt_请输入8位非负数__n");
printf("tt______________________n");
printf("tt重新输入学号: ");scanf("%d",&m->num);
}
printf("tt姓名: ");scanf("%s",m->name);
if(m->name[0] > 0)
{
printf("tt______________________n");
printf("tt______姓名请输入汉字__n");
printf("tt______________________n");
printf("tt重新输入姓名: ");scanf("%s",m->name);
}
printf("tt语文: ");scanf("%d",&m->yw);
printf("tt数学: ");scanf("%d",&m->sx);
printf("tt英语: ");scanf("%d",&m->yy);
if(m->sx<0||m->sx>100||m->yw<0||m->yw>100||m->yy<0||m->yy>100)
{
printf("tt______________________n");
printf("tt_成绩请输入0-100之间__n");
printf("tt______________________n");
printf("tt重新输入成绩n");
printf("tt语文: ");scanf("%d",&m->yw);
printf("tt数学: ");scanf("%d",&m->sx);
printf("tt英语: ");scanf("%d",&m->yy);
}
printf("tt该学生信息已修改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("tt____________________________________________________n");
printf("tt|______________学生成绩分析_________________________|n");
printf("tt|___________________________________________________|n");
printf("tt一共录入了%d个学生成绩n",n);
printf("tt语文及格人数为%dn",jyw);
printf("tt数学及格人数为%dn",jsx);
printf("tt英语及格人数为%dn",jyy);
printf("tt分析完毕n");
printf("退出请按0:");
}