...假设它可以在5000元 范围内透支,它有一个用户密码
发布网友
发布时间:2024-09-27 19:10
我来回答
共1个回答
热心网友
时间:2024-10-24 17:34
下面这些是用Unix C++写的,可以使用g++编译。这是我以前写的一个程序,和你的要求非常类似,所以直接贴在下面。
-----------------------------------------------------------------------------------------------------------
//文件main.cc
#include "credit_card.h"
#include <iostream>
using namespace std;
#include <cstdlib>
Bank_book Bb;
Credit_card Cc;
Yonghu_ziliao Yz_Bb[10];
Yonghu_ziliao Yz_Cc[10];
void ZongCaozuo();
void BB();
void CC();
void ZongCaozuo()
{
int choose;
cout << "本银行有两种业务——存折、信用卡,用户可按个人情况选择办哪种业务:" << endl;
cout << "若选择存折业务,请按 0;" << endl;
cout << "若选择信用卡业务,请按 1;" << endl;
cout << "若选择退出,请按 2;" << endl;
while(1)
{
cin >> choose;
switch(choose)
{
case 0 : BB(); break;
case 1 : CC(); break;
case 2 : cout << "谢谢您的支持。" << endl; exit(0); break;
default: cout << "本银行无此业务,请用户重新做出选择。" << endl; break;
}
}
}
void BB()
{
int B_Zlei;
for(;;)
{
cout << "下面说明是为存折用户提供的服务:" << endl;
cout << "开户,请按数字 1;" << endl;
cout << "存款,请按数字 2;" << endl;
cout << "取款,请按数字 3;" << endl;
cout << "查询余额,请按数字 4;" << endl;
cout << "退出存折操作,请按数字 5;" << endl;
cin >> B_Zlei;
switch(B_Zlei)
{
case 1 : Bb.Kaihu(Yz_Bb); break;
case 2 : Bb.Deposit_money(Yz_Bb); break;
case 3 : Bb.Draw_money(Yz_Bb); break;
case 4 : Bb.Inquires(Yz_Bb); break;
case 5 : ZongCaozuo(); break;
default : cout << "存折没有此项服务,请重新选择。" << endl; break;
}
}
}
void CC()
{
int C_Zlei;
for(;;)
{
cout << "下面说明是为信用卡用户提供的服务:" << endl;
cout << "开户,请按数字 1;" << endl;
cout << "存款,请按数字 2;" << endl;
cout << "查询余额,请按数字 3;" << endl;
cout << "消费,请按数字 4;" << endl;
cout << "退出信用卡操作,请按数字 5;" << endl;
cin >> C_Zlei;
switch(C_Zlei)
{
case 1 : Cc.Kaihu(Yz_Cc); break;
case 2 : Cc.Deposit_money(Yz_Cc); break;
case 3 : Cc.Inquires(Yz_Cc); break;
case 4 : Cc.consumption(Yz_Cc); break;
case 5 : ZongCaozuo(); break;
default : cout << "信用卡没有此项服务,请重新选择。" << endl; break;
}
}
}
int main()
{
for (int i=0; i<10; i++)
{
Yz_Bb[i].ID = 0;
Yz_Bb[i].Code = 123456;
Yz_Bb[i].Remain_money = 0;
Yz_Cc[i].ID = 0;
Yz_Cc[i].Code = 123456;
Yz_Cc[i].Remain_money = 0;
}
ZongCaozuo();
return 0;
}
-----------------------------------------------------------------------------------------------------------
//文件credit_card.h
#ifndef _CREDIT_CARD_H_
#define _CREDIT_CARD_H_
#include "bank_book.h"
class Credit_card : public Bank_book //基类
{
public :
Credit_card(); //构造
~Credit_card(); //析构
double consumption(Yonghu_ziliao yz[10]); //消费
private :
double consume_money; //消费金额
int _id; //ID
int _code; //密码
};
#endif
-----------------------------------------------------------------------------------------------------------
//文件credit_card.cc
#include "credit_card.h"
#include <iostream>
using namespace std;
Credit_card :: Credit_card() //构造
{
consume_money = 0;
_id = 0;
_code = 0;
}
Credit_card :: ~Credit_card() //析构
{}
double Credit_card :: consumption(Yonghu_ziliao yz[10]) //消费
{
cout << "请输入您的ID号:" << endl;
cin >> _id;
if(_id == yz[_id-1].ID) //是否有此ID
{
cout << "请输入ID号对应的密码:" << endl;
cin >> _code;
if(_code == yz[_id-1].Code) //密码是否正确
{
cout << "请输入消费金额:" << endl;
cin >> consume_money; //输入消费金额
if(consume_money > yz[_id-1].Remain_money+5000) //是否超过上限
{
cout << "消费失败,您的消费金额已超出信用卡透支上限!" << endl;
return yz[_id-1].Remain_money;
}
else
{
cout << "消费成功!" << endl;
return yz[_id-1].Remain_money -= consume_money; //余额减去取款的金额
}
}
else
{
cout << "密码不对!" << endl;
}
}
else
{
cout << "系统中无此帐号存在,请核对后再输入!" << endl;
return yz[_id-1].Remain_money;
}
}
-----------------------------------------------------------------------------------------------------------
//文件bank_book.h
#ifndef _BANK_BOOK_H_
#define _BANK_BOOK_H_
typedef struct
{
int ID; //用户ID
int Code; //用户密码
double Remain_money; //用户存折余额
} Yonghu_ziliao; //用户资料结构体
class Bank_book //基类
{
public :
Bank_book(); //构造
~Bank_book(); //析构
void Kaihu(Yonghu_ziliao yz[10]); //开户
double Deposit_money(Yonghu_ziliao yz[10]); //存款
double Draw_money(Yonghu_ziliao yz[10]); //取款
void Inquires(Yonghu_ziliao yz[10]); //查询余额
private :
int kaihu_count; //开户数量
double De_money; //存款数
double Dr_money; //取款数
double Re_money; //余额
int id; //临时ID
int code; //密码
};
#endif
-----------------------------------------------------------------------------------------------------------
//文件bank_book.cc
#include "bank_book.h"
#include <iostream>
using namespace std;
Bank_book :: Bank_book()
{
kaihu_count = 0;
De_money = 0.0;
Dr_money = 0.0;
Re_money = 0.0;
id = 0;
code = 0;
}
Bank_book :: ~Bank_book()
{}
void Bank_book :: Kaihu(Yonghu_ziliao yz[10]) //开户
{
kaihu_count ++;
yz[kaihu_count-1].ID = kaihu_count;
cout << "您好,谢谢您对本行存折业务的支持!" << endl;
cout << "请输入六位数字的密码" << endl;
cin >> yz[kaihu_count-1].Code;
cout << "以下信息是您的ID号,请将ID号和密码保存好,以备以后在本行办理业务时使用。" << endl;
cout << "ID:" << kaihu_count << endl;
}
double Bank_book :: Deposit_money(Yonghu_ziliao yz[10]) //存款
{
cout << "请输入您的ID号:" << endl;
cin >> id;
if(id == yz[id-1].ID) //看系统中是否有此ID
{
cout << "请输入ID号对应的密码:" << endl;
cin >> code;
if(code == yz[id-1].Code) //密码是否正确
{
cout << "请输入您要存款的金额!" << endl;
cin >> De_money; //输入存款金额
cout << "成功存款!" << endl;
}
else
{
cout << "密码不对!" << endl;
}
}
else
{
cout << "系统中无此帐号存在,请核对后再输入!" << endl;
}
return yz[id-1].Remain_money += De_money; //余额加上存款的金额
}
double Bank_book :: Draw_money(Yonghu_ziliao yz[10])
{
cout << "请输入您的ID号:" << endl;
cin >> id;
if(id == yz[id-1].ID) //是否有此ID
{
cout << "请输入ID号对应的密码:" << endl;
cin >> code;
if(code == yz[id-1].Code) //密码是否正确
{
cout << "请输入您要取款的金额!" << endl;
cin >> Dr_money; //输入取款金额
if(Dr_money > yz[id-1].Remain_money)
{
cout << "对不起,您账户余额不足,请核对后再取款。" << endl;
return yz[id-1].Remain_money;
}
else
{
cout << "成功取款!" << endl;
return yz[id-1].Remain_money -= Dr_money; //余额减去取款的金额
}
}
else
{
cout << "密码不对!" << endl;
}
}
else
{
cout << "系统中无此帐号存在,请核对后再输入!" << endl;
}
}
void Bank_book :: Inquires(Yonghu_ziliao yz[10])
{
cout << "请输入您的ID号:" << endl;
cin >> id;
if(id == yz[id-1].ID) //是否有此ID
{
cout << "请输入ID号对应的密码:" << endl;
cin >> code;
if(code == yz[id-1].Code) //密码是否正确
cout << "您存折账户上所剩余额为:" << yz[id-1].Remain_money << endl;
else
cout << "密码不正确!" << endl;
}
else
{
cout << "系统中无此帐号存在,请核对后再输入!" << endl;
}
}