<span style="font-family: Arial, Helvetica, sans-serif;">main.cpp</span>
<span style="font-family: Arial, Helvetica, sans-serif;">#include<iostream></span>
#include<fstream> #include <windows.h> //清屏函数的调用需要的头文件 #include<conio.h> //调用getch需要用到的头文件 #include<stdlib.h> #include <windows.h> //system,Sleep,exit函数的调用需要的头文件 #include"customer.h" #include"officer.cpp" using namespace std; class Bank:public customer,public officer { public: void work() { officer off; customer cus; string name; int mima; cout<<"+----------------------------+"<<endl; cout<<"+ 欢迎观临程序员银行 +"<<endl; cout<<"+----------------------------+"<<endl; cout<<"请输入业务员用户名:"; cin>>name; cout<<"请输入密码:"; mima=off.get_password(); cout<<endl; if(name==off.getname1()&&mima==off.getmima1()) { cout<<"正在进入程序员银行,请稍后!"<<endl; Sleep(1000); system("cls"); cout<<"+--------------------------+"<<endl; cout<<"1.开户 "<<"2.销户"<<endl; cout<<"3.存钱 "<<"4.取款"<<endl; cout<<"5.信息查询 "<<"6.转账"<<endl; cout<<"7.挂失 "<<"8.解挂"<<endl; cout<<"9.更改密码 "<<"10.退出"<<endl; cout<<"+--------------------------+"<<endl; int choice; while(cin>>choice) { switch(choice) { case 1: cus.openAccount(); //开户 break; case 2: cus.cancelAccount(); //注销账户 break; case 3: cus.save(); //存款 break; case 4: cus.withdraw(); //取款 break; case 5: cus.showAccount(); //查询余额 break; case 6: cus.transferAccounts(); //转账 break; case 7: cus.reportLoss(); //挂失 break; case 8: cus.cancelLoss(); //解除挂失 break; case 9: cus.updatePassword(); //更改密码 break; case 10: { cout<<"欢迎您再来. "<<endl; Sleep(100); system("cls"); cout<<"+--------------------------+"<<endl; cout<<"1.开户 "<<"2.销户"<<endl; cout<<"3.存钱 "<<"4.取款"<<endl; cout<<"5.信息查询 "<<"6.转账"<<endl; cout<<"7.挂失 "<<"8.解挂"<<endl; cout<<"9.更改密码 "<<"10.退出"<<endl; cout<<"+--------------------------+"<<endl; break; } } } } else { cout<<"用户名或密码错误,请重新启动!"<<endl; } } }; int main() { Bank bank; bank.work(); return 0; }
officer.cpp
#include<iostream> #include<fstream> #include <windows.h> //清屏函数的调用需要的头文件 #include<conio.h> //调用getch需要用到的头文件 #include<stdlib.h> #include <windows.h> //system,Sleep,exit函数的调用需要的头文件 using namespace std; class officer { private: string name1; int mima1; public: officer(string na,int nn):name1(na),mima1(nn) {} void setofficer() { cin>>name1>>mima1; } string getname1() { return name1; } int getmima1() { return mima1; } officer() { name1="huangyong"; mima1=123456; } ~officer() { } int get_password() { int password=0; int m; for(int i=0; i<6;) { m=getch()-48; if(m>=0&&m<=9) { cout<<'*'; (password*=10)+=m; i++; } } return password; } };
#ifndef CUSTOMER_H_INCLUDED #define CUSTOMER_H_INCLUDED #include<iostream> #include<fstream> #include <windows.h> //清屏函数的调用需要的头文件 #include<conio.h> //调用getch需要用到的头文件 #include<stdlib.h> #include <windows.h> //system,Sleep,exit函数的调用需要的头文件 using namespace std; class customer { private: string name[100]; int mima[100]; static int n; double jinqian[100]; int status[100]; int account[100]; int shenfen[100]; public: customer(string na,int nn) { name[n]=na; mima[n]=nn; } customer() { } void setcustomer() { cin>>name[n]>>mima[n]; } string getname() { return name[n]; } int getmima() { return mima[n]; } void setname() { cin>>name[n]; } void setmima(int s) { cin>>mima[s]; } ~customer() { } int get_password() { int password=0; int m; for(int i=0; i<3;) { m=getch()-48; if(m>=0&&m<=9) { cout<<'*'; (password*=10)+=m; i++; } } return password; } int passwordIsRight(int s) { if(mima[s]) return 1; else return 0; } int inputPassword() { char ch; //接收字符形式密码 int iPass=0; //要转换为数字 int i; while(1) { for(i=0; i<6; i++) { ch=getch(); //输入但不显示 putchar('*'); //输出* if(isdigit(ch)) iPass=iPass*10+(ch-'0'); else { iPass=0; break; //退出for循环后,再次接受 } } fflush(stdin); //清除键盘缓存区中已经有的输入 cout<<endl;; if(iPass==0) //此条件成立可能由两种情况引起:输入了非数字字符被直接重置为0,或6位全0后正常退出for循环 { cout<<"密码要求全为数字,且不能全0!"<<endl;; cout<<"请重新输入密码: "; } else break; } return iPass; } void openAccount() { fstream infile; infile.read("customer1",ios::binary); for(int i=0;i<5;i++) { infile.read((char*)&account[i],sizeof(account[i])); infile.read((char*)&name[i],sizeof(name[i])); infile.read((char*)&shenfen[i],sizeof(shenfen[i])); infile.read((char*)&jinqian[i],sizeof(jinqian[i])); infile.read((char*)&status[i],sizeof(status[i])); n++; } if(n==100) { cout<<"银行用户数已经达到上限,不能再开户"; return; } //下面正常办理开户业务 int acc; //账号 string nam; //姓名 int shenf; int pw; //密码 double bal; //金额 int sta; //状态 cout<<"正在开户"<<endl; acc=10001+n; cout<<"账号:"<<acc<<endl; cout<<"户主姓名:"; cin>>nam; cout<<"身份号码:"; cin>>shenf; int iPass1, iPass2; cout<<"密码:"; iPass1=inputPassword(); //输入密码1 cout<<"确认密码:"; iPass2=inputPassword(); //输入密码2 if(iPass1==iPass2) { pw=iPass1; sta=0; //账户状态为“正常” cout<<"存入金额:"; cin>>bal; name[n]=nam; mima[n]=pw; jinqian[n]=bal; status[n]=sta; shenfen[n]=shenf; account[n]=acc; n++; //正式用户数增加1,确认了新用户已经加入 cout<<"成功开户!"<<endl; } else { cout<<"两次密码不一致,未成功开户!"<<endl; //没有N++,则读入的值无效 } ofstream outfile; outfile.open("customer",ios::out); for(int i=0; i<n; i++) { outfile<<i+10001<<" "<<name[i]<<" "<<shenfen[i]<<" "<<mima[i]<<" "<<jinqian[i]<<" "; } ofstream outfile1; outfile1.open("customer1",ios::binary); for(int i=0;i<n;i++) { outfile1.write((char*)&account[i],sizeof(account[i])); outfile1.write((char*)&name[i],sizeof(name[i])); outfile1.write((char*)&jinqian[i],sizeof(jinqian[i])); } } void cancelAccount() { int who; //查找到该账号在数组中对应的下标 who = getUser(); //根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { cout<<name[who-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; if(passwordIsRight(who-10001)) { cout<<"余额:"<<jinqian[who-10001]; //提示"余额"二字,显示金额 cout<<endl; cout<<"确认销户(y/n)?"; if(tolower(getchar())=='y') { cout<<"余额:"<<jinqian[who-10001]; cout<<"销户成功!本次取款金额为"; jinqian[who-10001]=0; //取款后余额变0 status[who-10001]=2; //状态变为注销 } else { cout<<"你取消了操作,销户失败!"<<endl; } fflush(stdin); //清除了getchar()时在键盘缓存中的遗留,以免影响后续操作 } } } /* 功能:存款 说明:需要保证账户存在,且处于正常状态 */ void save() { int who; double money; who = getUser(); //根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { if(status[who-10001]==0) { cout<<name[who-10001]<<endl; cout<<"输入存款额:"; cin>>money; jinqian[who-10001]+=money; cout<<"存款后,您有"<<jinqian[who-10001]; } else if(status[who-10001]==1) { cout<<"该用户处于挂失状态,存款失败!"<<endl; } else { cout<<"该用户已经销户,存款失败!"<<endl; } } return; } /* 功能:取款 说明:需要保证账户存在,且处于正常状态,另外,余额要足够取 */ void withdraw() { int who; double money; who = getUser(); //根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { cout<<name[who-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; cout<<"请输入密码:"<<endl; if(passwordIsRight(who-10001)) { cout<<"输入取款额:"; cin>>money; if(money>jinqian[who-10001]) //亲,不玩透支 { cout<<"余额不足,取款失败!"<<endl; } else { jinqian[who-10001]-=money; cout<<"取款后,余额有:"<<jinqian[who-10001]; } } } return; } /* 功能:查询账户 说明:显示账户信息 */ void showAccount() { int who; string sta[3]= {"正常","挂失","已经销户"}; who = getUser(); //根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { cout<<"姓名:"<<name[who-10001]<<endl; cout<<"身份号码:"<<shenfen[who-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; if(passwordIsRight(who-10001)) { cout<<"余额:"<<jinqian[who-10001]; cout<<"状态:"<<sta[status[who-10001]]<<endl; } } return; } /* 功能:转账 说明:需要保证两个账户都存在,且处于正常状态,另外,转出账户的余额要足够 */ void transferAccounts() { int whoout, whoin; double money; cout<<"转出自"; whoout = getUser(); //根据账号查询用户,返回用户的下标 if(whoout>=0) //说明id账户存在 { cout<<name[whoout-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; if(passwordIsRight(whoout-10001)) { cout<<"输入转账金额:"; cin>>money; if(money>jinqian[whoout-10001]) //亲,不玩透支 { cout<<"余额不足,转账失败!"<<endl; } else { cout<<"转出到"; whoin = getUser(); //根据账号查询用户,返回用户的下标 if(whoin>=0) //说明id账户存在 { jinqian[whoout-10001]-=money; jinqian[whoin-10001]+=money; cout<<"转账后,您还有"<<jinqian[whoout-10001]; } } } } return; } /* 功能:挂失账户 */ void reportLoss() { int who; who = getUser(); //根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { cout<<name[who-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; if(passwordIsRight(who-10001)) { if(status[who-10001]==0) { status[who-10001]=1; cout<<"挂失成功"<<endl; } else if(status[who-10001]==1) { cout<<"该账户已经处于挂失状态"<<endl; } else { cout<<"该账户已销户,不能挂失"<<endl; } } } return; } /* 功能:解除挂失 */ void cancelLoss() { int who; who = getUser(); //根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { cout<<name[who-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; if(passwordIsRight(who-10001)) { if(status[who-10001]==0) { cout<<"该账户处于正常状态,不需要解除挂失"<<endl; } else if(status[who-10001]==1) { status[who-10001]=0; cout<<"解除挂失成功"<<endl; } else { cout<<"该账户已销户,操作无效"<<endl; } } } return; } /* 功能:改密码 */ void updatePassword() { int who; int iPass1, iPass2; who = getUser();//根据账号查询用户,返回用户的下标 if(who>=0) //说明id账户存在 { cout<<name[who-10001]<<endl; cout<<"请输入密码:"; get_password(); cout<<endl; if(passwordIsRight(who-10001)) { cout<<"新密码:"; setmima(who-10001); //输入密码1 iPass1=mima[who-10001]; cout<<"确认密码:"; iPass2=inputPassword(); //输入密码2 if(iPass1==iPass2) { cout<<"修改成功!"<<endl; } else { cout<<"两次输入不同,修改失败!"<<endl; } } } return; } /* 功能:输入账号查询用户,返回用户在数组中的下标 入口参数:要查询用户的账号 返回值:如果该用户存在,返回该用户在数组中的下标,否则,返回一个负数(-1) 说明: 由账号自动产生,使按账号有序,本模块采用二分查找 */ int getUser() { int id; cout<<"账号:"; cin>>id; int index=-1; int low=0, high=n-1, mid; while(low<=high) { mid = (low+high)/2; if(account[mid]==id) { index=mid; break; //找到了,立即退出循环 } else if (account[mid]>id) high=mid-1; else low=mid+1; } if (index<0) cout<<"该用户不存在,本次操作失败!"<<endl; index=index+10001; //若找到,其值在0~N-1间,否则,保持-1 return index; } }; int customer::n=0; #endif // CUSTOMER_H_INCLUDED