代码c语言 银行管理Microsoft Word 文档.docx
《代码c语言 银行管理Microsoft Word 文档.docx》由会员分享,可在线阅读,更多相关《代码c语言 银行管理Microsoft Word 文档.docx(41页珍藏版)》请在冰豆网上搜索。
代码c语言银行管理MicrosoftWord文档
/****************************************************
*银行管理系统*
**
*使用注意:
1.修改FILEPATH路径*
*2.可以将db.txt中内容删掉*
*3.本系统默认的管理员:
账户密码*
*123admin123*
*****************************************************/
#include
#include
#include
#defineID_START10000
#definePS_NUMBER100
#definePWD_RESET"123456"
#defineFILEPATH"F:
\\MyC\\db.txt"
structperson{
intid;
charname[20];
charpwd[6];
floatmoney;
intstate;
}p,ps[PS_NUMBER];
intps_num;//ps中元素的实际数量
voidinputPerson();
voidwriteIntoFile();
voidmainMenu();
voidpersonalMenu();
voidadminMenu();
voidqueryPerson();
/*账户id创建*/
voidmakeId(){
intlastId=ID_START;
FILE*fp=fopen(FILEPATH,"r");
fseek(fp,0,SEEK_END);
if(ftell(fp)!
=0){
fseek(fp,-46,SEEK_CUR);
fscanf(fp,"%d",&lastId);
lastId++;
}
fclose(fp);
p.id=lastId;
printf("创建账户成功,账户为%d\n",p.id);
}
/*是否继续开户控制*/
voidcreateContinue(){
charifContinue;
printf("\n是否继续创建账户?
n(否)\\y(是):
");
fflush(stdin);
scanf("%c",&ifContinue);
if(ifContinue=='y'){
inputPerson();
}else{
if(ifContinue=='n'){
mainMenu();
}else{
printf("输入错误,请重新输入...\n");
createContinue();
}
}
}
/*开户信息输入*/
voidinputPerson(){
system("cls");
p.money=0.0f;//floatdouble类型会出错,必须先初始化一个值,才能使用输入
printf("请输入用户的信息:
\n");
printf("姓名:
");
scanf("%s",p.name);
if(strlen(p.name)>6){
printf("姓名过长,请重新输入...\n");
inputPerson();
}
makeId();
charpassCheck[6];
printf("密码:
");
scanf("%s",p.pwd);
printf("确认密码:
");
scanf("%s",passCheck);
while(strcmp(p.pwd,passCheck)!
=0){
printf("两次密码输入不一致,请重新输入...\n");
printf("密码:
");
scanf("%s",p.pwd);
printf("确认密码:
");
scanf("%s",passCheck);
}
if(strlen(p.pwd)>6){
printf("密码过长,请重新输入...\n");
inputPerson();
}
printf("存入的金额:
");
scanf("%f",&p.money);
p.state=1;
writeIntoFile();
createContinue();
}
/*保存一条记录*/
voidwriteIntoFile(){
FILE*fp=fopen(FILEPATH,"r+");
fseek(fp,0,SEEK_END);
fprintf(fp,"%-10d",p.id);
fprintf(fp,"%-10s",p.name);
fprintf(fp,"%-10s",p.pwd);
fprintf(fp,"%-13.2f",p.money);
fprintf(fp,"%-1d",p.state);
fprintf(fp,"%s","\n");
fclose(fp);
}
/*读取所有记录*/
voidreadAllFromFile(){
FILE*fp=fopen(FILEPATH,"r");
//ps的实际大小
fseek(fp,0,SEEK_END);
if(ftell(fp)/46ps_num=ftell(fp)/46;
}else{
ps_num=PS_NUMBER;
}
//读取数据到ps
fseek(fp,0,SEEK_SET);
for(inti=0;ifseek(fp,46*i,SEEK_SET);
fscanf(fp,"%d",&ps[i].id);
fseek(fp,46*i+10,SEEK_SET);
fscanf(fp,"%s",ps[i].name);
fseek(fp,46*i+20,SEEK_SET);
fscanf(fp,"%s",ps[i].pwd);
fseek(fp,46*i+30,SEEK_SET);
fscanf(fp,"%f",&ps[i].money);
fseek(fp,46*i+43,SEEK_SET);
fscanf(fp,"%d",&ps[i].state);
}
fclose(fp);
}
/*登陆查找控制返回值:
1-正确2-账户或密码错误3-账户停用*/
intfindPerson(intid,charpwd[6]){
FILE*fp=fopen(FILEPATH,"r");
//得到db.txt中记录条数
fseek(fp,0,SEEK_END);
intnumInTXT=ftell(fp)/46;
//查找该id的记录
fseek(fp,0,SEEK_SET);
for(inti=0;iintidFind;
charpwdFind[6];
intstate;
fseek(fp,46*i,SEEK_SET);
fscanf(fp,"%d",&idFind);
fseek(fp,46*i+20,SEEK_SET);
fscanf(fp,"%s",pwdFind);
fseek(fp,46*i+43,SEEK_SET);
fscanf(fp,"%d",&state);
if(idFind==id&&(!
strcmp(pwd,pwdFind))){
if(state==1){
p.id=idFind;
strcpy(p.pwd,pwdFind);
fseek(fp,46*i+10,SEEK_SET);
fscanf(fp,"%s",p.name);
fseek(fp,46*i+30,SEEK_SET);
fscanf(fp,"%f",&p.money);
fclose(fp);
return1;
}else{
fclose(fp);
return3;
}
}
}
fclose(fp);
return2;
}
/*显示所有账户列表*/
voidshowAllPerson(){
printf("\n共%d条记录:
\n",ps_num);
printf("\t账户\t\t姓名\t\t密码\t\t存款\t\t状态\n");
for(inti=0;iprintf("\t%d\t\t%s\t\t%s\t\t%.2f\t\t%d\n",ps[i].id,ps[i].name,ps[i].pwd,ps[i].money,ps[i].state);
}
}
/*显示账户余额*/
voidshowRestMoney(){
system("cls");
chargetCh;
printf("您的账户余额是:
%.2f\n",p.money);
printf("按\"n\"退出,按其他键返回:
");
fflush(stdin);
scanf("%c",&getCh);
if(getCh=='n'){
exit(0);
}else{
personalMenu();
}
}
/*更新存款到记录*/
voidupdateMoney(intid,floatmoney){
FILE*fp=fopen(FILEPATH,"r+");
//得到db.txt中记录条数
fseek(fp,0,SEEK_END);
intnumInTXT=ftell(fp)/46;
//查找该id的记录
fseek(fp,0,SEEK_SET);
for(inti=0;iintidFind;
fseek(fp,46*i,SEEK_SET);
fscanf(fp,"%d",&idFind);
if(idFind==id){
fseek(fp,46*i+30,SEEK_SET);
fprintf(fp,"%-13.2f",money);
break;
}
}
fclose(fp);
}
/*存款*/
voidsaveMoney(){
system("cls");
floatmoney;
printf("输入您要存入的金额:
");
fflush(stdin);
scanf("%f",&money);
if(money>0){
p.money+=money;
updateMoney(p.id,p.money);
printf("存款成功...\n");
personalMenu();
}else{
printf("输入金额错误,请重新输入...\n");
saveMoney();
}
}
/*检查本账户减少金额时余额是否足够*/
boolcheckMoney(intid,floatmoney){
FILE*fp=fopen(FILEPATH,"r");
//得到db.txt中记录条数
fseek(fp,0,SEEK_END);
intnumInTXT=ftell(fp)/46;
//查找该id的记录
fseek(fp,0,SEEK_SET);
for(inti=0;iintidFind;
fseek(fp,46*i,SEEK_SET);
fscanf(fp,"%d",&idFind);
if(idFind==id){
floatmoneyFind;
fseek(fp,46*i+30,SEEK_SET);
fscanf(fp,"%f",&moneyFind);
fclose(fp);
if(moneyFind>=money){
returntrue;
}else{
returnfalse;
}
}
}
fclose(fp);
returnfalse;
}
/*取款*/
voidgetMoney(){
system("cls");
floatmoney;
printf("输入您要取出的金额:
");
fflush(stdin);
scanf("%f",&money);
if(money>0){
if(checkMoney(p.id,money)){
p.money-=money;
updateMoney(p.id,p.money);
printf("取款成功...\n");
personalMenu();
}else{
printf("本账户金额不足,请重新输入...\n");
getMoney();
}
}else{
printf("输入金额错误,请重新输入...\n");
getMoney();
}
}
/*查找要转入的账户*/
floatfindById(intid){
FILE*fp=fopen(FILEPATH,"r");
//得到db.txt中记录条数
fseek(fp,0,SEEK_END);
intnumInTXT=ftell(fp)/46;
//查找该id的记录
fseek(fp,0,SEEK_SET);
for(inti=0;iintidFind;
fseek(fp,46*i,SEEK_SET);
fscanf(fp,"%d",&idFind);
if(idFind==id){
floatmoney;
fseek(fp,46*i+30,SEEK_SET);
fscanf(fp,"%f",&money);
fclose(fp);
returnmoney;
}
}
fclose(fp);
return-1;
}
/*转账*/
voidsendMoney(){
system("cls");
intid;
floatmoney;
printf("请输入要转入的账户:
");
intret=scanf("%d",&id);
if(ret==1){
printf("请输入要转入的金额:
");
fflush(stdin);
scanf("%f",&money);
if(money>0){
floatsenderMoney=findById(id);
if(senderMoney==-1){
printf("转入账户错误,请重新输入...\n");
sendMoney();
}else{
if(checkMoney(p.id,money)){
//本账户减少金额
p.money-=money;
updateMoney(p.id,p.money);
//转入账户增加金额
senderMoney+=money;
updateMoney(id,senderMoney);
printf("转账成功...\n");
personalMenu();
}else{
printf("本账户金额不足,请重新输入...\n");
sendMoney();
}
}
}else{
printf("输入金额错误,请重新输入...\n");
sendMoney();
}
}else{
chargetCh;
printf("账户格式错误,只能是数字!
按\"n\"返回,按其他键继续:
");
fflush(stdin);
scanf("%c",&getCh);
if(getCh=='n'){
personalMenu();
}else{
sendMoney();
}
}
}
/*个人服务界面*/
voidpersonalMenu(){
system("cls");
printf("\n***************************个人服务***************************\n");
printf("\n");
printf("1.存款\n");
printf("2.取款\n");
printf("3.查询余额\n");
printf("4.转账\n");
printf("5.退出\n");
printf("\n");
printf("****************************************************************\n");
intchoose;
printf("\n请选择服务:
");
scanf("%d",&choose);
switch(choose){
case1:
saveMoney();
break;
case2:
getMoney();
break;
case3:
showRestMoney();
break;
case4:
sendMoney();
break;
case5:
exit(0);
default:
printf("服务选择错误!
请重新选择...\n");
personalMenu();
}
}
/*按下标交换结构数组的元素*/
voidswap(inti,intj){
intid;
charname[20];
charpwd[6];
floatmoney;
intstate;
id=ps[i].id;
ps[i].id=ps[j].id;
ps[j].id=id;
strcpy(name,ps[i].name);
strcpy(ps[i].name,ps[j].name);
strcpy(ps[j].name,name);
strcpy(pwd,ps[i].pwd);
strcpy(ps[i].pwd,ps[j].pwd);
strcpy(ps[j].pwd,pwd);
money=ps[i].money;
ps[i].money=ps[j].money;
ps[j].money=money;
state=ps[i].state;
ps[i].state=ps[j].state;
ps[j].state=state;
printf("交换%d与%d\n",i,j);
}
/*快速排序*/
voidquickSort(intstartPos,intendPos){
inti=startPos,j=endPos,ch_index=startPos;
for(;ifor(;j>i;j--){
if(ps[j].moneyswap(j,ch_index);
ch_index=j;
break;
}
}
for(;iif(ps[i].money>ps[ch_index].money){
swap(i,ch_index);
ch_index=i;
break;
}
}
}
if(ch_index-1>startPos)quickSort(startPos,ch_index-1);
if(endPos>ch_index+1)quickSort(ch_index+1,endPos);
}
/*账户列表*/
voidlistPerson(){
system("cls");
readAllFromFile();
printf("选择是按账户升序还是按金额升序显示(账户请按\"1\",金额请按\"2\"):
");
chargetSort;
fflush(stdin);
scanf("%c",&getSort);
if(getSort=='2'&&ps_num!
=0){
quickSort(0,ps_num-1);
}
showAllPerson();
chargetCh;
printf("\n按\"n\"退出,按其他键返回:
");
fflush(stdin);
scanf("%c",&getCh);
if(getCh=='n'){
exit(0);
}else{
adminMenu();
}
}
/*按条件查找type:
1-按id2-按姓名3-按=金额4-按>金额5-按<金额6-按>=金额7-按<=金额*/
voidfindByQuery(intid,charname[20],floatmoney,inttype){
ps_num=0;
FILE*fp=fopen(FILEPATH,"r");
//得到db.txt中记录条数
fseek(fp,0,SEEK_END);
intnumInTXT=ftell(fp)/46;
//查找该id的记录
fseek(fp,0,SEEK_SET);
for(inti=0;iboolifFind=false;
intidFind;
charnameFind[20];
floatmoneyFind;
switch(type){
case1:
fseek(fp,46*i,SEEK_SET);
fscanf(fp,"%d",&idFind);
if(idFind==id){
ifFind=true;
}
break;
case2:
fseek(fp,46*i+10,SEEK_SET);
fscanf(fp,"%s",nameFind);
if(!
strcmp(name,nameFind)){
ifFind=true;
}
break;
case3:
fseek(fp,46*i+30,SEEK_SET);
fscanf(fp,