#include <iostream> #include <string.h> #include <stdio.h> #include <iomanip> //文件操作必写 #include <fstream> using namespace std; //银行中最大的账户数量 #define MAXACCOUNTNUMBER 20000 //银行卡的开号初始值 #define INITNUMBER 10001 struct Account { long accNumber; //卡号 char accName[20]; //户主 char accPwd[10]; //密码 double balance; //余额 char flag[6]; //卡的状态 }; //银行中所有卡 Account array[MAXACCOUNTNUMBER]; //银行中银行卡数量 int size=0; //欢迎界面 void welcome(); //登录 void login(); //主界面 void mainFrame(); //密码检测 bool decide( char pass[]); //生成银行中自增卡号 long autoCreament(); //开户 void openAccount(); //存款 void deposit(); //取款 void draw(); //销户 void closed(); //查询 void demand(); //退出 void quit(); //输出指定卡的信息 void printAccount(Account acc); void read(); //文件读取 void preserve(); //文件保存 //根据卡号查找卡在数组中的下标 int searchAccount(long accNumber); int main() { read(); //欢迎界面 welcome(); //登录 login(); while(1) { //业务选择 mainFrame(); preserve(); } return 0; } //管理员登陆 void login() { char loginName[20]; char loginPwd[20]; int i; //.. for(i=1;i<4;i++) { cout<<"请输入管理员账号:"; fflush(stdin); gets(loginName); cout<<"请输入管理员密码:"; fflush(stdin); gets(loginPwd); //判断 if(strcmp(loginName,"田宇")==0&&strcmp(loginPwd,"计124-3")==0) { break; } else { if(i<3) { cout<<"账号或密码错误!请核对后重新输入\n"; cout<<"您还有"<<3-i<<"次机会重新输入账号和密码。"<<endl; } else { cout<<"密码错误超过3次,退出系统……\n"; exit(0); } } } } //总界面 void welcome() { cout<<" +------------------------------------------+"<<endl; cout<<" |-----------欢迎登陆田氏银行系统-----------|"<<endl; cout<<" +------------------------------------------+"<<endl; } //用户界面 void mainFrame() { int order; cout<<" |-----------------------------------|"<<endl; cout<<" |--------------1.开户---------------|"<<endl; cout<<" |-----------------------------------|"<<endl; cout<<" |--------------2.存款---------------|"<<endl; cout<<" |-----------------------------------|"<<endl; cout<<" |--------------3.取款---------------|"<<endl; cout<<" |-----------------------------------|"<<endl; cout<<" |--------------4.销户---------------|"<<endl; cout<<" |-----------------------------------|"<<endl; cout<<" |--------------5.查询---------------|"<<endl; cout<<" |-----------------------------------|"<<endl; cout<<" |--------------6.退出---------------|"<<endl; cout<<" |-----------------------------------|"<<endl; cout<<"请输入操作指令"<<endl; cin>>order; switch(order){ case 1: openAccount(); break; case 2: deposit(); break; case 3: draw(); break; case 4: closed(); break; case 5: demand(); break; case 6: quit(); break; default: break; } } //开户 void openAccount() { Account mm; cout<<"请输入户主:"; cin>>array[size].accName; cout<<"请输入密码(六位纯数字):"; cin>>array[size].accPwd; if(decide(array[size].accPwd)) { cout<<"请再次输入密码(六位纯数字):"; cin>>mm.accPwd; if(strcmp(array[size].accPwd,mm.accPwd)==0) { cout<<"请输入金额:"; cin>>array[size].balance; strcpy(array[size].flag,"正常"); //写卡号 array[size].accNumber=autoCreament(); printAccount(array[size]); size++; cout<<"开户成功,按enter键返回"<<endl; preserve(); fflush(stdin); getchar(); mainFrame(); } else { cout<<"两次输入的密码不一致,请重新开户"<<endl; openAccount(); } } else { cout<<"密码必须为6位数字,请重新开户"<<endl; openAccount(); } } //用户信息读取 void read() { ifstream account("yinhang.txt",ios::in); int s1; if(!account) { cerr<<"open error"<<endl; exit(1); } account>>size; for(s1=0;s1<size;++s1) { account>>array[s1].accNumber; account>>array[s1].accName; account>>array[s1].accPwd; account>>array[s1].balance; account>>array[s1].flag; } account.close(); } //用户信息写入 void preserve() { ofstream account("yinhang.txt",ios::out); int s1; if(!account) { cerr<<"open error"<<endl; exit(1); } for(s1=0;s1<size;++s1) { account<<s1+1<<" "; account<<array[s1].accNumber<<" "; account<<array[s1].accName<<" "; account<<array[s1].accPwd<<" "; account<<array[s1].balance<<" "; account<<array[s1].flag<<" "; account<<endl; } account.close(); } //密码鉴定 bool decide( char pass[]) { bool flag=true; int len,i; len=strlen(pass); if(len==6) { for(i=0;i<len;i++) { if(pass[i]<48||pass[i]>57){ flag=false; break; } } }else{ flag=false; } return flag; } //卡号的递增 long autoCreament() { if(size==0) { return INITNUMBER; } else { return array[size-1].accNumber+1; } } //输出指定卡的信息 void printAccount(Account acc) { cout<<" |----------------------------------------|"<<endl; cout<<setiosflags(ios::left)<<" |"<<setw(8)<<"卡号"<<setw(8)<<"户主"<<setw(8)<<"密码"<<setw(8)<<"金额"<<setw(8)<<"状态"<<"|"<<endl; cout<<setiosflags(ios::left)<<" +----------------------------------------+"<<endl; cout<<setiosflags(ios::left)<<" |"<<setw(8)<<acc.accNumber<<setw(8)<<acc.accName<<setw(8)<<" 有 "<<setw(8)<<acc.balance<<setw(8)<<acc.flag<<"|"<<endl; cout<<" |----------------------------------------|"<<endl; } /*根据卡号查找卡的下标 * 若返回值=-1,卡号有误 */ int searchAccount(long accNumber) { int i,pos=-1; for(i=0;i<size;i++) { if(array[i].accNumber==accNumber) { //找到了这张卡 pos=i; break; } } return pos; } //存款 void deposit() { long accNum,accNum1;//存款的账号 double money;//存款的金额 int pos; char a; cout<<"请输入存款账号:"; cin>>accNum; pos=searchAccount(accNum); if(strcmp(array[pos].flag,"已销户")==0) { cout<<"此卡已销户,重新输入卡号请按Y 返回主菜单请按其他键"<<endl; cin>>a; if(a=='Y' || a=='y') { deposit(); } else { mainFrame(); } } if(pos==-1) { //卡号有误 cout<<"卡号不存在,请重新输入卡号"<<endl; //... deposit(); } cout<<"请再次输入存款账号:"; cin>>accNum1; if(accNum!=accNum1) { cout<<"两次输入的账号不一致,请确认后再次输入"<<endl; deposit(); } else { cout<<"请输入存款金额:"; cin>>money; //展示下存款前卡的信息 cout<<"存款前卡的信息:"<<endl; printAccount(array[pos]); //找到了该卡号所对应的卡 array[pos].balance+=money; //展示存款后卡的信息 cout<<"存款后卡的信息:"<<endl; printAccount(array[pos]); cout<<"存款成功,按enter键返回"<<endl; preserve(); fflush(stdin); getchar(); mainFrame(); } } //取款 void draw() { long accNum;//取款的账号 double money;//取款的金额 int pos,i; char a,b; Account mm; cout<<"请输入取款的账号:"; cin>>accNum; pos=searchAccount(accNum); if(strcmp(array[pos].flag,"已销户")==0) { cout<<"此卡已销户,重新输入卡号请按Y 返回主菜单请按其他键"<<endl; cin>>a; if(a=='Y' || a=='y') { draw(); } else { mainFrame(); } } if(pos==-1) { //卡号有误 cout<<"卡号不存在,请重新输入卡号"<<endl; //... draw(); } else { for(i=1;i<4;i++) { cout<<"请输入密码:"; cin>>mm.accPwd; if(strcmp(array[pos].accPwd,mm.accPwd)==0) { break; } else { if(i<3) { cout<<"账号或密码错误!请核对后重新输入\n"; cout<<"您还有"<<3-i<<"次机会重新输入账号和密码。"<<endl; } else { cout<<"密码错误超过3次,退出系统……\n"; exit(0); } } } } cout<<"请输入取款金额:"; cin>>money; //展示下取款前卡的信息 cout<<"取款前卡的信息:"<<endl; printAccount(array[pos]); if(array[pos].balance<money) { cout<<"您的余额不足,重新输入取款账号和密码请按Y 返回主菜单请按其他键"<<endl; cin>>b; if(b=='Y' || b=='y') { draw(); } else { mainFrame(); } } else { //找到了取卡号所对应的卡 array[pos].balance-=money; } //展示存款后卡的信息 cout<<"取款后卡的信息:"<<endl; printAccount(array[pos]); cout<<"取款成功,按enter键返回"<<endl; preserve(); fflush(stdin); getchar(); mainFrame(); } //查询 void demand() { long accNum;//查询的账号 int pos,i; char a; Account mm; cout<<"请输入要查询的账号:"; cin>>accNum; pos=searchAccount(accNum); if(pos==-1) { //卡号有误 cout<<"卡号不存在,重新输入卡号请按Y 返回主菜单请按其他键"<<endl; cin>>a; if(a=='Y' || a=='y') { demand(); } else { mainFrame(); } } else { for(i=1;i<4;i++) { cout<<"请输入密码:"; cin>>mm.accPwd; if(strcmp(array[pos].accPwd,mm.accPwd)==0) { break; } else { if(i<3) { cout<<"账号或密码错误!请核对后重新输入\n"; cout<<"您还有"<<3-i<<"次机会重新输入账号和密码。"<<endl; } else { cout<<"密码错误超过3次,退出系统……\n"; exit(0); } } } } //展示卡的信息 cout<<"此卡的信息:"<<endl; printAccount(array[pos]); cout<<"查询成功,按enter键返回"<<endl; fflush(stdin); getchar(); mainFrame(); } //销户 void closed() { long accNum;//销户的账号 int pos,i; char a; Account mm; cout<<"请输入要销户的账号:"; cin>>accNum; pos=searchAccount(accNum); if(pos==-1) { //卡号有误 cout<<"卡号不存在,请重新输入卡号"<<endl; //... closed(); } else { for(i=1;i<4;i++) { cout<<"请输入密码:"; cin>>mm.accPwd; if(strcmp(array[pos].accPwd,mm.accPwd)==0) { cout<<endl<<"销户前卡的信息:"<<endl; printAccount(array[pos]); cout<<"您将进行销户!!!!!!"<<endl; cout<<" 确定请按Y 按其他键返回主菜单 "<<endl; cin>>a; if(a=='Y' || a=='y') { getchar(); array[pos].balance=0; strcpy(array[pos].flag,"已销户"); strcpy(array[pos].accName,"无"); cout<<endl<<"销户后卡的信息:"<<endl; printAccount(array[pos]); cout<<endl<<"销户成功,按enter键返回"<<endl; preserve(); fflush(stdin); getchar(); mainFrame(); } else { mainFrame(); } } else { if(i<3) { cout<<"账号或密码错误!请核对后重新输入\n"; cout<<"您还有"<<3-i<<"次机会重新输入账号和密码。"<<endl; } else { cout<<"密码连续3次错误!!!销户失败!!!"<<endl; cout<<endl<<"按enter键返回"<<endl; fflush(stdin); getchar(); mainFrame(); } } } } } //退出 void quit() { char a; cout<<"退出请按Y 按其他键返回主菜单 "<<endl; cin>>a; if(a=='Y' || a=='y') { cout<<"感谢您本次的使用,欢迎下次光临。"<<endl; exit(0); } else { mainFrame(); } }