深受大作业毒害,泪流满面,看了大佬的源代码,在此基础上进行了补充,希望对大家有帮助。
不说了,直接上代码。
//
// main.cpp
// 银行复合
//
// Created by 张书航 on 2022/5/11.
//
#include
#include
#include
#include
#include
#include
#include
#define account0 20030103254000 //第一个开户的卡号
#define random(a,b) (rand()%(b-a+2)+2*a) //设置随机函数
using namespace std;
class ATM;
class depositcard;
class creditcard;
class fuhecard;
class account
{
public:
account()
{
id="0",name="0",sex="0",address="0",phone="0",shenfen="0",password="0",money=0; //默认构造函数
}
account(string i,string n,string se,string a,string t,string s,string p,double m) //带参数构造函数
{
id=i,name=n,sex=se,address=a,phone=t,shenfen=s,password=p,money=m;
}
string gett(){
return phone;
}
string getid()
{
return id;
}
string gets(){
return shenfen;
}
void display(); //用户信息界面
void yue(); //余额查询
protected:
string id,name,address,phone,shenfen,password,sex;
double money;
};
int number=0;
class ATM
{
public:
void setdeposit(); //储蓄卡开户
void setcredit(); //信用卡开户
void setfuhe(); //复合卡开户
void enterfuhe(); //复合卡登陆
void enterdeposit(); //储蓄卡登录
void entercredit(); //信用卡登录
void delaccount(); //销户
void guanli();
void exitatm(); //退出
void dsave(); //储蓄卡类导入磁盘文件
void csave(); //信用卡类导入磁盘文件
void fsave(); //复合卡类导入磁盘文件
void fangke(); //访客界面
void fread(); //复合卡信息从磁盘文件导出
void dread(); //储蓄卡信息从磁盘文件导出
void cread(); //信用卡信息从磁盘文件导出
void setchongzhi();
void tou();
string inputpassword(); //设置账户的密码
bool checkmima(string str);
bool checkshou(string str);
bool checkshen(string str); //检查身份证是否合格
void menu(); //功能菜单
protected:
depositcard *deposit[100]; //定义多个储存卡对象
creditcard *credit[100]; //定义多个信用卡对象
fuhecard *fuhe[100];
static int depositcount,creditcount,fuhecount;
};
class depositcard:virtual public account ,virtual public ATM //账户派生类:储蓄卡
{
public:
friend class ATM;
depositcard(string i,string n,string se,string a,string t,string s,string p,double m):account(i,n,se,a,t,s,p,m) {}
void save(); //存钱功能
void fetch(); //取钱功能
void lixi(); //利息计算
string getp()
{
return password;
}
};
class creditcard:public account ,public ATM //账户派生类:信用卡
{
public:
friend class ATM;
creditcard(string i,string n,string se,string a,string t,string s,string p,double m,double tou,double tm):
account(i,n,se,a,t,s,p,m),touzhi(tou),tmax(tm) {}
void save(); //存钱功能
void fetch(); //取钱功能
void lixi(); //利息计算
void toumoney();
void show();
string getp()
{
return password;
}
string getid()
{
return id;
}
string gets(){
return shenfen;
}
double gettouzhi();
double gettmax();
void add(double x);
private:
double touzhi; //透支额度
double tmax;
};
void ATM::tou(){
cout<<"透支余额"< } void creditcard::show() { cout< cout<<"请按任意键继续!"< } class fuhecard:public account ,public ATM { public: friend class ATM; fuhecard(string i,string n,string se,string a,string t,string s,string p,double m,double tou,double tm): account(i,n,se,a,t,s,p,m),touzhi(tou),tmax(tm) {} void save(); //存钱功能 void quqian(); //取钱功能 void lixi(); //利息计算 void toumoney(); //每月要透支的钱 void show(); string getp() { return password; } string getid() { return id; } double gettouzhi(); double gettmax(); void add(double x); private: double touzhi; //透支额度 double tmax; }; void fuhecard::show() { cout<<"透支余额:"< cout<<"请按任意键继续!"< } int ATM::depositcount=0; int ATM::creditcount=0; int ATM::fuhecount=0; void ATM::dsave() //储蓄卡类导入磁盘文件 { ofstream outfile("deposit.txt",ios::out);//以输出方式打开文件,接收从内存输出的数据 int n=0; outfile< for(n=0; n { outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< } outfile.close(); } void ATM::dread() { int n; string id,sex; string nam,passw,phone; double money; string shenfen; string address; ifstream infile("deposit.txt",ios::in); //以输入方式打开文件 infile>>depositcount; for(n=0; n { infile>>id; infile>>money; infile>>nam; infile>>passw; infile>>shenfen; infile>>phone; infile>>address; infile>>sex; depositcard * chuang = new depositcard(id,nam,sex,address,phone,shenfen,passw,money); deposit[n] = chuang; //赋值首地址 } infile.close(); cout<<"可以正常使用 !"< } void ATM::csave() //信用卡类导入磁盘文件 { ofstream outfile("credit.txt",ios::out);//以输出方式打开文件 credit.txt接收从内存输出的数据 int n=0; outfile< for(n=0; n { outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< } outfile.close(); } void ATM::fsave() //信用卡类导入磁盘文件 { ofstream outfile("fuhe.txt",ios::out);//以输出方式打开文件 bankdat.dat接收从内存输出的数据 int n=0; outfile< for(n=0; n { outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< outfile< } outfile.close(); } void ATM::fread() { int n; string id,sex; string nam,passw,phone; double money,touzhi,tmax; string shenfen; string address; ifstream infile("fuhe.txt",ios::in); //以输入方式打开文件 infile>>fuhecount; if(!infile) { cerr<<"Error reading, no data!"< return; } for(n=0; n { infile>>id; infile>>money; infile>>nam; infile>>passw; infile>>shenfen; infile>>phone; infile>>address; infile>>sex; infile>>touzhi; infile>>tmax; fuhecard *gcc = new fuhecard(id,nam,sex,address,phone,shenfen,passw,money,touzhi,tmax); //每读入一个n开辟一段内存 fuhe[n] =gcc; //赋值首地址 } infile.close(); cout<<"可以正常阅读 !"< } void ATM::cread() { int n; string id,sex; string nam,passw,phone; double money,touzhi,tmax; string shenfen; string address; ifstream infile("credit.txt",ios::in); //以输入方式打开文件 infile>>creditcount; if(!infile) { cerr<<"Error reading, no data!"< return; } for(n=0; n { infile>>id; infile>>money; infile>>nam; infile>>passw; infile>>shenfen; infile>>phone; infile>>address; infile>>sex; infile>>touzhi; infile>>tmax; creditcard * gcc = new creditcard(id,nam,sex,address,phone,shenfen,passw,money,touzhi,tmax); //每读入一个n开辟一段内存 credit[n] =gcc; //赋值首地址 } infile.close(); cout<<"可以正常阅读 !"< } string ATM::inputpassword() { string s=""; char ch; while((ch=getchar())!='n') { s+=ch; } ; return s; } void account::display() //用户信息界面 { cin.get(); cout<<"* "< cout<<"* 姓名:"< cout<<"* 性别:"< cout<<"* 地址:"<
cout<<"* 手机:"< cout<<"* 卡号:"< cout<<"* 余额: "< cout<<"* 按输入键回到上一菜单 "< cout<<"* "< } bool ATM::checkmima(string str){ int k=str.length(); if(k!=6){ return false; } else return true; } bool ATM::checkshou(string str){ int k=str.length(); if(k!=11&&k!=8){ return false; } else return true; } bool ATM::checkshen(string str) { int len = str.length(); if (len != 18) { return false; } else { int i; for (i = 0; i < 18; i++) { if (str[i] < '0' || str[i] > '9') { if (i == 17 && str[i] != 'x' && str[i] != 'X') { return false; } else if (i != 17) { return false; } else return false; } } } return true; } void ATM::exitatm() { cout<<"感谢您使用张书航ATM存取一体机,欢迎再来!"< } void ATM::menu() //功能菜单函数 { char u; cout << " *******************************" << endl; cout << " * *" << endl; cout << " * 欢迎使用张书航牌ATM存取一体机! *" << endl; cout << " * *" << endl; cout << " *******************************" << endl << endl; cout << " 按任意键继续!" << endl; getchar(); do { cout< cout<<" * *"< cout<<" * 1.储蓄卡账户创建 *"< cout<<" * 2.储蓄卡账户登录 *"< cout<<" * 3.信用卡账户创建 *"< cout<<" * 4.信用卡账户登录 *"< cout<<" * 5.复合卡账户创建 *"< cout<<" * 6.复合卡账户登录 *"< cout<<" * 7.账户注销 *"< cout<<" * 8.退出 *"< cout<<" * 9.管理员模式 *"< cout<<" * 10.访客模式 *"< cout<<" * *"< cin>>u; while(u<'1'||u>'11') { cout<<"请合法输入!"< cin>>u; } switch(u) { case '1': setdeposit(); //创建储蓄卡账户 break; case '2': enterdeposit(); break; //储蓄卡登录 case '3': setcredit(); break; //创建信用卡账户 case '4': entercredit(); break; //信用卡登录 case '5': setfuhe(); break; //创建信用卡账户 case '6': enterfuhe(); break; case '7': delaccount(); break; //账户注销*/ case '8': exitatm(); break; //退出 case '9': guanli(); break; case '10': fangke(); break; //退出 } } while(2); } void ATM::fangke() //创建账户 { string i,n,a,t,s,se,p; double m; int f=1; cin.get(); cout<<"请输入姓名:"< getline(cin,n); cout<<"请输入性别:(男或女)"< cin>>se; cin.get(); cout<<"请输入地址:"< cin>>a; while(1) { cout<<"请输入手机号:(11位或8位)"< cin>>t; if(!checkshou(t)){ cout<<"请合法输入手机号"< continue; } else break; } while(1) { cout<<"请输入身份证号:"< cin>>s; if(!checkshen(s)) { cout<<"不合法输入!"< continue; } else break; } getchar(); while(1) { int x=2; while(1){ cout<<"请输入6位纯数字密码:"< p=inputpassword(); if(!checkmima(p)) { cout<<"不合法输入!"< continue; } else break; } string p1; cout<<"请再次输入您设置的密码!"< p1=inputpassword(); cout< while(p!=p1&&x!=0) { cout<<"输入错误,请重新输入!("< p1=inputpassword(); x--; cout< } if(p1==p) break; if(x==0) { f=0; break; } } if(f==0) return ; cout<<"请输入存入金额:"< cin>>m; while(m<=0) { cout<<"请合法输入金额:"< cin>>m; } cin.get(); unsigned long long chuang = account0 + random(1,200)+number; cout<<"开户成功!"< cout << "您的银行卡号为:" << chuang << " 请牢记! 卡号将用于您的账户登陆!" << endl; cout<<"模拟环节结束,将在五秒后返回"< sleep(5); return; } void ATM::setdeposit() //创建账户 { string i,n,a,t,s,se,p; double m; int f=1; cin.get(); cout<<"请输入姓名:"< getline(cin,n); cout<<"请输入性别:(男或女)"< cin>>se; cin.get(); cout<<"请输入地址:"< getline(cin,a); while(1) {cout<<"请输入手机号:(11位或8位)"< cin>>t; if(!checkshou(t)){ cout<<"请合法输入手机号"< continue; } else break; } while(1) { cout<<"请输入身份证号:"< cin>>s; if(!checkshen(s)) { cout<<"不合法输入!"< continue; } else break; } getchar(); while(1) { int x=2; while(1){ cout<<"请输入6位纯数字密码:"< p=inputpassword(); if(!checkmima(p)) { cout<<"不合法输入!"< continue; } else break; } string p1; cout<<"请再次输入您设置的密码!"< p1=inputpassword(); cout< while(p!=p1&&x!=0) { cout<<"输入错误,请重新输入!("< p1=inputpassword(); x--; cout< } if(p1==p) break; if(x==0) { f=0; break; } } if(f==0) return ; cout<<"请输入存入金额:"< cin>>m; while(m<=0) { cout<<"请合法输入金额:"< cin>>m; } getchar(); unsigned long chuang = account0 + random(1,200)+number; number++; cout<<"开户成功!"< cout << "您的银行卡号为:" << chuang<< " 请牢记! 卡号将用于您的账户登陆!" << endl; stringstream str; str << chuang; str >> i; deposit[depositcount]=new depositcard(i,n,se,a,t,s,p,m); //把新建账户对象地址传过去 depositcount++; dsave(); cout<<"请按任意键继续!"< cin.get(); return; } void ATM::setcredit() //创建信用卡账户 { srand((unsigned)time(NULL)); string i,n,a,t,s,se,p; double m; double tou,tmax; int f=1; cin.get(); cout<<"请输入姓名:"< getline(cin,n); cout<<"请输入性别:(男或女)"< cin>>se; cin.get(); getchar(); cout<<"请输入地址:"< getline(cin,a); while(2){ cout<<"请输入手机号:(11位或8位)"< cin>>t; if(!checkshou(t)) { cout<<"不合法输入"< continue; } else break; } while(1) { cout<<"请输入身份证号:"< cin>>s; if(!checkshen(s)) { cout<<"不合法输入!"< continue; } else break; } getchar(); while(1) { int x=2; cout<<"请输入6位纯数字密码:"< while(1){ p=inputpassword(); { if(!checkmima(p)) { cout<<"不合法输入!"< continue; } else break; }} cout< string p1; cout<<"请再次输入您设置的密码!"< p1=inputpassword(); cout< while(p!=p1) { cout<<"输入错误,请重新输入!("< p1=inputpassword(); x--; cout< if(x==0) { f=0; break; } } if(p1==p) break; } if(f==0) return; cout<<"请输入存入金额:"< cin>>m; while(m<=0) { cout<<"请合法输入金额:"< cin>>m; } cout<<"请输入透支金额!存入金额小于100000额度为(2000——10000)"< cout<<"存入金额大于100000额度为(2000——100000)"< cin>>tou; if(m>=100000) {while(tou<=1000||tou>100000) { cout<<"请合法输入!"< cin>>tou; }} else while(tou<=2000||tou>10000) { cout<<"请合法输入!"< cin>>tou; } tmax=tou; cin.get(); unsigned long long gcc = account0 + random(1,200)+number; number++; cout<<"开户成功!"< cout << "您的银行卡号为:" << gcc << " 请牢记! 卡号将用于您的账户登陆!" << endl; stringstream str; str << gcc; str >> i; credit[creditcount]=new creditcard(i,n,se,a,t,s,p,m,tou,tmax); //动态建立一个账户对象 //把新建账户对象地址传过去 creditcount++; csave(); cout<<"请按任意键继续!"< cin.get(); return; } void ATM::setfuhe() //创建复合卡账户 { srand((unsigned)time(NULL)); string i,n,a,t,s,se,p; double m; double tou,tmax; int f=1; cin.get(); cout<<"请输入姓名:"< getline(cin,n); cout<<"请输入性别:(男或女)"< cin>>se; cin.get(); getchar(); cout<<"请输入地址:"< getline(cin,a); while(2){ cout<<"请输入手机号:(11位或8位)"< cin>>t; if(!checkshou(t)) { cout<<"不合法输入"< continue; } else break; } while(1) { cout<<"请输入身份证号:"< cin>>s; if(!checkshen(s)) { cout<<"不合法输入!"< continue; } else break; } getchar(); while(1) { int x=2; cout<<"请输入6位纯数字密码:"< while(1){ p=inputpassword(); { if(!checkmima(p)) { cout<<"不合法输入!"< continue; } else break; }} cout< string p1; cout<<"请再次输入您设置的密码!"< p1=inputpassword(); cout< while(p!=p1) { cout<<"输入错误,请重新输入!("< p1=inputpassword(); x--; cout< if(x==0) { f=0; break; } } if(p1==p) break; } if(f==0) return ; cout<<"请输入存入金额:"< cin>>m; while(m<=0) { cout<<"请合法输入金额:"< cin>>m; } cout<<"请输入透支金额!存入金额小于100000额度为(2000——10000)"< cout<<"存入金额大于100000额度为(2000——100000)"< cin>>tou; if(m>=100000) {while(tou<=1000||tou>100000) { cout<<"请合法输入!"< cin>>tou; }} else while(tou<=2000||tou>10000) { cout<<"请合法输入!"< cin>>tou; } tmax=tou; cin.get(); unsigned long long gcc = account0 + random(1,200)+number; number++; cout<<"开户成功!"< cout << "您的银行卡号为:" << gcc << " 请牢记! 卡号将用于您的账户登陆!" << endl; stringstream str; str << gcc; str >> i; fuhe[fuhecount]=new fuhecard(i,n,se,a,t,s,p,m,tou,tmax); //动态建立一个账户对象 //把新建账户对象地址传过去 fuhecount++; fsave(); cout<<"请按任意键继续!"< cin.get(); return; } void ATM::enterdeposit() //储蓄卡登录 { dread(); string id,p; int f1=1; int flag=1,j,i=0; int x=2,y=2; cout<<"请输入卡号:"< cin>>id; getchar(); while(flag&&x!=0) { for(int i=0; i { if(id==deposit[i]->getid()) { flag=0; j=i; break; } } if(flag==1) { cout<<"账号不存在,请重新输入!("< cin>>id; x--; } } if(x==0) { return; } getchar(); while(1) { cout<<"请输入密码:"< p=inputpassword(); if(p!=deposit[j]->getp()) { cout< cout<
p=inputpassword(); y--; } else break; if(y==0) { char n; cout<<"是否忘记密码,是(1)进入密码重置,否(2)"<


