-1

我正在尝试制作一个提供最少硬币数量的程序,但如果我提供的数字不是分成四等份的数字,它就会惨遭失败。例如,如果我输入 1.25,我得到 5 个四分之一,但如果我输入 1.26,我得到 5 个四分之一 1 镍,这当然是不正确的。我究竟做错了什么?

#include <stdio.h>
#include <cs50.h>
#include <math.h>

void calculate_change(change_requested){   
        int num_quarters = 0;
        int num_dimes = 0;
        int num_nickles = 0;
        int num_pennies = 0;

        int val_quarters = 25;
        int val_dimes = 10;
        int val_nickles = 5;

        num_quarters = floor(change_requested/val_quarters);
        if(change_requested % val_quarters != 0){
            num_dimes = floor( (change_requested - (num_quarters * val_quarters))/val_dimes );
            if( change_requested - (((num_quarters * val_quarters) + (num_dimes * val_dimes))) != 0){
                num_nickles = floor( change_requested - ( (num_quarters * val_quarters) + (num_dimes * val_dimes)/val_nickles ));
                if( change_requested - (((num_quarters * val_quarters) + (num_dimes * val_dimes) + (num_nickles * val_nickles))) != 0){
                    for(int i = 0; i<change_requested - (((num_quarters * val_quarters) + (num_dimes * val_dimes) + (num_nickles * val_nickles))); i++){
                        num_pennies++;
                    }
                }
            }
        }
        if(num_quarters > 0){
            printf("%i Quarters ",num_quarters);
        }
        if(num_dimes > 0){
            printf("%i Dimes ",num_dimes);
        }
        if(num_nickles > 0){
            printf("%i Nickles",num_nickles);
        }
        if(num_pennies > 0){
            printf("%i Pennies",num_pennies);
        }
        printf("\n");

    }

int main (void){
    printf("How Much Change Do You Need?\nAmount: ");
    float change_requested = GetFloat();
    calculate_change(change_requested * 100);
    return 0;
}
4

3 回答 3

4

I feel you are overcomplicating this. If you need to write

for(int i = 0; i<change_requested - (((num_quarters * val_quarters) + (num_dimes * val_dimes) + (num_nickles * val_nickles))); i++){

and all this in one line, then something is certainly wrong. You can do this in a much simpler way, by subtracting the value of the coins just calculated from the total amount in order to to obtain the rest:

int vals[] = { 25, 10, 5 };
const char *names[] = { "quarters", "dimes", "nickles" };

int pennies = 100 * GetFloat(); // not good at all, btw (*)

for (int i = 0; i < 3; i++) {
    int n = pennies / vals[i];

    if (n) {
        printf("%d %s ", n, names[i]);
    }

    pennies -= n * vals[i];
}

if (pennies)
    printf("%d pennies", pennies);

printf("\n");

As to why GetFloat() is not good: floating-point numbers aren't exact, so, for example, 1.26 may be actually represented as something like 1.25999946. When you convert this to an integral value, you may lose a penny or so due to truncation if you are unlucky.

于 2013-08-01T04:55:06.970 回答
1

您的代码中的括号已关闭。看一下这一行:

num_nickles = floor( change_requested - ( (num_quarters * val_quarters) + (num_dimes * val_dimes)/val_nickles ));

您的代码首先要评估(num_quarters * val_quarters)哪个是125. 然后它评估(num_dimes * val_dimes)结果是0。然后它除以(num_dimes * val_dimes)whichval_nickles也出来0,然后将它添加0(num_quarters * val_quarters)。因此,这行代码的本质是:
num_nickles = floor( 126 - 125 + 0)结果是1.

于 2013-08-01T05:51:58.390 回答
0
#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    // four kinds of coins as constant variables
    const int quarter = 25, dime = 10, nickel = 5;
    float change;
    unsigned int changeCoin, count, reminder;

    do
    {
        printf ("O hai! How much change is owned?\n");
        change = GetFloat();
    }
    while (change < 0);

    // convert input into cents
    changeCoin = round (change * 100);

    // count the coins
    count = changeCoin / quarter;
    reminder = changeCoin % quarter;

    count += reminder / dime; 
    reminder %= dime;

    count += reminder / nickel;
    reminder %= nickel;

    count += reminder;

    printf ("%d\n", count);         
}
于 2015-10-30T04:15:29.073 回答