gpt4 book ai didi

收银机以 C 语言输出美元和硬币

转载 作者:行者123 更新时间:2023-11-30 15:00:40 29 4
gpt4 key购买 nike

我必须对一个收银机进行编程,让用户输入要支付的金额,它必须输出要支付的金额,然后输出支付该金额所需的加元数量,以及支付该金额所需的季度数。支付金额等...我已经设法计算出要支付的金额以及截断要支付的金额所需的加元数量,但我不知道如何解决需要支付 25 美分、10 美分、5 美分和 10 美分。这是我的代码:

#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: $ %.2f\n", cost);

loonies = cost;
printf ("Loonies required: %d, ", loonies);

loonreq = cost - loonies;
printf ("balance owing: $ %.2f\n", 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: $ %.2f\n", cost);

loonies = cost;
printf ("Loonies required: %d, ", loonies);

loonreq = cost - loonies;
printf ("balance owing: $ %.2f\n", 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;
}

关于收银机以 C 语言输出美元和硬币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41948822/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com