#include <stdio.h> #include "银行.h" int main() { InitEnvironmen(); while(1) { switch(printHead()) { case 49: NewUser();break; case 50: Deposit();break; case 51: Draw();break; case 52: Query();break; case 53: CheckHistory();break; case 54: EXIT();break; default: printf("没有对应的操作……");break; } system("pause"); } return 0; } #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> #include <string.h> #define FailureBeep() _beep(2000,300) #define SucceedBeep() _beep(600,500) #define NameLength 20 #define HistoryLength 500 #define DEPOSIT 0 #define DRAW 1 #define MAX 200 static int UserCont; time_t t; struct tm *p; typedef struct user { char name[NameLength] ; unsigned int userAccount; double blance; struct date { int year,month,day; }date; char password[36]; struct history { int year,month,day; int hour,minute,second; double money,blance; int Operation; }userHistory[HistoryLength]; int HistoryTop; }user; user * AllUser[MAX],* BadPassword; void InitEnvironmen() { FILE *DataFile; int i; i=0; system("mode con:lines=30 cols=71"); system("title 银行系统@Jokers 版权所有 翻版必究"); system("color 9e"); DataFile = fopen("Data.jo","rb"); if(DataFile==NULL) { DataFile=fopen("Data.jo","wb"); fwrite(&UserCont,sizeof(int),1,DataFile); fclose(DataFile); return ; } else { fread(&UserCont,sizeof(int),1,DataFile); while(i<UserCont&&!feof(DataFile)) { AllUser[i]=(user*)malloc(sizeof(user)); if(fread(AllUser[i],sizeof(user),1,DataFile)<1) { printf("读取文件失败。/n"); return ; } if(i<UserCont) i++; else { fclose(DataFile); return ; } } return ; } } int printHead() { int length=70; int i; system("cls"); system("mode con:lines=30 cols=80"); printf(" "); for(i=0;i<length;i++) printf("*"); puts("");printf(" "); printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*");printf(" "); printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*");printf(" "); puts("*/t/t ①开户 ②存款 ③取款 ④查询 ⑤交易记录 ⑥退出 *"); printf(" "); printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*");printf(" "); printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*");printf(" "); for(i=0;i<length;i++) printf("*"); puts(""); printf("请按下你想要进行的操作的数字键:"); return getche(); } int printHead2() { int length=53; int i; system("cls"); system("mode con:lines=30 cols=64"); printf(" "); for(i=0;i<length;i++) printf("*"); puts(""); printf(" ");printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*"); printf(" ");printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*"); printf(" ");puts("*/t/t ①按账号 ②按账户名 /t/t *"); printf(" ");printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*"); printf(" ");printf("*"); for(i=0;i<length-2;i++) printf(" "); puts("*");printf(" "); for(i=0;i<length;i++) printf("*"); puts(""); printf("请按下你想要进行的操作的数字键:"); return getche(); } void MakeHistory(user * user,tm * ptime,char operation,double money) { user->userHistory[user->HistoryTop].year = ptime->tm_year+1900; user->userHistory[user->HistoryTop].month = ptime->tm_mon+1; user->userHistory[user->HistoryTop].day = ptime->tm_mday; user->userHistory[user->HistoryTop].hour = ptime->tm_hour; user->userHistory[user->HistoryTop].minute = ptime->tm_min; user->userHistory[user->HistoryTop].second = ptime->tm_sec; user->userHistory[user->HistoryTop].Operation = operation; user->userHistory[user->HistoryTop].money=money; if(operation==DRAW) user->userHistory[user->HistoryTop].blance=user->blance; else if(operation==DEPOSIT) user->userHistory[user->HistoryTop].blance=user->blance; if(user->HistoryTop<HistoryLength) user->HistoryTop++; else user->HistoryTop=0; } void NewUser() { user * pUser = (user *)malloc(sizeof(user)); int passch,pn=0; UserCont++; printf("请输入用户名:"); scanf("%s",pUser->name); printf("请输入您的账户初始密码:"); while((passch=getch())!=13) { pUser->password[pn++]=passch; printf("*"); } pUser->password[pn]='/0'; pUser->userAccount=UserCont; time(&t); p=localtime(&t); pUser->date.year = p->tm_year+1900; pUser->date.month = p->tm_mon+1; pUser->date.day = p->tm_mday; pUser->userAccount = UserCont; pUser->HistoryTop=0; pUser->blance=0; AllUser[UserCont-1]=pUser; printf("您的账号是%d,请牢记 您的密码!/n",UserCont); SucceedBeep(); } char ComparePassword(user * Cuser) { char password[36],n=0; printf("/n请输入您的密码:"); while((password[n]=getch())!='/r') { n++; printf("*"); } puts(""); password[n]='/0'; return (strcmp(password,Cuser->password)); } user * getCurrutUser() { int i,operation; user *CurrutUser=NULL; unsigned int account; char name[NameLength]; deposit_Nu: operation=printHead2(); if(operation==49) { printf("请输入账号:"); scanf("%u",&account); for(i=0;i<UserCont;i++) if(AllUser[i]->userAccount==account) { CurrutUser = AllUser[i]; SucceedBeep(); break; } } else if(operation==50) { printf("请输入账户名:"); scanf("%s",name); for(i=0;i<UserCont;i++) if(stricmp(AllUser[i]->name,name)==0) { CurrutUser = AllUser[i]; SucceedBeep(); break; } } else { printf("没有对应的操作……/n请重新输入:"); FailureBeep(); goto deposit_Nu; } if(ComparePassword(CurrutUser)==0) return CurrutUser; else return BadPassword; } void Deposit() { float money; user * CurrutUser = getCurrutUser(); if(CurrutUser == NULL) { printf("您输入的用户账号不存在,请重试输入。/n"); FailureBeep(); return ; } else if(CurrutUser==BadPassword) { printf("您输入的密码不正确,请重试输入。/n"); FailureBeep(); return ; } else { puts("请输入存款金额:"); deposit_money: scanf("%f",&money); if(money>0) { CurrutUser->blance+=money; time(&t); p = localtime(&t); MakeHistory(CurrutUser,p,DEPOSIT,money); SucceedBeep(); printf("您已成功存款%g元/n",money); } else { printf("存款金额必须大于零。/n请重新输入存款金额:"); FailureBeep(); goto deposit_money; } } } void Draw() { float money; user * CurrutUser = getCurrutUser(); if(CurrutUser == NULL) { printf("您输入的用户账号不存在,请重试输入。/n"); FailureBeep(); return; } else if(CurrutUser==BadPassword) { printf("您输入的密码不正确,请重试输入。/n"); FailureBeep(); return ; } else { puts("请输入取款金额:"); scanf("%f",&money); if(money>0&&money<=CurrutUser->blance) { CurrutUser->blance-=money; printf("您以取款%g元/n",money); SucceedBeep(); time(&t); p = localtime(&t); MakeHistory(CurrutUser,p,DRAW,money); } else if(money<0) { printf("取款金额必须大于零。/n"); FailureBeep(); return ; } else if(money>CurrutUser->blance) { puts("您的余额不足,暂时不能取这么多钱。/n"); FailureBeep(); } } } void Query() { user * CurrutUser = getCurrutUser(); if(CurrutUser == NULL) { printf("您输入的用户账号不存在,请重试输入。/n"); FailureBeep(); return ; } else if(CurrutUser==BadPassword) { printf("您输入的密码不正确,请重试输入。/n"); FailureBeep(); return ; } else { SucceedBeep(); printf(" 账号 /t姓名 /t余额/t 开户日期/n%6u/t %8s/t%6g %6d-%d-%d/n", CurrutUser->userAccount,CurrutUser->name,CurrutUser->blance, CurrutUser->date.year,CurrutUser->date.month,CurrutUser->date.day); } } void CheckHistory() { int i; user * CurrutUser = getCurrutUser(); if(CurrutUser == NULL) { printf("您输入的用户账号不存在,请重试输入。/n"); FailureBeep(); return ; } else if(CurrutUser==BadPassword) { printf("您输入的密码不正确,请重试输入。/n"); FailureBeep(); return ; } else { printf("/n/t账号:%6d/t用户名:%8s/n/n",CurrutUser->userAccount, CurrutUser->name); printf(" 日期 时间 交易类型 交易金额 余额 /n"); for(i=0;i<CurrutUser->HistoryTop;i++) { printf("%4d-%2d-%2d %2d:%2d:%2d", CurrutUser->userHistory[i].year, CurrutUser->userHistory[i].month, CurrutUser->userHistory[i].day, CurrutUser->userHistory[i].hour, CurrutUser->userHistory[i].minute, CurrutUser->userHistory[i].second); if(CurrutUser->userHistory[i].Operation==DEPOSIT) printf(" 存款 "); else if(CurrutUser->userHistory[i].Operation==DRAW) printf(" 取款 "); printf("%6.1f %6.1f /n",CurrutUser->userHistory[i].money, CurrutUser->userHistory[i].blance); } } } void EXIT() { int i=0,opera; FILE * DataOut; printf("确认要退出吗?(Y/N)/n/t/tN/b"); opera=getch(); switch(opera) { case 'y': case 'Y': case '/n':{ printf("即将退出,开始保存数据…………请等待。"); DataOut=fopen("Data.jo","wb"); fwrite(&UserCont,sizeof(int),1,DataOut); while(i<UserCont) { fwrite(AllUser[i],sizeof(user),1,DataOut); i++; } fclose(DataOut); SucceedBeep(); exit(0); } case 'n': case 'N': default : FailureBeep();return ; } }