C语言之学生档案管理.docx
《C语言之学生档案管理.docx》由会员分享,可在线阅读,更多相关《C语言之学生档案管理.docx(15页珍藏版)》请在冰豆网上搜索。
C语言之学生档案管理
/***********************一叶星尘S欢**********************/
#include
#include
#include
#include
#defineMAX500//定义储存容量
typedefstruct
{
intmonth,day,year。
//出生年月
}BD。
typedefstruct
{
intnumber。
//学号
charname[13]。
//姓名
charsex[7]。
//性别:
maleorfemale
BDbirth。
//出生年月MM-DD-YY
charaddr[35]。
//地址
}ST。
STstudent[MAX]。
//全局变量
/***********************函数声明**************************/
voidmenu_show()。
//显示菜单
voidmenu()。
//菜单响应
charget_menu_choice()。
//获取菜单选择信息
FILE*file_operate(char*mode)。
//文件操作模块
voidadd(FILE*fp)。
//添加学生信息
voidshow(FILE*fp)。
//显示学生信息
voidsearch(FILE*fp)。
//查找学生信息
voidmodify(FILE*fp)。
//修改学生信息
voiddel(FILE*fp)。
//删除学生信息
voidf_backup()。
//文件备份
voidpsw_check()。
//密码验证
voidset_psw()。
//设置密码
/************************主函数***************************/
intmain()
{
system("cls")。
psw_check()。
menu()。
return0。
}
/***********************显示菜单**************************/
voidmenu_show()
{
printf("\n\n")。
printf("\t+------------------------------------------+\n")。
printf("\t|学生档案管理系统|\n")。
printf("\t|------------------------------------------|\n")。
printf("\t|【1.输入数据】|【5.删除数据】|\n")。
printf("\t|【2.显示数据】|【6.备份数据】|\n")。
printf("\t|【3.查询数据】|【7.设置口令】|\n")。
printf("\t|【4.修改数据】|【0.退出】|\n")。
printf("\t+------------------------------------------+\n")。
}
/*********************菜单响应模块************************/
voidmenu()
{
while
(1)
{
menu_show()。
switch(get_menu_choice())
{
case'1':
add(file_operate("a"))。
system("pause")。
system("cls")。
break。
case'2':
show(file_operate("rb"))。
system("pause")。
system("cls")。
break。
case'3':
search(file_operate("rb"))。
system("pause")。
system("cls")。
break。
case'4':
modify(file_operate("rb"))。
system("pause")。
system("cls")。
break。
case'5':
del(file_operate("r"))。
system("pause")。
system("cls")。
break。
case'6':
f_backup()。
system("pause")。
system("cls")。
break。
case'7':
set_psw()。
system("pause")。
system("cls")。
break。
case'0':
printf("Thankyouforusethisprogram.\n")。
system("pause")。
system("cls")。
exit(0)。
//正常退出
}
}
}
/**********************接收菜单选择***********************/
charget_menu_choice()
{
charmenu_choice。
do
{
fflush(stdin)。
printf("Pleasechoice:
")。
scanf("%c",&menu_choice)。
if(menu_choice<'0'||menu_choice>'7')
puts("inputerror,tryagain.")。
}while(menu_choice<'0'||menu_choice>'7')。
returnmenu_choice。
}
/************************文件操作*************************/
FILE*file_operate(char*mode)
{
charchoise。
FILE*fp。
do
{
fflush(stdin)。
if((fp=fopen("d:
\student",mode))==NULL)//打开文件
{
puts("Fileoperationfailure")。
puts("TryAgain(y/n)?
")。
scanf("%c",&choise)。
}
}while(choise=='y'||choise=='Y')。
if(choise=='n'||choise=='N')
exit
(1)。
//非正常退出
returnfp。
}
/************************输入资料*************************/
voidadd(FILE*fp)
{
inti=0。
charchoice='y'。
do
{
printf("No.(11101040221):
")。
scanf("%d",&student[i].number)。
printf("Name(lessthan12character):
")。
scanf("%s",&student[i].name)。
printf("Sex(maleorfemale):
")。
scanf("%s",&student[i].sex)。
printf("Birthday:
\n")。
printf("year:
")。
scanf("%d",&student[i].birth.year)。
printf("month:
")。
scanf("%d",&student[i].birth.month)。
printf("day:
")。
scanf("%d",&student[i].birth.day)。
printf("Address:
")。
scanf("%s",student[i].addr)。
if(fwrite(&student[i],sizeof(ST),1,fp)!
=1)
puts("datawriteerror.")。
i++。
fflush(stdin)。
printf("Continue(y/n)?
")。
scanf("%c",&choice)。
}while(choice=='y'||choice=='Y')。
fclose(fp)。
}
/***********************查找信息**************************/
voidsearch(FILE*fp)
{
inti,m。
charsearch_name[20]。
//查找姓名变量
charchoice='y'。
for(i=0。
feof(fp)==0。
i++)//读取文件
{
if(fread(&student[i],sizeof(ST),1,fp)!
=1&&feof(fp)==0)
puts("error")。
}
m=i-1。
//m被赋值为数组非空数据最大索引值
do
{
fflush(stdin)。
puts("Enterthenameforsearching:
")。
gets(search_name)。
for(i=0。
ii++)//遍历数组
{
if(strcmp(search_name,student[i].name)==0)//判断是否有要查找的学生姓名,有则显示数据
{
printf("No.:
%dName:
%sSex:
%sBirth:
%d-%d-%dAddress:
%s\n",student[i].number,student[i].name,student[i].sex,
student[i].birth.year,student[i].birth.month,student[i].birth.day,student[i].addr)。
}
}
if(feof(fp)!
=0)
{
puts("searchingdone.")。
}
puts("continue(y/n)?
")。
scanf("%c",&choice)。
}while(choice=='y'||choice=='Y')。
fclose(fp)。
}
/***********************显示资料**************************/
voidshow(FILE*fp)
{
inti,m。
for(i=0。
feof(fp)==0。
i++)
{
if((fread(&student[i],sizeof(ST),1,fp))!
=1&&feof(fp)==0)
{
puts("error")。
}
}
m=i-1。
printf("There%dRecord:
\n",m)。
printf("NumberNameSexBirthAddress\n")。
for(i=0。
ii++)
{
printf("%-8d%-12s%-6s%-4d-%-2d-%-2d%-35s\n",student[i].number,student[i].name,student[i].sex,
student[i].birth.year,student[i].birth.month,student[i].birth.day,student[i].addr)。
}
if(feof(fp)!
=0)
{
puts("DisplayInformtionDone.")。
}
fclose(fp)。
}
/**********************修改资料***************************/
voidmodify(FILE*fp)
{
intnumber_temp。
charchoice。
inti,m,j=0。
FILE*fp_update。
for(i=0。
feof(fp)==0。
i++)//读取文件
if(fread(&student[i],sizeof(ST),1,fp)!
=1&&feof(fp)==0)
puts("error")。
m=i-1。
do
{
fflush(stdin)。
printf("Enternumberforchange:
")。
scanf("%d",&number_temp)。
for(i=0。
ii++)
if(number_temp==student[i].number)
{
j=i。
printf("No.:
%dName:
%sSex:
%sbirth:
%d-%d-%dAddress:
%s\n",student[i].number,student[i].name,student[i].sex,
student[i].birth.year,student[i].birth.month,student[i].birth.day,student[i].addr)。
}
if(j==0)
{
puts("Nothisrecord.")。
break。
}
fflush(stdin)。
printf("changedata%d(y/n)?
number_temp")。
//修改确认
scanf("%c",&choice)。
if(choice=='n'||choice=='N')//修改资料
continue。
printf("No.:
")。
scanf("%d",&student[j].number)。
printf("Name:
")。
scanf("%s",student[j].name)。
printf("Sex:
")。
scanf("%s",student[j].sex)。
printf("Birthday:
")。
printf("year:
")。
scanf("%d",&student[j].birth.year)。
printf("month:
")。
scanf("%d",&student[j].birth.month)。
printf("day:
")。
scanf("%d",&student[j].birth.day)。
printf("Address:
")。
scanf("%s",student[j].addr)。
fflush(stdin)。
printf("continue(y/n)?
")。
scanf("%c",&choice)。
}while(choice=='y'||choice=='Y')。
fp_update=file_operate("w")。
for(i=0。
ii++)
if(fwrite(&student[i],sizeof(ST),1,fp_update)!
=1)
puts("updateerror.")。
puts("changedone.")。
fclose(fp_update)。
}
/***********************删除资料**************************/
voiddel(FILE*fp)
{
inti,m,t=-1。
intnumber_temp。
charchoice='y'。
for(i=0。
feof(fp)==0。
i++)
{
if(fread(&student[i],sizeof(ST),1,fp)!
=1&&feof(fp)==0)
puts("error")。
}
m=i-2。
do
{
fflush(stdin)。
printf("Enterthenumberfordelete:
")。
scanf("%d",&number_temp)。
for(i=0。
ii++)
{
if(student[i].number==number_temp)//删除前显示学生资料
{
printf("No.:
%dName:
%sSex:
%sBrith:
%d-%d-%dAddress:
%s\n",student[i].number,student[i].name,
student[i].sex,student[i].birth.year,student[i].birth.month,student[i].birth.day,student[i].addr)。
fflush(stdin)。
printf("delete%d(y/n)?
")。
scanf("%c",&choice)。
if(choice=='n'||choice=='N')
break。
for(t=i。
tt++)
student[t]=student[t+1]。
//删除数据
puts("deletedatadone.")。
m--。
}
}
if(t==-1&&choice!
='n'&&choice!
='N')
printf("No%d'sdata!
\n",number_temp)。
fflush(stdin)。
printf("continue(y/n)?
")。
scanf("%c",&choice)。
}while(choice=='y'||choice=='Y')。
fclose(fp)。
fp=file_operate("w")。
for(i=0。
ii++)
{
if(fwrite(&student[i],sizeof(ST),1,fp)!
=1)
puts("error")。
}
fclose(fp)。
}
/***********************文件备份**************************/
voidf_backup()
{
FILE*fp,*fp_bak。
charch。
charfilename[312]。
//存放备份文件文件名
printf("Enterthebackupfilename:
")。
scanf("%s",filename)。
fp=file_operate("rb")。
fp_bak=fp。
if((fp_bak=fopen(filename,"wb"))==NULL)
{
puts("backupfileerror.")。
exit(0)。
}
while(!
feof(fp))
{
ch=fgetc(fp)。
fputc(ch,fp_bak)。
}
fclose(fp)。
fclose(fp_bak)。
puts("filebackupsuccess.")。
}
/************************密码验证*************************/
voidpsw_check()
{
charpsw[8],psw_temp[8]。
inti,leap。
FILE*fp。
if((fp=fopen("d:
\sn.txt","rt"))==NULL)//读取密码,如不成功则要求设置密码
{
puts("Youmustsetpasswordfirst.")。
menu()。
}
if((fread(psw,sizeof(psw),1,fp))!
=1)
puts("error.")。
for(i=0。
i<8。
i++)//解密
psw[i]=~psw[i]。
do
{
leap=0。
printf("Enterpassward:
")。
for(i=0。
i<8。
i++)
{
psw_temp[i]=getch()。
printf("*")。
if(psw_temp[i]==psw[i])
++leap。
}
if(leap!
=8)
{
puts("\nInvalidpassword!
")。
}
elseprintf("\n")。
fclose(fp)。
}while(leap!
=8)。
}
/**********************设置密码***************************/
voidset_psw()
{
charpsw[8],psw_temp[8]。
inti=0,leap=0。
FILE*fp。
do
{
printf("Enterpassword:
")。
//输入密码
for(i=0。
i<8。
i++)
{
fflush(stdin)。
psw[i]=getch()。
printf("*")。
}
printf("\nConfirmpassword:
")。
//再输入一次密码
for(i=0。
i<8。
i++)
{
fflush(stdin)。
psw_temp[i]=getch()。
printf("*")。
}
for(i=0。
i<8。
i++)//确认密码
if(psw[i]==psw_temp[i])
leap++。
if(leap==8)
{
printf("\nSetpasswordsuccess.\n")。
for(i=0。
i<8。
i++)
psw[i]=~psw[i]。
//密码加密
if((fp=fopen("d:
\sn.txt","wt"))==NULL)//密码存盘
puts("error.")。
if((fwrite(psw,sizeof(psw),1,fp))!
=1)
puts("Savepassworderror.")。
fclose(fp)。
}
elseprintf("\n")。
}while(leap!
=8)