代码是:
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程序来计算变化,该变更显示客户欠多少以及客户付多少钱。我做了那部分,它完美无瑕地工作,但我不知道如何使程序显示出纳员需要返回给客户多少美元,四分之一,硬币,镍币和便士。任何帮助将不胜感激。
答案 0 :(得分:1)
displayChange需要两个额外的参数 - 浮动欠款,浮动支付。其他参数应为“&”因为它们是输出参数。
在displayChange中我会将float转换为int:
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;