C语言班级信息管理系统.docx
《C语言班级信息管理系统.docx》由会员分享,可在线阅读,更多相关《C语言班级信息管理系统.docx(16页珍藏版)》请在冰豆网上搜索。
![C语言班级信息管理系统.docx](https://file1.bdocx.com/fileroot1/2022-12/1/bce30dd1-5dd2-4d9b-9dad-ea80755e67cc/bce30dd1-5dd2-4d9b-9dad-ea80755e67cc1.gif)
C语言班级信息管理系统
C语言班级信息管理系统
#include
#include
#include
#include
#defineNULL0
#defineLENsizeof(structstudent)
#define"c:
\student.txt"
structstudent
{
longintnum;
charname[20];
intage;
charsex[4];
charbirthday[10];
charaddress[30];
longinttele_num;
charE_mail[20];
structstudent*next;
};
intTOTAL_NUM=0;
structstudent*head=NULL;
voidwelcome();
voidmainmenu();
voidrecord();
voidinsert(structstudent*stu);
voiddisplay(structstudent*stu);
voiddisplayAll();
voidquery();
voidquery_by_num();
voidquery_by_name();
voidreadData();
voidwriteData();
voidfreeAll();
voiddel();
voidchange();
voiddevise(structstudent*p);
intmain()
{
charuserName[9];
charuserPWD[7];
inti;
welcome();
for(i=0;i<3;i++)
{
printf("\n管理员初始用户名和密码均为admin\n");
printf("请输入您的用户名:
");
scanf("%s",userName);
printf("\n请输入您的密码:
");
scanf("%s",userPWD);
if((strcmp(userName,"admin")==0)&&(strcmp(userPWD,"admin")==0))
{
//用户名和密码正确,显示主菜单
mainmenu();
break;
}
else
{
if(i<2)
{
//用户名或密码错误,提示用户重新输入
printf("用户名或密码错误,请重新输入!
");
}
else
{
//连续3次输错用户名或密码,退出系统。
printf("您已连续3次将用户名或密码输错,系统将退出!
");
}
}
}
return0;
}
//显示欢迎信息
voidwelcome()
{
printf("+---------------------------+\n");
printf("||\n");
printf("|欢迎使用学生信息管理系统|\n");
printf("||\n");
printf("+---------------------------+\n");
}
//系统主菜单
voidmainmenu()
{
intchoice;
choice=-1;
readData();
printf("\n温馨提示:
为保证您的操作得到保存,请按正常顺序退出系统^_^\n");
do
{
printf("\n\n\n");
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+[0]----退出系统|\n");
printf("\t\t\t+*·*·*·*·*·*·*·*·*·*·|\n");
printf("\t\t\t--------------------------------\n");
printf("请输入您的选择:
");
scanf("%d",&choice);
switch(choice)
{
case0:
writeData();
freeAll();
exit(0);
case1:
record();
break;
case2:
displayAll();
break;
case3:
query();
break;
case4:
del();
break;
case5:
change();
break;
default:
printf("\n无效选项!
");
break;
}
}
while(choice!
=0);
}
//录入学生信息
voidrecord()
{
structstudent*p0;
p0=(structstudent*)malloc(LEN);
printf("请输入学生的学号:
");
scanf("%ld",&p0->num);
printf("请输入学生的姓名:
");
scanf("%s",p0->name);
printf("请输入学生的年龄:
");
scanf("%d",&p0->age);
printf("请输入学生的性别:
");
scanf("%s",p0->sex);
printf("请输入学生的出生年月:
");
scanf("%s",p0->birthday);
printf("请输入学生的地址:
");
scanf("%s",p0->address);
printf("请输入学生的电话:
");
scanf("%ld",&p0->tele_num);
printf("请输入学生的E-mail:
");
scanf("%s",p0->E_mail);
insert(p0);
printf("该学生的信息为:
\n");
printf("-------------------------------------------------------------------------------");
printf("学号\t姓名\t年龄\t性别\t出生年月\t\t地址\t电话\tE-mail\n");
display(p0);
}
voidinsert(structstudent*stu)
{
structstudent*p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!
=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
TOTAL_NUM++;
}
voiddisplay(structstudent*p)
{
printf("%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",p->num,p->name,p->age,p->sex,p->birthday,p->address,p->tele_num,p->E_mail);
}
//浏览学生信息
voiddisplayAll()
{
structstudent*p;
printf("学生总数:
%d\n",TOTAL_NUM);
p=head;
if(head!
=NULL)
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
do
{
display(p);
p=p->next;
}
while(p!
=NULL);
}
printf("\n");
}
//查询学生信息
voidquery()
{
intchoice;
choice=-1;
do
{
printf("\n");
printf("+--------------------+\n");
printf("|按学号查询请按1|\n");
printf("|按姓名查询请按2|\n");
printf("|取消请按0|\n");
printf("+--------------------+\n");
printf("请输入您的选择:
");
scanf("%d",&choice);
switch(choice)
{
case0:
return;
case1:
query_by_num();
break;
case2:
query_by_name();
break;
default:
printf("\n无效选项!
");
break;
}
}
while(choice!
=0);
}
//按学号查询学生信息
voidquery_by_num()
{
intnum;
structstudent*p1;
printf("请输入学生的学号:
");
scanf("%ld",&num);
if(head==NULL)
{
printf("无学生记录!
\n");
return;
}
p1=head;
while(num!
=p1->num&&p1->next!
=NULL)
p1=p1->next;
if(num==p1->num)
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("没有该学生记录,请核对!
");
}
//按姓名查询学生信息
voidquery_by_name()
{
charname[20];
structstudent*p1;
printf("请输入学生的姓名:
");
scanf("%s",name);
if(head==NULL)
{
printf("无学生记录!
\n");
return;
}
p1=head;
while(strcmp(name,p1->name)&&p1->next!
=NULL)
p1=p1->next;
if(!
strcmp(name,p1->name))
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("没有该学生记录,请核对!
");
}
//写入文件
voidwriteData()
{
FILE*fp;//文件指针
structstudent*p;
fp=fopen(,"w");
if(!
fp)
{
printf("文件打开错误!
");
return;
}
fprintf(fp,"%d\n",TOTAL_NUM);
for(p=head;p!
=NULL;p=p->next)
{
fprintf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
p->num,p->name,p->age,p->sex,p->birthday,p->address,p->tele_num,p->E_mail);
}
fclose(fp);
}
voidfreeAll()
{
structstudent*p1,*p2;
p1=p2=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}
//读取文件
voidreadData()
{
FILE*fp;//文件指针
structstudent*p1,*p2;
fp=fopen(,"r");
if(!
fp)
{
printf("文件打开错误!
");
return0;
}
fscanf(fp,"%d\n",&TOTAL_NUM);
head=p1=p2=(structstudent*)malloc(LEN);
fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
&p1->num,p1->name,&p1->age,p1->sex,p1->birthday,p1->address,&p1->tele_num,p1->E_mail);
while(!
feof(fp))
{
p1=(structstudent*)malloc(LEN);
fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
&p1->num,p1->name,&p1->age,p1->sex,p1->birthday,p1->address,&p1->tele_num,p1->E_mail);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
fclose(fp);
}
//删除学生信息
voiddel()
{
structstudent*p1,*p2;
longintnum;
if(head==NULL)
{
printf("无学生记录!
\n");
return;
}
printf("请输入您要删除的学生的学号:
");
scanf("%ld",&num);
p1=head;
while(num!
=p1->num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
elsep2->next=p1->next;
free(p1);
TOTAL_NUM--;
}
else
printf("没有该学生记录,请核对!
\n");
}
//修改学生信息
voidchange()
{
structstudent*p1,*p2;
longintnum;
if(head==NULL)
{
printf("无学生记录!
\n");
return;
}
printf("请输入您要修改的学生的学号:
");
scanf("%ld",&num);
p1=head;
while(num!
=p1->num&&p1->next!
=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
devise(p1);
else
printf("没有该学生记录,请核对!
\n");
}
voiddevise(structstudent*p)
{
intchoice;
choice=-1;
do
{
printf("请选择您要修改的学生的信息内容:
\n");
printf("+----------------------+\n");
printf("|姓名请按1|\n");
printf("|年龄请按2|\n");
printf("|性别请按3|\n");
printf("|出生年月请按4|\n");
printf("|地址请按5|\n");
printf("|电话请按6|\n");
printf("|E-mail请按7|\n");
printf("|取消请按0|\n");
printf("+----------------------+\n");
printf("请输入您的选择:
");
scanf("%d",&choice);
switch(choice)
{
case0:
return;
case1:
printf("请输入新姓名:
");
scanf("%s",p->name);
break;
case2:
printf("请输入新年龄:
");
scanf("%d",&p->age);
break;
case3:
printf("请输入新性别:
");
scanf("%s",p->sex);
break;
case4:
printf("请输入新出生年月:
");
scanf("%s",p->birthday);
break;
case5:
printf("请输入新地址:
");
scanf("%s",p->address);
break;
case6:
printf("请输入新电话:
");
scanf("%ld",&p->tele_num);
break;
case7:
printf("请输入新E-mail:
");
scanf("%s",p->E_mail);
break;
default:
printf("\n无效选项!
");
break;
}
}
while(choice!
=0);
}