计算变化 [英] Counting change

查看:100
本文介绍了计算变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,但我有很多其他语言的经验。我编写的这个脚本提示用户输入更改金额,然后计算金额的每个硬币的金额。例如,37美分是1个季度,1个角钱和2个便士。我有一个while循环,但它不会循环,它不应该

展开 | 选择 | Wrap < span class =codeDivider> | 行号

解决方案

< blockquote>


我是C ++的新手,但我有很多其他语言的经验。我编写的这个脚本提示用户输入更改金额,然后计算金额的每个硬币的金额。例如,37美分是1个季度,1个角钱和2个便士。我有一个while循环,但它不会循环,它不应该

展开 | 选择 | Wrap < span class =codeDivider> | 行号


你的问题''如果你进行测试并观察输入值,那就显而易见了。

如果你这样做,你会注意到它一直停留在0.01。


我用他的例子来解释一下:

37这意味着输入= 37;

现在25个被扣除一个季度,所以剩下12个。

现在10扣除一分钱,所以剩下2。

现在一分钱被扣除所以输入= 0.01。


您的测试存在问题,输入= 0.01但您测试的是>所以你需要替换你的>使用> =并且它会工作。


关于你的程序的问题,不是四分之一= 25美分?

因为在你的程序你扣除0.25美分所以你将得到大约140个季度37。



和你的程序有关的问题,没有ta quarter = 25美分?

因为在你的程序中你扣除了0.25美分所以你将获得大约140个季度的37美元。



如果输入是美元,则不是:-)


然而,在while循环中针对0.00的最终测试是个问题。正如gpraghuram所说使用float或double变量来测试逻辑条件几乎总是一个坏主意,因为这些变量类型只有一个近似值,这意味着一些最不重要的小数位可能不会保持你期望的值,但这些地方确实影响了逻辑结果。


在我看来,最好将输入从美元转换为美分,然后从可以保持整数值开始,然后进行计算使用整数提供的更精确的数学分数

展开 | 选择 | Wrap | 行号


I''m brand new to C++, yet I have experience in many other languages. This script I wrote prompts the user for a change amount, then calculates the amounts of each coin that amount is. For example, 37 cents is 1 quarter, 1 dime, and 2 pennies. I have a while loop but it keeps looping when it shouldn''t

Expand|Select|Wrap|Line Numbers

解决方案

I''m brand new to C++, yet I have experience in many other languages. This script I wrote prompts the user for a change amount, then calculates the amounts of each coin that amount is. For example, 37 cents is 1 quarter, 1 dime, and 2 pennies. I have a while loop but it keeps looping when it shouldn''t

Expand|Select|Wrap|Line Numbers


Your problem''s a bit obvious if you run a test and watch the value of input.
If you do you''ll notice it stays stuck at 0.01.

I''ll explain it with his example:
37 this means that input = 37;
now 25 gets deducted for a quarter so 12 is left.
now 10 gets deducted for a dime so 2 is left.
And now a penny gets deducted so input = 0.01.

There problem is with your test, input = 0.01 but you test for > so you need to replace your > with >= and it''ll work.

And a question about your program, doesn''t a quarter = 25 cents?
Because in your program you deduct 0.25 cents so you''ll get about 140ish quarters for 37.


And a question about your program, doesn''t a quarter = 25 cents?
Because in your program you deduct 0.25 cents so you''ll get about 140ish quarters for 37.

Not if the input is in dollars :-)

However the final test against 0.00 in the while loop is a problem. As gpraghuram says using a float or double variable to test for a logical condition is nearly always a bad idea because these variable types only hold an approximation which means that some of the least significant decimal places may not hold the value you are expecting but these places do effect the logical outcome.

In my opinion it would be better to convert the input from dollars to cents to start with which can be held in an integer value and then do your calculations in cents with the more precise mathematics that integers provide

Expand|Select|Wrap|Line Numbers


这篇关于计算变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆