c语言做的学生管理系统.docx
《c语言做的学生管理系统.docx》由会员分享,可在线阅读,更多相关《c语言做的学生管理系统.docx(49页珍藏版)》请在冰豆网上搜索。
c语言做的学生管理系统
/*
*Copyright(c)2012,陈长佑
*Allrightreserved
*
*文件名称:
log_menu.c
*文件标识:
*摘要:
本文件放置了登录函数,功能是登录到管理员主菜单或学生主菜单,
*管理员账户固定,登录前加载密码,学生登录前先加载学生信息,然后匹配学生用户名和密码。
*
*版本:
1.0
*作者:
陈长佑
*完成日期:
2012年8月4日
*/
#include"globle.h"
voidloger()//登录函数
{
intm=0;
inti=0;
inty=310909;//固定的管理员账户
charst[32];//把加载的管理员密码放到该数组中
charstr[32];//把输入密码放到该数组中
node_phead=create_headnode();//创建头结点
strcpy(st,load_manege_key());//75加载管理员密码
head=load_stu(head);//加载学生信息
for(i=0;i<3;i++)
{
printf("请输入用户名:
\n");
scanf("%d",&m);
getchar();
printf("请输入密码:
\n");
scanf("%s",str);
if(head==NULL)
{
exit
(1);
}
while((m==y)&&(strcmp(str,st)==0))//比较管理员用户名和密码
{
manege_menu(head);//进入管理员菜单
}
node_psp=head->next;
if(sp!
=NULL)
{
while(sp!
=NULL)
{
if((m==sp->stu.num)&&(strcmp(str,sp->stu.key)==0))//验证学生学号和密码
break;
sp=sp->next;
}
if(sp!
=NULL)
{
student_menu(head,m);//进入学生菜单
}
else
{
printf("系统不存在这用户!
\n");
}
}
if(i<2)
{
printf("输入密码或用户名错误,请在输入一次\n");
}
else
{
exit
(1);
}
}
return;
}
/*
*Copyright(c)2012,陈长佑
*Allrightreserved
*
*文件名称:
main.c
*文件标识:
*摘要:
本文件放置主函数和定义脏标记,功能是开始运行该程序。
*
*版本:
1.0
*作者:
陈长佑
*完成日期:
2012年8月4日
*/
#include"globle.h"
intflag=0;//脏标记
intmain()
{
loger();//登录函数
return0;
}
/*
*Copyright(c)2012,陈长佑
*Allrightreserved
*
*文件名称:
globle.h
*文件标识:
*摘要:
本文件放置管理员主菜单函数和各个子函数,功能是管理员能进行添加保存加载修改查询排序打印修改自身密码等操作。
*
*版本:
1.0
*作者:
陈长佑
*完成日期:
2012年8月4日
*/
#include"globle.h"
voidmanege_menu(node_phead)
{
inti=0;
intchoice;
while
(1)
{
system("clear");
printf("欢迎进入管理员界面\n");
printf("====================\n");
printf("1:
添加学生信息\n");
printf("2:
保存学生信息\n");
printf("3:
加载学生信息\n");
printf("4:
删除学生信息\n");
printf("5:
修改学生信息\n");
printf("6:
查询学生信息\n");
printf("7:
排序学生信息\n");
printf("8:
打印学生信息\n");
printf("9:
修改管理员密码\n");
printf("0:
退出\n");
printf("====================\n");
printf("请选择:
\n");
i=scanf("%d",&choice);
while(i==0)
{
printf("输入错误,请再次输入:
\n");
while(getchar()!
='\n');//清空键盘缓冲
i=scanf("%d",&choice);//再次输入选择
getchar();//去掉换行符
}
switch(choice)
{
case1:
add_stu(head);
break;
case2:
save_stu(head);
break;
case3:
load_stu(head);
break;
case4:
delete_stu(head);
break;
case5:
modify_stu(head);
break;
case6:
search_stu(head);
break;
case7:
sort_stu(head);
break;
case8:
printf_stu(head);
break;
case9:
modify_manege_key();
break;
case0:
quit_stu(head);
break;
default:
printf("输入不对,请重新输入:
");
break;
}
getchar();
}
}
node_pcreate_headnode()//创建一个头结点
{
node_phead;
head=(node_p)malloc(sizeof(node_t));
if(head==NULL)
{
exit
(1);
}
head->next=NULL;
returnhead;
}
node_pcreate_node()//创建空结点,无指向
{
node_pheadp;
headp=(node_p)malloc(sizeof(node_t));
if(headp==NULL)
{
exit
(1);
}
headp->next=NULL;
returnheadp;
}
node_pinput_data()//输入学生信息
{
node_pheadp;
staticintk;
k=add_num();
headp=create_node();
if(headp==NULL)
{
printf("内存分配失败\n");
}
headp->stu.num=k;
printf("自动生成学号\n");
printf("%d",headp->stu.num);
printf("请输入姓名\n");
scanf("%s",headp->stu.name);
getchar();
printf("请输入密码\n");
scanf("%s",headp->stu.key);
getchar();
printf("请输入年龄\n");
if(headp->stu.age<0&&headp->stu.age>150)
{
printf("输入");
}
/*
else
{
printf("请输入学生名字密码年龄班级\n");
scanf("%s%s%d%d",headp->stu.name,headp->stu.key,&headp->stu.age,&headp->stu.classes);
getchar();
printf("请输入数学语文c语言成绩:
\n");
scanf("%d%d%d",&headp->stu.grade.math,&headp->stu.grade.chinese,&headp->stu.grade.c);
}*/
k++;
returnheadp;
}
intadd_num()//学号自增函数
{
inta=301;
intb,c,d,t;
printf("请设置学号的年级班级第一个学生的编号\n");
scanf("%d%d%d",&b,&c,&d);
getchar();
t=a*1000000+b*10000+c*100+d;
returnt;
}
node_pinsert_node(node_phead,node_pheadp)//把结点插入到链表中去
{
headp->next=head->next;
head->next=headp;
returnhead;
}
node_padd_stu(node_phead)//增加学生信息
{
intn;
inti;
node_pheadp;
printf("输入学生人数\n");
scanf("%d",&n);
getchar();
if(head==NULL)
{
exit
(1);
}
for(i=0;i{
headp=input_data(i);
head=insert_node(head,headp);//头部插入
}
printf("添加学生信息成功\n");
getchar();
flag=1;
returnhead;
}
node_psave_stu(node_phead)//保存学生信息
{
FILE*fp;
if(head==NULL)
{
exit
(1);
}
if((fp=fopen("stb","wt"))==NULL)
{
printf("不能打开文件\n");
exit
(1);
}
node_pheadp=head->next;
while(headp!
=NULL)
{
fwrite(&headp->stu,sizeof(STU),1,fp);
headp=headp->next;
}
printf("保存学生信息成功\n");
fclose(fp);
flag=0;
getchar();
returnhead;
}
node_pload_stu(node_phead)//加载学生信息
{
FILE*fp;
if(head==NULL)
{
exit
(1);
}
node_pp=head->next;
while(p!
=NULL)
{
head->next=p->next;
free(p);
p->next=NULL;
p=head->next;
}
if((fp=fopen("stb","rt"))==NULL)
{
printf("不能打开文件\n");
exit
(1);
}
node_pheadp=create_node();
while(fread(&headp->stu,sizeof(STU),1,fp)==1)
{
printf("学号=%d,姓名=%s,密码=%s,年龄=%d,班级=%d\n",headp->stu.num,headp->stu.name,headp->stu.key,headp->stu.age,headp->stu.classes);
printf("数学=%d,语文=%d,c语言=%d\n",headp->stu.grade.math,headp->stu.grade.chinese,headp->stu.grade.c);
head=insert_node(head,headp);
headp=create_node();
}
fclose(fp);
flag=0;
printf("加载学生信息成功\n");
getchar();
returnhead;
}
node_pdelete_stu(node_phead)//删除学生信息
{
intn;
node_pheadp;
if(head==NULL)
{
exit
(1);
}
headp=head->next;
if(headp==NULL)
{
exit
(1);
}
printf("请输入删除学生的学号:
\n");
scanf("%d",&n);
getchar();
if(headp!
=NULL)
{
while(headp!
=NULL)
{
if(n==headp->stu.num)
break;
head=head->next;
headp=headp->next;
}
if(headp!
=NULL)
{
head->next=headp->next;
free(headp);
headp->next=NULL;
printf("删除学生信息成功\n");
flag=1;
}
else
{
printf("系统不存在这用户!
\n");
}
}
returnhead;
}
node_pmodify_stu(node_phead)//修改学生信息
{
inti;
intchoice;
while
(1)
{
system("clear");
printf("====================\n");
printf("1:
修改学生成绩\n");
printf("2:
修改学生姓名\n");
printf("3:
修改学生班级\n");
printf("0:
返回上一级菜单\n");
printf("====================\n");
printf("请选择:
\n");
i=scanf("%d",&choice);
while(i==0)
{
printf("输入错误,请重新输入:
\n");
while(getchar()!
='\n');//清空键盘缓冲
i=scanf("%d",&choice);
getchar();//去掉换行符
}
switch(choice)
{
case1:
modify_score(head);
break;
case2:
modify_name(head);
break;
case3:
modify_classes(head);
break;
case0:
returnhead;
default:
printf("输入不对,请重新输入:
");
break;
}
getchar();
}
flag=1;
returnhead;
}
voidsearch_stu(node_phead)
{
inti;
intchoice;
while
(1)
{
system("clear");
printf("====================\n");
printf("1:
按姓名查询学生信息\n");
printf("2:
按班级查询学生信息\n");
printf("3:
按总成绩排名查询学生信息\n");
printf("0:
返回上一级菜单\n");
printf("====================\n");
printf("请选择:
\n");
i=scanf("%d",&choice);
while(i==0)
{
printf("输入错误,请重新输入:
\n");
while(getchar()!
='\n');//清空键盘缓冲
i=scanf("%d",&choice);//
getchar();//去掉换行符
}
switch(choice)
{
case1:
name_search(head);
break;
case2:
classes_search(head);
break;
case3:
total_sort_output(head);
break;
case0:
return;
default:
printf("输入不对,请重新输入:
");
break;
}
getchar();
flag=0;
}
return;
}
voidsort_stu(node_phead)
{
inti;
intchoice;
while
(1)
{
system("clear");
printf("====================\n");
printf("1:
根据总成绩显示学生信息\n");
printf("2:
根据排名显示学生信息\n");
printf("3:
根据姓名排名显示学生信息\n");
printf("0:
返回上一级菜单\n");
printf("====================\n");
printf("请选择:
\n");
i=scanf("%d",&choice);
while(i==0)
{
printf("输入错误,请重新输入:
\n");
while(getchar()!
='\n');//清空键盘缓冲
i=scanf("%d",&choice);//
getchar();//去掉换行符
}
switch(choice)
{
case1:
total_sort_output(head);
break;
case2:
rank_output(head);
break;
case3:
name_sort_output(head);
break;
case0:
return;
default:
printf("输入不对,请重新输入:
");
break;
}
getchar();
flag=0;
}
return;
}
voidprintf_stu(node_phead)//打印学生信息
{
if(head==NULL)
{
exit
(1);
}
node_pheadp=head->next;
while(headp!
=NULL)
{
printf("学号=%d,姓名=%s,密码=%s,年龄=%d,班级=%d\n",headp->stu.num,headp->stu.name,headp->stu.key,headp->stu.age,headp->stu.classes);
printf("数学=%d,语文=%d,c语言=%d\n",headp->stu.grade.math,headp->stu.grade.chinese,headp->stu.grade.c);
headp=headp->next;
}
getchar();
return;
}
voidquit_stu(node_phead)//退出管理员菜单函数
{
if(head==NULL)
{
exit
(1);
}
if(flag==0)
{
exit
(1);
}
if(flag==1)
{
printf("是否保存信息y或n:
\n");
getchar();
if(getchar()=='y'||getchar()=='Y')
{
save_stu(head);
exit
(1);
}
if('n'==getchar()||'N'==getchar())
{
exit
(1);
}
}
return;
}
voidsave_manege_key()//保存管理员密码
{
FILE*fp;
charst[32]={"abcde"};
if((fp=fopen("sts","wt"))==NULL)
{
printf("不能打开文件\n");
exit
(1);
}
fprintf(fp,"%s",st);
rewind(fp);
fclose(fp);
printf("保存管理员密码成功\n");
getchar();
return;
}
char*load_manege_key()//加载管理员密码
{
FILE*fp;
staticcharst[32]={};
if((fp=fopen("sts","rt"))==NULL)
{
printf("不能打开文件\n");
exit
(1);
}
fscanf(fp,"%s",st);
rewind(fp);
fclose(fp);
printf("加载管理员密码成功\n");
returnst;
}
voidmodify_manege_key()//修改管理员密码
{
FILE*fp;
staticcharsnew[32]={};
staticchargnew[32]={};
while
(1)
{
printf("请输入管理员新密码:
\n");
scanf("%s",snew);
getchar();
printf("请再次输入管理员新密码:
\n");
scanf("%s",gnew);
if(strcmp(snew,gnew)==0)
{
if((fp=fopen("sts","wt"))==NULL)
{
printf("不能打开文件\n");
exit
(1);
}
fprintf(fp,"%s",gnew);
rewind(fp);
fclose(fp);