代码是:
int main()
{
displayOwe(getOwe(), customerpaid());
displayChange(dollars, quarters, dimes, nickels, pennies);
return 0;
}
float getOwe()
{
float owed;
printf("\nHow much does the customer owe? ");
scanf("%f" ,&owed );
return owed;
}
float customerpaid()
{
float paid;
printf("\nHow much did the customer pay? ");
scanf("%f" ,&paid );
return paid;
}
float calculateTotal (float paid, float owed)
{
float answer;
answer = paid - owed;
return answer;
}
float displayOwe(float owed , float paid)
{
printf("\nCustomer owes $%0.2f and paid $%0.2f.", owed, paid);
}
int displayChange(int dollars, int quarters, int dimes, int nickels, int pennies)
{
dollars= ;
quarters=
dimes=
nickels=
pennies=
printf("\n\nCashier should refund %d dollars, %d quarters, %d dimes, %d nickels, and %d pennies.", dollars, quarters, dimes, nickels, pennies);
}
我必须创建一个C程序来计算更改,以显示客户欠的金额和客户支付的金额。我做了那部分,它工作正常,但是我不知道如何使程序显示收银员需要返还给客户多少美元,四分之一,一角钱,一分钱和几美分。任何帮助,将不胜感激。
最佳答案
displayChange需要两个额外的参数-欠费,已付浮动。其他参数应该是引用“&”,因为它们是输出参数。
在displayChange内部,我会将float转换为ints:
int iOwed = owed * 100;
int iPaid = paid * 100;
现在,一分钱是1,而不是0.01,一美元是100,而不是1;
将它们设置为整数后,首先进行更改:
int change = iPaid - iOwed;
然后通过从最大货币单位到最小货币单位来计算每个债务的欠款-先除法,然后得到余数。
dollars = change / 100;
使用模运算符获得余数:
remainder = change % 100;
然后继续使用25、10、5和1来计算四分之一,一角硬币,镍币和几美分的剩余值。
quarters = remainder / 25;
remainder = remainder % 25;