2012-09-15 44 views
1

请看看下面的代码计算美元就在货币改革计划 - C++

#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 

那么我在这里做错了什么?

感谢

+0

那么我在这里做错了什么? - 期待别人做你的功课我猜... – dtech

+0

不,我不是..我自己开始,寻求帮助。 – user15169

+0

我们似乎已经丢失了下面的代码... –

回答

0

没有太大变化你的代码,你可以通过更换

change = balance * 100; 

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

然而,把更多的想进入你的解决方案得到您想要的答案,而且我保证你会得到更多的积分,而不是简单的解决方法。另外,你不敢打破约定,请使用名称空间标准以外的主要位置您的

像这样:

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

int main() 
{ 
return 0; 
} 

注:我说想要的答案。

+0

因为什么时候在文件范围内放置'using namespace std;'是个好主意,更不用说'约定'了? –

+1

因为永远。它不是文件范围。其*文件范围。恶魔之穴将其放入一个共享*标题*文件中。你提供的源文件是好的(我个人从来没有使用它,但如果你打算,c/cpp是地方;最肯定的是从来没有头文件。 – WhozCraig

1

正如人们一再说过的,错误在于痛苦的重复代码。考虑以下几点:

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

    return count; 
} 

这可以用于每个面额 - 重复和一行。这可以通过让函数得到两个值:新的余额和该面额的计数。这种做法通过引用平衡来作弊,并且作为将此称为独立功能的“副作用”,它会根据返回的面额数量减少余额。很明显,你会想记录这一点。

... 
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; 
} 

以这种方式,你避免了重复的代码和它涉及的错误。