给出最小变化量的C++程序对于最低面值不起作用。
这样做的目的是使纸币/硬币的最小数量有一定的变化。到目前为止,我的代码适用于所有值,但包括2p或1p硬币的值除外,例如12.33给出了1英镑10、1英镑2、1英镑20、1英镑10和1英镑12,但没有1p。我也试过了2p,它可以得到1p,1p可以得到0个硬币。我无法找出这些特定值的错误,因为每个面额的代码都是相同的给出最小变化量的C++程序对于最低面值不起作用。,c++,C++,这样做的目的是使纸币/硬币的最小数量有一定的变化。到目前为止,我的代码适用于所有值,但包括2p或1p硬币的值除外,例如12.33给出了1英镑10、1英镑2、1英镑20、1英镑10和1英镑12,但没有1p。我也试过了2p,它可以得到1p,1p可以得到0个硬币。我无法找出这些特定值的错误,因为每个面额的代码都是相同的 #include <iostream> using namespace std; int main() { float change; int fifty =
#include <iostream>
using namespace std;
int main()
{
float change;
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;
int two = 0;
int one = 0;
int fiftyp = 0;
int twentyp = 0;
int tenp = 0;
int fivep = 0;
int twop = 0;
int onep = 0;
cout << "Please enter the amount of change given:\n";
cin >> change;
while ( change >= 50)
{
fifty++;
change -= 50;
}
while ( change >= 20)
{
twenty++;
change -= 20;
}
while ( change >= 10)
{
ten++;
change -= 10;
}
while ( change >= 5)
{
five++;
change -= 5;
}
while ( change >= 2)
{
two++;
change -= 2;
}
while ( change >= 1)
{
one++;
change -= 1;
}
while ( change >= 0.5)
{
fiftyp++;
change -= 0.5;
}
while ( change >= 0.2)
{
twentyp++;
change -= 0.2;
}
while ( change >= 0.1)
{
tenp++;
change -= 0.1;
}
while ( change >= 0.05)
{
fivep++;
change -= 0.05;
}
while ( change >= 0.02)
{
twop++;
change -= 0.02;
}
while ( change >= 0.01)
{
onep++;
change -= 0.01;
}
cout << "Give " << fifty << " £50 notes.\n";
cout << "Give " << twenty << " £20 notes.\n";
cout << "Give " << ten << " £10 notes.\n";
cout << "Give " << five << " £5 notes.\n";
cout << "Give " << two << " £2 coins.\n";
cout << "Give " << one << " £1 coins.\n";
cout << "Give " << fiftyp << " 50p coins.\n";
cout << "Give " << twentyp << " 20p coins.\n";
cout << "Give " << tenp << " 10p coins.\n";
cout << "Give " << fivep << " 5p coins.\n";
cout << "Give " << twop << " 2p coins.\n";
cout << "Give " << onep << " 1p coins.\n";
return 0;
}
问题是float不能代表所有的十进制数,0.02实际上略小于0.02,0.01小于0.01
补救办法是对代表最小面额的整数进行计算,即一便士
让用户以便士为单位输入金额,或者读取您解析的字符串。
后一种方法更为复杂且用户友好,但首先使用前一种方法获得工作程序可能是一个好主意。
这两种方法都可以忽略无意义的数量,如3.14159265
<>我也建议你在你的精选C++书籍中阅读循环和数组。< /P> < P>问题是浮点数学。考虑在程序末尾添加下面的行:
cout << "Change left: " << change << '\n';
很明显,这小于0.01。虽然你可以尝试添加模糊因子来处理与完美数的微小偏差,但通常最好使用整数来表示货币,例如1等于1美分。您应使用适当的舍入进行此转换:
long int cents = lrintf(change * 100.0);
然后像这样替换while循环:
while (cents >= 5000) {
fifty++;
cents -= 5000;
}
等等。下面/上面的答案解决了你实际提出的问题 但在我看来,我认为你问自己的问题并不正确 下面是使用数组制作程序的另一种更简单的方法:
如果你想在任何时候改变硬币的数量/价值,考虑使用向量代替数组< /p>正确的工具来解决这些问题是你的调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包含一个重现您的问题的示例,以及您在调试器中所做的观察。请改用模运算。也可能是一个面值数组。你可能还想考虑迁移到pNess中,只使用一个整数。下面的所有答案都是正确的,但是你应该重新思考你实际编码的方式,试着学习。functions@EdHeal变化>=0.01-否,poundsUpvote建议使用整数值进行货币计算。一旦有了整数,就可以使用整数除法和模运算来摆脱while循环,如:50=美分/5000;仙%=5000;二十美分=美分/2000。。。啊,谢谢!我将把输入改为便士。出于兴趣,为什么浮点数不是精确的值?@user193369请看。坏主意,仍然使用双硬币[],甚至硬编码数字12。将硬币数量放入常量值或使用向量,两者都比上面的代码好
while (cents >= 5000) {
fifty++;
cents -= 5000;
}
int main(){
double change;
double coins[12]{ 50,20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01 }; //All coins that can be given
int coinsChanged[12]{0,0,0,0,0,0,0,0,0,0,0,0}; //All coins that are going to be given
std::cout << "Please enter the amount of change given:\n";
std::cin >> change;
while (change >= 0.01) { //If the amount gets below the minimum coin you can give, the progrm stops
for (int i = 0; i < 12; ++i) //Checks all available coins one by one
while (change >= coins[i]) {
coinsChanged[i]++;
change -= coins[i]; //Add the coin to the "giving" array
}
}
for (int i = 0; i < 12; ++i)
std::cout << "Give : " << coinsChanged[i] << " £" << coins[i] << " notes" << std::endl; // Prints everything
return 0;
}