C ++努力使美元退出并使美元正常化

时间:2018-03-20 21:15:45

标签: c++

我已将其他所有设置正确并且正常工作,但我已经让我的大脑努力让我的提款设置正常工作。我只需要配方的帮助,除了当总美分低于0

时它才能正常工作
void SavingsAccount::deposit()
{
    int dollarHold, centHold, holder;
    cout << "Please input the dollars to be deposited: ";
    cin >> dollarHold;
    cout << "Please input the cents to be deposited: ";
    cin >> centHold;
    if (centHold > 99)
    {
        holder = centHold / 100;
        centHold -= (holder * 100);
        dollarHold += holder;
    }
    dollars += dollarHold;
    cents += centHold;
    if (cents > 99)
    {
        holder = cents / 100;
        cents -= (holder * 100);
        dollars += holder;
    }
    cout << "Dollar: " << dollars << " Cents: " << cents << endl;
}

void SavingsAccount::withdrawl()
{
    int dollarHold, centHold, holder;
    cout << "Please input the dollars to be withdrawn: ";
    cin >> dollarHold;
    dollarHold *= -1;
    cout << "Please input the cents to be withdrawn: ";
    cin >> centHold;
    centHold *= -1;

    if (centHold < 0)
    {
        holder = centHold / 100;
        centHold -= (holder * 100);
        dollarHold += holder;
    }

    dollars += dollarHold;
    cents += centHold;
    if (cents < 0)
    {
        holder = cents / 100;
        cents += (holder * -100);
        dollars -= holder;
    }

    cout << "Dollar: " << dollars << " Cents: " << cents << endl;
}

1 个答案:

答案 0 :(得分:1)

将余额以美分存储并以美元和美分显示会更容易。

然后,函数将简化为:

void SavingsAccount::deposit()
{
    int dollars, cents;
    cout << "Please input the dollars to be deposited: ";
    cin >> dollars;
    cout << "Please input the cents to be deposited: ";
    cin >> cents;

    int total = dollars*100 + cents;

    // Assuming balance is the new member variable and stored in cents.
    balance += total;

    dollars = balance/100;
    cents = balance%100;
    cout << "Dollar: " << dollars << " Cents: " << cents << endl;
}

void SavingsAccount::withdrawl()
{
    int dollars, cents;
    cout << "Please input the dollars to be withdrawn: ";
    cin >> dollars;
    cout << "Please input the cents to be withdrawn: ";
    cin >> cents;

    int total = dollars*100 + cents;

    // Assuming balance is the new member variable and stored in cents.
    balance -= total;

    dollars = balance/100;
    cents = balance%100;
    cout << "Dollar: " << dollars << " Cents: " << cents << endl;
}

但是,如果您必须以美元和美分存储数据,则可以通过在函数中将所有内容转换为美分来简化成员函数。

void SavingsAccount::deposit()
{
    int dollarHold, centHold, holder;
    cout << "Please input the dollars to be deposited: ";
    cin >> dollarHold;
    cout << "Please input the cents to be deposited: ";
    cin >> centHold;

    int total = (dollars + dollarHold)*100 + (cents + centHold);

    dollars = total / 100;
    cents = total % 100;

    cout << "Dollar: " << dollars << " Cents: " << cents << endl;
}

void SavingsAccount::withdrawl()
{
    int dollarHold, centHold, holder;
    cout << "Please input the dollars to be withdrawn: ";
    cin >> dollarHold;
    cout << "Please input the cents to be withdrawn: ";
    cin >> centHold;

    int total = (dollars - dollarHold)*100 + (cents - centHold);

    dollars = total / 100;
    cents = total % 100;

    cout << "Dollar: " << dollars << " Cents: " << cents << endl;
}