0

There is a question in "Problem Solving and Program Design in C" book.I wrote the code, but loop isn't terminating.

#include <stdio.h>
#include <math.h>
void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies);
int main(void)
{
    int c_dollars, c_quarters = 0, c_dimes = 0, c_nickels = 0, c_pennies = 0;
    double a_paid, a_due, m_change, coin_change;
    printf("Enter the amount paid> ");
    scanf("%lf", &a_paid);
    printf("Enter the amount due> ");
    scanf("%lf", &a_due);
    m_change = a_paid - a_due;
    c_dollars = floor(m_change);
    coin_change = m_change - floor(m_change);
    // shows coin change
    printf("\n%f\n", coin_change);
    change(coin_change, &c_quarters, &c_dimes, &c_nickels, &c_pennies);
    printf("Change is dollars: %d$, quarters: %d, dimes: %d, nickels: %d,\
pennies: %d", c_dollars, c_quarters, c_dimes, c_nickels, c_pennies);
    return(0);
}
void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies)
{
    int q = 0, d = 0, n = 0, p = 0;
     do{
        if(coin_change >= 0.25){
            q++;
            *quarters = *quarters + q;
            coin_change = coin_change - q*0.25;
        }
        else if( coin_change >= 0.10){
            d++;
            *dimes = *dimes + d;
            coin_change = coin_change - 0.1;
        }
        else if( coin_change  >=  0.05){
            n++;
            *nickels = *nickels + n;    
            coin_change = coin_change - (n*0.05);
        }
        else if(coin_change >= 0.01){
            p++;
            *pennies = *pennies + p;
            coin_change = coin_change - (p*0.01);
        }
    }while(coin_change>0);
}

Thank you, I solved problem.The proper code is

#include <stdio.h>
#include <math.h>
void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies);
int main(void)
{
    int c_dollars, c_quarters = 0, c_dimes = 0, c_nickels = 0, c_pennies = 0;
    double a_paid, a_due, m_change, coin_change;
    printf("Enter the amount paid> ");
    scanf("%lf", &a_paid);
    printf("Enter the amount due> ");
    scanf("%lf", &a_due);
    m_change = a_paid - a_due;
    c_dollars = floor(m_change);
    coin_change = (int)((m_change - floor(m_change)) * 100 + 0.5);
    // shows coin change (int)((m_change - floor(m_change)) * 100 + 0.5)
    //coin_change = coin_change * 100;
    printf("\n%f\n", coin_change);

    change(coin_change, &c_quarters, &c_dimes, &c_nickels, &c_pennies);
    printf("Change is dollars: %d$, quarters: %d, dimes: %d, nickels: %d,\
pennies: %d", c_dollars, c_quarters, c_dimes, c_nickels, c_pennies);
    return(0);
}
void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies)
{
    int q = 1, d = 1, n = 1, p = 1;
     do{
        if(coin_change >= 25){
            *quarters = *quarters + q;
            coin_change = coin_change - 25;
        }
        else if( coin_change >= 10){
            *dimes = *dimes + d;
            coin_change = coin_change - 10;
        }
        else if( coin_change  >=  5){
            *nickels = *nickels + n;    
            coin_change = coin_change - 5;
        }
        else if(coin_change >= 1){
            *pennies = *pennies + p;
            coin_change = coin_change - 1;
        }
    }while (coin_change >= 1);
}
4

3 回答 3

1

将货币视为浮点数并不是一个好主意;浮点数具有有限的精度和(更糟糕的)它们可以表示的小数部分的限制。

如果您更好地描述了您的问题,则更容易提供更具体的帮助。

于 2011-04-18T14:34:33.307 回答
1

将双精度数与while(coin_change>0)0 进行比较绝不是一个好主意,因为浮点数的表示不精确。设置一些容差,喜欢0.01并检查是否coin change > 0.01

于 2011-04-18T14:34:35.677 回答
0

在您的 change() 函数中,您应该直接写入变量,例如:

if(coin_change >= 0.25){ *quarters = *quarters ++; coin_change = coin_change - q*0.25; }

您当前的方法将添加许多不必要的硬币。想一想:

`q = 0; //...

if(coin_change >= 0.25){ //真,有条件输入

        q++;   //q == 1

        *quarters = *quarters + q;   //*quarters = 1

        coin_change = coin_change - q*0.25;    //Coin change == .75

    }

//...

if(coin_change >= 0.25){ //真,有条件输入

        q++;   //q == 2

        *quarters = *quarters + q;   //*quarters = 3 ( 1 + 2 )

        coin_change = coin_change - q*0.25;    //Coin change == .50

    }

//...

if(coin_change >= 0.25){ //真,有条件输入

        q++;   //q == 3

        *quarters = *quarters + q;   //*quarters = 6 ( 3 + 3 )

        coin_change = coin_change - q*0.25;    //Coin change = .25

    }

`

您还应该使用整数换硬币,只需将值乘以 100 并跟踪便士,因此 $1 = 100。

此外,您可以使用 4 个 while 循环来获得更清晰的命令:

while(coin_change >= 0.25){ *quarters = *quarters ++; coin_change = coin_change - q*0.25; }

于 2011-04-18T14:44:28.963 回答