0
我想寫一個賬單更換器的代碼,其中插入的金額被轉換回硬幣爲用戶。問題是當我輸入111.111時,我一直保持小數點數爲50c,如222.222。我的20C和10C未使用。請幫助試圖寫一個賬單更換器/硬幣自動售貨亭的代碼
#include <stdio.h>
int main()
{
double sum50c=0, sum20c=0, sum10c=0, remainder, remainder2, remainder3, end=0;
double amount;
do
{
printf("Please enter an amount(dollars):");
scanf("%lf", &amount);
amount=amount*100;
if(amount<0){
printf("Invalid Input\n");
printf("Re-enter your amount:");
scanf("%lf", &amount);
}
if(amount>=50){
remainder=amount/50;
sum50c=remainder;
}else
if(remainder!=0){
remainder2=remainder/20;
sum20c=remainder2;
}else
if(remainder2!=0){
remainder3=remainder3/10;
sum10c=remainder3;
}
if(sum50c>200||sum20c>200||sum10c>200){
end++;
}else{
end=0;
}
}
while(end<=0);
printf("The amount of 50cents=%lf, 20cents=%lf, 10cents=%lf", sum50c, sum20c, sum10c);
}
,則不應使用實數。使用整數,因爲硬幣是分散分佈的,並且沒有5.55美分硬幣。以美分工作(1美分是原子的,不能有任何小於這個數值的東西)。 –
另外,'something/x'給你的商數,而不是餘數。按照建議更改爲整數,然後在'%'(模)運算符上進行讀取。 – Lundin