gpt4 book ai didi

收银机程序 - C

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

美好的一天!在我为学校编写的程序中,我们必须制作一个收银机类型的程序,看起来很简单,但对于我的生活来说,我无法让它工作。在计算出购买的产品数量和所有价格后,程序必须要求现金支付,然后找零。但找零必须以加元(或 1 美元钞票)的形式退还,然后再退还剩余的美分。帮助?我已经让加元开始工作(在某种程度上),但我不知道如何找回零钱。

#include <stdio.h>
#include <stdlib.h>

int main()
{
int itemNum, justChange;
double prodPrice, tax, cashGiven, change, purchasePrice, changeGiven, changeBack, cashBack;
float totalPrice;

//Num of items
printf ("Number of items: ");
scanf("%d", &itemNum);

//Price of items
printf("Please enter price of items: ");
scanf("%lf", &prodPrice);

//Math Stuff
purchasePrice = itemNum*prodPrice;
tax = purchasePrice * 0.13;
totalPrice = purchasePrice*1.13;

//find change alone
//justChange = totalPrice


//Price Output
printf("Purchase price is: %.2lf \n",purchasePrice );
printf("TAX (HST 13%): %.2lf\n",tax );
printf("Total price is: %.2lf \n",totalPrice );



printf("Please Enter Cash: ");
scanf("%lf", &cashGiven);

printf("Please Enter Change: ");
scanf("%lf", &changeGiven);

//MAth stuuff again

double endCash;
double loony;
int yoloswag;
endCash = cashGiven - totalPrice;
loony = endCash/1;
loony = loony--;

if (loony<0)
printf ("Loonies: 0");
else
printf("Loonies: %.0lf \n",loony );

printf("change: %d ", totalPrice-floor(totalPrice) );

return 0;
}

最佳答案

创建一个包含可能更改值的数组;

double cashValues[6] = {1, 0.5, 0.2, 0.1, 0.05, 0.01};

然后创建一个 for 循环,尝试从差值中减去可能的变化值,直到差值为零。

double difference;
difference = cashGiven - totalPrice;
while (difference != 0) {
for(int i=0; i<6; i++) {
if(cashValues[i] <= difference) {
difference -= cashValues[i];
printf("%f \n", cashValues[i]);
i=0;
}
}
}

关于收银机程序 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949873/

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