在货币兑换计划中计算美元 - C ++

时间:2012-09-15 20:44:49

标签: c++ coin-change

请查看以下代码

#include <QtCore/QCoreApplication>
#include <iostream>

int main(int argc, char *argv[])
{
    using namespace std;

    double purchaseAmount;
    double paidAmount;
    float balance;

     int change, quarters, dimes, nickels, pennies, tenDollar, fiveDollar; // declare variables

    cout << "Enter Total purchased amount" << endl;
    cin >> purchaseAmount;

    cout << "Enter Total paid amount" << endl;
    cin >> paidAmount;

    balance = paidAmount - purchaseAmount ;

     tenDollar = balance / 10; // calculate the number of Ten Dollars
     change =   tenDollar  % 10  ; // calculate the change needed
     change = balance * 100;
     quarters = change / 25; // calculate the number of quarters
     change = change % 25; // calculate remaining change needed
     dimes = change / 10; // calculate the number of dimes
     change = change % 10; // calculate remaining change needed
     nickels = change / 5; // calculate the number of nickels
     pennies = change % 5; // calculate pennies

     cout << "\nQuarters: " << quarters << endl; // display # of quarters
     cout << " Dimes: " << dimes << endl; // display # of dimes
     cout << " Nickels: " << nickels << endl; // display # of nickels
     cout <<" Pennies: " << pennies << endl; // display # of pennies
     cout <<" Ten dollar: " << tenDollar << endl; // display # of Ten dollar
     //cout <<" Five dollar: " << fiveDollar << endl; // display # of Ten dollar

     return (0);

 }

我想在这里做什么,计算十美元,四分之一,硬币,镍币和硬币的变化。例如,当我以这种方式运行程序时 -

Enter Total purchased amount
9.75
Enter Total paid amount
20

Quarters: 4
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

哪个错了。话虽这么说,上面的输出是错误的。相反它应该是

Enter Total purchased amount
9.75
Enter Total paid amount
20

Quarters: 1
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

那我在这里做错了什么?

由于

2 个答案:

答案 0 :(得分:1)

正如反复提到的那样,错误在于痛苦的重复代码。请考虑以下事项:

int currencyCount(int& pennies, int penniesInDenomination) {
  const int count = penniesInBase / penniesInDenomination;
  pennies = pennies % penniesInDenomination;

  return count;
}

这可以用于每个面额 - 重复和一行。这通过使函数获得两个值来工作:新余额和该面额的计数。这种方式通过参考取得平衡来作弊,作为副作用&#34;称这显然是独立的功能,它根据返回的面额数量减少余额。显然,你想要记录下来。

...
const int numberOfQuarters = currencyCount(balance, 25);
const int numberOfDimes    = currencyCount(balance, 10);
...

你还可以将货币信息(例如它所代表的便士的名称和数量)放在一个向量中并循环遍历它,执行相同的操作:

typedef std::pair<std::string, int> Currency; 
typedef std::vector<Currency> Currencies;
typedef Currencies::const_iterator CIter;

...

for(CIter iter = currencies.begin(); iter != currencies.end(); ++iter) {
  const int quantity = currencyCount(balance, iter->second);
  std::cout << iter->first << ": " << quantity << std::endl;
}

通过这种方式,您可以避免重复的代码及其涉及的错误。

答案 1 :(得分:0)

如果不更改代码,可以通过替换

来获得所需的答案
change = balance * 100;

change = ((balance) - floor(balance)) * 100;

然而,在你的解决方案中加入更多的思考,我保证你会获得更多的积分,而不是简单的解决方案。另外,你不敢破坏约定,请将你的 使用命名空间std 放在main之外。

像这样:

#include <iostream>
using namespace std; // Place it here! Not inside main.

int main() 
{
 return 0;
}

注意:我说了想要的答案。