简单的C收银机程序

时间:2019-01-26 02:37:10

标签: c types floating-point double

程序要求输入价格,然后在其中添加gst(13%)。然后,应输出所需支付的不同硬币数量(卢尼= 1美元,四分之一= 0.25,角钱= 0.1,镍= 0.05,便士= 0.01)。

实际上,我几乎可以自己完成所有工作,但是最后应该输出几分钱的部分对我来说却无法正常工作。

例如: 当我输入8.32时,这就是我得到的


请输入付款金额:$ 8.32

GST:1.09

欠款:9.41

需要的行李箱:9,欠款$ 0.41

所需季度:1,余额$ 0.16

所需资金:1,欠款$ 0.06

所需镍:1,欠款$ 0.01

所需硬币:0,余额为$ 0.01


很显然,倒数第二行显示有0.01的未偿余额,但是程序告诉您需要0便士。

我认为问题出在类型上,但是对我来说还不是很清楚。

代码

                                                                            // Presenting needed variables for this task
double amount;                                                          // We ask a customer to input the amount
int loonies, quarters, dimes, nickels, pennies;                                                         // Loonies and quarters represent single number of coins, therefore they are integer values
float owing, gst;

printf("Please enter the amount to be paid: $");                                // Asking to enter the input
scanf("%lf", &amount);

gst = amount * .13 + .005;
printf("GST: %.2f\n", gst);

owing = amount + gst;
printf("Balance owing: %.2f\n", owing);

loonies = owing;                                                                // By assigning loonies to amount, we calculate the integer amount of loonies we can fit into the amount number
owing = owing - loonies;                                                    // Calculating the owing amount left to pay


printf("Loonies required: %d, balance owing $%.2f\n", loonies, owing);          // Showing the amount of required loonies and the rest of owing balance

quarters = owing / 0.25;                                                        // Calculating amount of quarters to pay
owing = owing - quarters * 0.25;                                                    // Calculating the rest of the amount left to pay

printf("Quarters required: %d, balance owing $%.2f\n", quarters, owing);        // Showing the amount of required quarters and the rest of owing balance

dimes = owing / 0.1;
owing = owing - dimes * 0.1;

printf("Dimes required: %d, balance owing $%.2f\n", dimes, owing);

nickels = owing / 0.05;
owing = owing - nickels * 0.05;

printf("Nickels required: %d, balance owing $%.2f\n", nickels, owing);


pennies = owing / 0.01;
owing = owing - pennies * 0.01;

printf("Pennies required: %d, balance owing $%.2f\n", pennies, owing);


return 0;
}

0 个答案:

没有答案
相关问题