本文介绍了计算货币兑换计划中的美元 - 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,nick,pennies,tenDollar,fiveDollar; // declare variables cout<< 输入总购买量< endl; cin>> purchaseAmount; cout<< 输入总支付金额< endl; cin>>已付金额; balance = paidAmount - purchaseAmount; tenDollar = balance / 10; //计算十美元的数量 change = tenDollar%10; //计算所需的更改 change = balance * 100; quarters = change / 25; //计算季度数 change = change%25; //计算所需的剩余变化 dimes = change / 10; //计算dimes数 change = change%10; //计算所需的剩余变化 nickels = change / 5; //计算镍价数量 pennies = change%5; //计算便士 cout<< \\\Quarters:<季度< endl; // display#of quarterters cout<< Dimes:< dimes cout<< Nickels:<<镍 endl; // display#of nickels cout<<Pennies:<<便士< endl; // display panties cout<<Ten dollar:<< tenDollar< endl; // display#of Ten dollar // cout<<Five dollar:<< 5 Dollar< endl; // display#of Ten dollar return(0); } 我在这里要做的,在十美元,宿舍,山丘,镍和便士。例如,当我以这种方式运行程序 - 输入总购买金额 9.75 输入总计支付金额 20 季度:4 Dimes:0 镍:0 硬币:0 十美元:1 这是错误的。话虽这么说,上面的输出是错误的。而应该是 输入总购买金额 9.75 输入总支付金额 20 季度:1 Dimes:0 Nickels:0 Pennies:0 十美元:1 感谢解决方案正如已经反复说的,错误是在痛苦重复的代码。请考虑以下内容: int currencyCount(int& pennies,int penniesInDenomination){ const int count = penniesInBase / penniesInDenomination ; pennies = pennies%penniesInDenomination; return count; } 这可以用于每个面额。这通过使函数获得两个值工作:新的平衡和该面额的计数。这样做通过参考余额作弊,作为称为这个表面上独立的功能的副作用,它根据返还的面额的数量减少余额。显然,你需要记录这个。 ... const int numberOfQuarters = currencyCount ; const int numberOfDimes = currencyCount(balance,10); ... 您也可以将货币信息向量中的数字,并代表它执行相同的事情: typedef std :: pair< std: :string,int>货币; typedef std :: vector< Currency>货币; 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<< :<<数量< std :: endl; } 这种方式避免重复的代码及其涉及的错误。 p> Please take a look at the below code #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); }What I am trying to do here, calculate the change left in ten dollars,quarters, dimes, nickels and pennies. And for example when I run the program this way - Enter Total purchased amount9.75Enter Total paid amount20Quarters: 4 Dimes: 0 Nickels: 0 Pennies: 0 Ten dollar: 1Which is wrong. That being said, the above output is wrong. Rather it should beEnter Total purchased amount9.75Enter Total paid amount20Quarters: 1 Dimes: 0 Nickels: 0 Pennies: 0 Ten dollar: 1So what am I doing wrong here?Thanks 解决方案 As has been repeatedly said, the mistake was in the painfully repeated code. Consider the following:int currencyCount(int& pennies, int penniesInDenomination) { const int count = penniesInBase / penniesInDenomination; pennies = pennies % penniesInDenomination; return count;}This can be used for each and every denomination -- repeatedly and in one line. This works by having the function get two values: the new balance and the count for that denomination. This sorta cheats by taking the balance by reference and, as a "side effect" of calling this apparently stand alone function, it decreases the balance according to the number of denominations returned. Obviously, you would want to document this....const int numberOfQuarters = currencyCount(balance, 25);const int numberOfDimes = currencyCount(balance, 10);...You could also put the currency information (such as the name and the number of pennies it represents) in a vector and loop over it performing the same thing: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;}In this manner, you avoid repeated code and the mistakes it involves. 这篇关于计算货币兑换计划中的美元 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 22:40