我刚刚完成了我的更改计算器(根据购买成本和现金计算变更)。我正在测试它是否有效并发现了一个错误。每当要给出的便士数量恰好为1时,就说它是0.我仔细检查了代码并手动完成了我编写的代码(不运行它),看起来应该可行。如果您想自己测试一下,只需将0作为购买成本,将36.91作为现金。 (这意味着变化是36.91,应该是1 20,1 10,1 5,1半美元,1季度,1角钱,1镍和1便士[但它说0便士]。
适当的注意:我对C ++很陌生并且非常了解
以下是代码:
/*This program simulates a situation at the register where someone pays with cash
and the cashier needs to know how much change and of which coins he or she needs to give.
Written by Jay Schauer
*/
//Data Declarations
#include <iostream>
#include <cstdint>
int main()
{
using namespace std;
double cost; //Stores cost of purchase
cout << "Input cost of purchase (in USD)" << endl;
cin >> cost;
double cash; //Stores cash that pays for purchase
cout << "Input cash given (in USD)" << endl;
cin >> cash;
double changet = cash - cost; //Stores the change,
the t is for temporaary because I use a different change variable later
cout << "Change to be given is " << changet << " in..." << endl;
//Also, I thought this was pretty clever since doubles apparantly can't be used
//with modulus (it gave me an error),
//so I figured I just multiply it by 100 and multiply all the divisions by 100 also
changet *= 100;
int change = changet; //Converts changet to an integer to be used with the modulus
int coins; //Stores the amount of "coins" to be given as change
coins = change / 2000;
cout << coins << " twenty dollar bills" << endl;
change = change % 2000;
coins = change / 1000;
cout << coins << " ten dollar bills" << endl;
change = change % 1000;
coins = change / 500;
cout << coins << " five dollar bills" << endl;
change = change % 500;
coins = change / 100;
cout << coins << " one dollar bills" << endl;
change = change % 100;
coins = change / 50;
cout << coins << " half dollars" << endl;
change = change % 50;
coins = change / 25;
cout << coins << " quarters" << endl;
change = change % 25;
coins = change / 10;
cout << coins << " dimes" << endl;
change = change % 10;
coins = change / 5;
cout << coins << " nickels" << endl;
change = change % 5;
//There is one problem that I can't figure out
//If the number of pennies to be given for change is exactly 1,
//it says 0 for the number of pennies to be given as change
coins = change / 1;
cout << coins << " pennies" << endl;
system("pause");
return 0;
}
答案 0 :(得分:4)
不幸的是,您在使用双点和浮点值时遇到了一个常见问题。它们并不准确。它们非常适合摇滚乐,但是当你想要精确的时候,那就好了....
好的,所以你有36.91 ......那种。它真的像36.90999999999。 你将它乘以100得到3690.999999999。 您将其转换为整数并截断为3690.该分数被完全丢弃。
你应该可以通过rounding 在这里工作。
int change = std::round(changet);
答案 1 :(得分:2)
当你应该使用浮点除法时,这段代码依赖于整数除法。简而言之,您将截断答案的小数部分。
让我们一步一步,使用您输入的$ 36.91。首先,20美元的账单:
coins = change / 2000;
- &gt; coins = 3691 / 2000;
- &gt; coins = 1.8455;
因为我们处理整数,所以它属于整数除法,coins
实际上设置为1
。一个20美元的账单将作为变更。
继续,10美元的账单:
change = change % 2000;
- &gt; change = 3691 % 2000;
- &gt; change = 1691;
coins = change / 1000;
- &gt; coins = 1691 / 1000;
- &gt; coins = 1.691;
再次,这属于整数除法,因此1.691被截断,coins
被设置为1.该程序将指示一张10美元的账单到期。
现在,5美元的账单......
change = change % 1000;
- &gt; change = 1691 % 1000;
- &gt; change = 691;
coins = change / 500;
- &gt; coins = 691 / 500;
- &gt; coins = 1.382;
1.382的小数部分再次被删除,coins
被设置为1.这是一个5美元的账单。
change = change % 500;
- &gt; change = 691 % 500;
- &gt; change = 191;
coins = change / 100;
- &gt; coins = 191 / 100;
- &gt; coins = 1.91;
1.91被截断为1.这是一张1美元的账单。
change = change % 100;
- &gt; change = 191 % 100;
- &gt; change = 91;
coins = change / 50;
- &gt; coins = 91 / 50;
- &gt; coins = 1.82;
再一次,那被截断了。 1.82被转换成一个整数设置为1.半美元。
change = change % 50;
- &gt; change = 91 % 50;
- &gt; change = 41;
coins = change / 25;
- &gt; coins = 41 / 25;
- &gt; coins = 1.64;
四分之一。
change = change % 25;
- &gt; change = 41 % 25;
- &gt; change = 16;
coins = change / 10;
- &gt; coins = 16 / 10;
- &gt; coins = 1.6;
一角钱。
change = change % 10;
- &gt; change = 16 % 10;
- &gt; change = 6;
coins = change / 10;
- &gt; coins = 6 / 5;
- &gt; coins = 1.2;
一个镍。
change = change % 5;
- &gt; change = 6 % 5;
- &gt; change = 1;
coins = change / 1;
- &gt; coins = 1 / 1;
- &gt; coins = 0.9999999999999;
零便士。
这是二进制算术处理除法的原因。精度损失导致计算0.9999999,截断时将导致零便士。
您的解决方案:由于将某事物除以1会产生相同的内容(身份),请从您的程序中取出coins = change / 1;
。
另外,搜索有关浮点运算的文章。