C 现金,预计18/n而非22/n

C 现金,预计18/n而非22/n,c,cs50,C,Cs50,我似乎无法找出我的代码有什么问题,但对于简单的输入,如1或2,我收到了不正确的值,但是对于.41,收到了正确的输入。如果有人能帮助我,我将不胜感激 这是我的代码: #include <stdio.h> #include <cs50.h> #include <math.h> int main(void) { //Establish Variables float amount_owed; int c = 0; //Get Val

我似乎无法找出我的代码有什么问题,但对于简单的输入,如1或2,我收到了不正确的值,但是对于.41,收到了正确的输入。如果有人能帮助我,我将不胜感激

这是我的代码:

#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)

{
    //Establish Variables
    float amount_owed;
    int c = 0;
    //Get Valid Input from User
    do
    {
        amount_owed = get_float ("Change Owed: ");
    }   while (amount_owed <= 0);

    //Check for quarters, mark  # of quarters that can be used, subtract value from original amount_owed
    do
    {
        (c++);
        (amount_owed = amount_owed - .25);
    }   while (amount_owed >= .25);

    //Check for dimes, mark # of dimes that can be used, subtract value from original amount_owed

    do
    {
        (c++);
        (amount_owed = amount_owed - .1);
    }   while ((amount_owed >= .1) && (amount_owed < .25));

    //Check for Nickels, mark $ of nickels that can be used, subtract value from original amount_owed

    do
    {
        (c++);
        (amount_owed = amount_owed - .05);
    }   while ((amount_owed >= .05) && (amount_owed < .1));

    //Check for Pennies, mark # of pennis that can be used, subtract value from original amount_owed

    do
    {
        (c++);
        (amount_owed = amount_owed - .01);
    }   while ((amount_owed >= .01) && (amount_owed < .05));
    //Print Number of Minimum number of coins that can be used

    {
       if (amount_owed == 0)
       ;
        printf("%d\n", c);
    }
}

首先,你不应该在需要精确的事情上使用float。但我们稍后再讨论这个问题,因为您的程序还有另一个问题

现在只需假设float实际上是精确的

当你写作时:

do
{
    c++;
    ...
} while(...);
正文中的代码将始终至少执行一次

因此,对于您的代码,如果输入为1.0,则第一个循环将执行4次,欠下的金额将为0.0

当下一个3做,而你仍然会进入身体一次,并做C++,即使数额是零。因此,第一个循环的结果将是7,而不是4,第二个循环的结果将是4,第三个循环的结果将是1

解决方法是使用常规while而不是do-while。比如:


请定义错误值的含义。您的程序很可能出现浮点错误。如果你想用美分进行精确的计算,通常最好使用整数。在纸上计算出你的逻辑在值为1时的作用。在更改数值之前,是否检查您是否有任何硬币?do/while循环在这里是错误的选择。是的,再次投票给整数。有了整数,你就保证了完美的准确性。此外,对于整数,您可以使用除法的舍入行为和模运算符,这使得逻辑对于任何硬币值都是通用的。建议不要使用浮点类型来处理货币。它们的精度有限,往往会以意外且往往是灾难性的方式对数字进行四舍五入。这可能就是您遇到问题的原因。@Havenard:不建议永远不要对货币使用浮点。浮点运算对于涉及简单算术以外的计算非常有用,例如股票市场期权估值。一个恰当的建议是程序员应该始终理解他们正在使用的工具和技术。
#include <stdio.h>

int main(void) {
    float amount_owed = 1.0;
    int c = 0;

    while (amount_owed >= 0.25)
    {
        c++;
        amount_owed = amount_owed - 0.25;
    }

    while ((amount_owed - 0.1) >= 0)
    {
        c++;
        amount_owed = amount_owed - 0.1;
    }

    while (amount_owed >= .05)
    {
        c++;
        amount_owed = amount_owed - .05;
    }

    while (amount_owed >= .01)
    {
        c++;
        amount_owed = amount_owed - .01;
    }

    printf("%d\n", c);

    return 0;
}
#include <stdio.h>

int main(void) {
    unsigned amount_owed = 100;  // note 100 instead of 1.0
    int c = 0;

    while (amount_owed >= 25)  // note 25 instead of .25
    {
        c++;
        amount_owed = amount_owed - 25;
    }

    while ((amount_owed - 10) >= 0)
    {
        c++;
        amount_owed = amount_owed - 10;
    }

    . . .