#include <iostream> #include <string> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop class*/ class Account { public: Account(){money=0;} void bankin(); void bankout(); void display(); void getdata(); private: int number; string name; double money; }; void Account::getdata() { cout<<"输入账户姓名:"<<endl; cin>>name; cout<<"输入账户账号:"<<endl; cin>>number; } void Account::bankin() { double t; cout<<"输入要存的钱金额:"<<endl; cin>>t; money=money+t; } void Account::bankout() { double t; cout<<"输入要取出钱的金额:" <<endl; cin>>t; if(t<=money) money=money-t; else cout<<"余额不足"<<endl; } void Account::display() { cout<<"账户姓名:"<<name<<endl; cout<<"账户账号:" <<number<<endl; cout<<"当前账户余额:"<<money<<endl; } int main(int argc, char** argv) { int a; Account b; cout<<"选择操作:1.输入账户信息,2.存钱,3.取钱,4.显示账户余额"<<endl; cin>>a; if(a==1) b.getdata(); if(a==2) b.bankin(); else if(a==3) b.bankout(); else if(a==4) b.display(); else cout<<"输入不合法"<<endl; return 0; }修改一下代码
时间: 2024-04-02 15:32:41 浏览: 371
#include <iostream>
#include <string>
using namespace std;
class Account
{
public:
Account(): money(0), number(0) {}
void bankin();
void bankout();
void display();
void getdata();
private:
int number;
string name;
double money;
};
void Account::getdata()
{
cout << "输入账户姓名:" << endl;
cin >> name;
cout << "输入账户账号:" << endl;
cin >> number;
}
void Account::bankin()
{
double t;
cout << "输入要存的钱金额:" << endl;
cin >> t;
money += t;
}
void Account::bankout()
{
double t;
cout << "输入要取出钱的金额:" << endl;
cin >> t;
if (t <= money)
money -= t;
else
cout << "余额不足" << endl;
}
void Account::display()
{
cout << "账户姓名:" << name << endl;
cout << "账户账号:" << number << endl;
cout << "当前账户余额:" << money << endl;
}
int main(int argc, char** argv)
{
int a;
Account b;
cout << "选择操作:1.输入账户信息,2.存钱,3.取钱,4.显示账户余额" << endl;
cin >> a;
switch (a)
{
case 1:
b.getdata();
break;
case 2:
b.bankin();
break;
case 3:
b.bankout();
break;
case 4:
b.display();
break;
default:
cout << "输入不合法" << endl;
break;
}
return 0;
}
阅读全文