收银机,美元和季度输出以 C 格式



我必须编写一个收银机,让用户输入要支付的金额,它必须输出要支付的金额,然后输出支付金额所需的 Loonies 数量,然后是支付金额所需的季度数,等等......我已经设法通过截断要支付的金额来计算出要支付的金额以及所需的加元数量,但我对如何解决需要支付的季度、一角钱、镍币和便士的数量感到茫然。这是我的代码:

#include <stdio.h>
#include <math.h>
int main (void)
{
        double cost;
        int loonies, quarters;
        float loonreq;

        printf ("Please enter the amount to be paid: ");
        scanf ("%lf", &cost);
        printf ("Change Due: $ %.2fn", cost);
        loonies = cost;
        printf ("Loonies required: %d, ", loonies);
        loonreq = cost - loonies;
        printf ("balance owing: $ %.2fn", loonreq);
        return 0;
}

你去吧!

#include <stdio.h>
#include <math.h>
int main (void)
{
    double cost;
    int loonies, quarters, dimes, nickels, pennies;
    float loonreq;
    float balance;

    printf ("Please enter the amount to be paid: ");
    scanf ("%lf", &cost);
    printf ("Change Due: $ %.2fn", cost);
    loonies = cost;
    printf ("Loonies required: %d, ", loonies);
    loonreq = cost - loonies;
    printf ("balance owing: $ %.2fn", loonreq);
    quarters = (int)(((int)(100*loonreq))/25);
    printf("num quarters: %d n",quarters);
    balance = loonreq - (quarters*.25);
    printf("balance owing: %.2f$ n", balance);
    dimes = (int)(((int)(100*balance))/10);
    balance = balance - (dimes*.1);
    printf("num dimes: %d n", dimes);
    printf("balance owing: %.2f$ n", balance);
    nickels = (int)(((int)(100*balance))/5);
    printf("num nickels: %d n", nickels);
    balance = balance - (nickels*.05);
    pennies = (int)(balance*100);
    printf("balance owing: %.2f$ n", balance);
    printf("num pennies: %d n", pennies);

    return 0;
}

最新更新