| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 811 人关注过本帖
标题:求助~~~不明白怎么弄了,请求帮改改。
只看楼主 加入收藏
jiruanl062
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-11-6
收藏
 问题点数:0 回复次数:4 
求助~~~不明白怎么弄了,请求帮改改。
编译能通过,可是运行结果不对……不知道问题所在。初学C++

#include <iostream>
#include <string>
using namespace std;

/*创建一个名为BankAccount的基类,*****
**它的数据成员包括帐户所有人姓名******
**(一个字符串,假定命名为name),****
**以及帐户余额(double值)balance。***
**在类中添加成员函数deposit和withdraw*
**并添加取值函数getName和getBalance。*
**deposit(存款),withdraw(取款)。*****
**存款将amount加到balance取款反之*/
class BankAccount
{   
public:
    BankAccount();
    BankAccount(string the_name,double the_balance,double the_amount);
    void set_name(string new_name);
    void set_balance(double new_balance);
    void set_amount(double new_amount);
    string get_name() const;
    double get_balance() const;
    double get_amount() const;
    void deposit();
    //存款
    void withdraw();
    //取款

protected:
    string name;
    double balance;
    double amount;
};

BankAccount::BankAccount():name("No name yet"),balance(0),amount(0)
{
        //有意留空
}
    
BankAccount::BankAccount(string the_name,double the_balance,double the_amount)
        :name(the_name),balance(the_balance),amount(the_amount)
{
        //有意留空
}
    
string BankAccount::get_name() const
{
    return name;
}

double BankAccount::get_balance() const
{
    return balance;
}

double BankAccount::get_amount() const
{
    return amount;
}
    
void BankAccount::set_name(string new_name)
{
    name=new_name;
}
    
void BankAccount::set_balance(double new_balance)
{
    balance=new_balance;
}

void BankAccount::set_amount(double new_amount)
{
    amount=new_amount;
}
    
void BankAccount::deposit()
{
    balance = balance + amount;
    cout << "你的帐户名称为: " << name << endl;
    cout << "你的余额为: "<< balance << endl;
}
    
void BankAccount::withdraw()
{
    //withdraw函数都应返回一个表示帐户状态的证书(允许取款;或者余额不足,不允许取款)。
    if ( balance >= amount )
        balance = balance - amount;
    else
        cout << "余额不足,不允许取款!" << endl;
}

//----------------------------------------------------------------------------------------
/*它从BankAccount类派生。在一个MoneyMarketAccount中,
用户可以在一段优惠时间内两次免费取款。免费取款后,
以后每次取款都要从余额中扣除1.50美元的手续费。所以,
该类必须有一个数据成员来跟踪取款次数。另外,它还需要
覆盖withdraw定义。*/

class MoneyMarketAccount : public BankAccount
{
public:
    MoneyMarketAccount();
    MoneyMarketAccount(string the_name,double the_balance,double amount);
    void withdraw();
    //想要更改函数定义,只需要列出继承的哪个成员函数的声明
    static int times;
};

int MoneyMarketAccount::times=0;

MoneyMarketAccount::MoneyMarketAccount() : BankAccount()
{
        //主体空
}
    
MoneyMarketAccount::MoneyMarketAccount(string the_name,double the_balance,double the_amount)
        :BankAccount(the_name,the_balance,the_amount)
{
        //主体有意留空
}
    
void MoneyMarketAccount::withdraw()
{
//withdraw函数都应返回一个表示帐户状态的证书(允许取款;或者余额不足,不允许取款)。
    const double A=1.50;
    times++;
    if( times <= 2 )
    {
        if ( balance >= amount)
            balance = balance - amount;
        else
            cout<<"余额不足,不允许取款!" << endl;
    }
    else
    {  
        if ( balance >= (amount - A) )
            balance = balance-amount-A;
        else
            cout<<" 余额不足,不允许取款!" << endl;
    }

    cout << "你的帐户名称为: " << name << endl;
    cout << "你还剩余:" << balance << endl;
}

//-----------------------------------------------------------------------------
/*同样从BankAccount派生。该类除了包含数据成员name和balance之外
还有一个名为interest_rate(利率)的数据成员。银行的规定是假如
提前取款,客户必须缴纳一定的提前取款罚金(prepayment penalty)。
假定一次取款操作(任意金额)会扣除帐户年利息的25%作为罚金。另外,
假定取款金额和罚金都从帐户余额中扣除。同样的,withdraw函数必须
覆盖基类的版本。对于所有这三个类withdraw函数都应返回一个
表示帐户状态的证书(允许取款;或者余额不足,不允许取款)。
考虑到练习的目的,暂时不关心这些帐户的其他函数和属性
(比如何时和怎样支付利息)。*/
class CDAccount : public BankAccount
{
public:
    CDAccount();
    CDAccount(string the_name,double the_balance,double the_amount,double interest_rate);
    double get_interest_rate() const;
    void set_interest_rate(double new_interest_rate);
    void withdraw();    //重定义
protected:
    double interest_rate; //利率
};

CDAccount::CDAccount():BankAccount(),interest_rate(0)
{
        //空
}
    
CDAccount::CDAccount(string the_name,double the_balance,double the_amount,double the_interest_rate)
        :BankAccount(the_name,the_balance,the_amount),interest_rate(the_interest_rate)
{
        //nothing
}
    
double CDAccount::get_interest_rate() const
{
    return interest_rate;
}
    
void CDAccount::set_interest_rate(double new_interest_rate)
{
    interest_rate=new_interest_rate;
}
   
void CDAccount::withdraw()
{
    //withdraw函数都应返回一个表示帐户状态的证书(允许取款;或者余额不足,不允许取款)。
    const double B=0.25;
    double C;
    C= balance - amount - balance*interest_rate*B;
            
    if ( balance >= C)
        balance = C;
    else
        cout<<" 余额不足,不允许取款 " << endl;

    cout << "你的帐户名称为: " << name << endl;
    cout << "你还剩余:"<<balance<<endl;
}
//-----------------------------------------------------------------------------


void main()
{
    double amount;
    double balance;
    double rate=0.01;
    string yourname;
    char ans;
    char abc;
    BankAccount a1(yourname,balance,amount);
    MoneyMarketAccount a2(yourname,balance,amount);
    CDAccount a3(yourname,balance,amount,rate);
    
    a3.set_interest_rate(rate);
    cout << "Please enter your money :" << endl ;
    cin >> balance;
    cout << "Please enter your name :" << endl;
    cin >> yourname;

    a1.set_balance(balance);
    a1.set_name(yourname);

    do
    {
        cout << "请输入你选择的操作:A为存款,B为正常取款,C为提前取款!" <<endl;
        cin >> ans;

        switch(ans)
        {
        case 'A':    //存钱
            cout<<"请输入存款金额:"<<endl;
            cin>>amount;
            a1.set_amount(amount);
            a1.deposit();
            break;

        case 'B':    //正常取钱
            cout << " 请输入取款金额 : " << endl;
            cin >> amount;
            a2.set_amount(amount);
            a2.withdraw();
            break;

        case 'C':    //提前取钱
            cout << " 请输入取款金额 : " << endl;
            cin >> amount;
            a3.set_amount(amount);
            a3.withdraw();
            break;

        default :
            cout << "没有这项操作!" <<endl;
            break;
        }

            cout << "请问你还要继续操作吗? 继续请输入Y或者y." << endl;
            cin >> abc ;
    }while(abc == 'Y' || abc == 'y' );


}
搜索更多相关主题的帖子: deposit BankAccount 改改 balance 
2007-12-17 20:29
jiruanl062
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-11-6
收藏
得分:0 
帮下撒
2007-12-17 21:59
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
提问的智慧

Fight  to win  or  die...
2007-12-17 22:01
jiruanl062
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-11-6
收藏
得分:0 
各位大虾知道的能帮下么~~~
  为什么这里会出错?

case 'B':    //正常取钱
            cout << " 请输入取款金额 : " << endl;
            cin >> amount;
            a2.set_amount(amount);
            a2.withdraw();
            break;

        case 'C':    //提前取钱
            cout << " 请输入取款金额 : " << endl;
            cin >> amount;
            a3.set_amount(amount);
            a3.withdraw();
            break;

运行之后 就 取钱 这里出问题

无姓名显示  取钱后 金额出错……
2007-12-17 22:20
jiruanl062
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2007-11-6
收藏
得分:0 
各位大虾知道的能帮下么~~~
  为什么这里会出错?

case 'B':    //正常取钱
            cout << " 请输入取款金额 : " << endl;
            cin >> amount;
            a2.set_amount(amount);
            a2.withdraw();
            break;

        case 'C':    //提前取钱
            cout << " 请输入取款金额 : " << endl;
            cin >> amount;
            a3.set_amount(amount);
            a3.withdraw();
            break;

运行之后 就 取钱 这里出问题

无姓名显示  取钱后 金额出错……
2007-12-19 08:48
快速回复:求助~~~不明白怎么弄了,请求帮改改。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020439 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved