程序在完成之前退出

时间:2018-09-20 21:11:48

标签: c modulo

上下文中的问题

上下文:要提交此程序并正常工作,我需要能够输入8.68作为扫描到的数量。然后,该程序需要能够计算出每个程序有多少个您需要提供硬币类型 x 后的硬币类型以及剩余余额

What the output should look like

问题/问题::我的程序将运行到四分之一秒,并且一切正常,一旦到达零角(零角),程序将中断并退出。 (即使我一开始使用的金额不同,如果硬币的数量也会不同,在这种情况下,硬币的破折点会不同,在这种情况下,角钱的值为0)它将运行并完成与上面显示的输出相同的操作。

代码:

#include <stdio.h>

int main()
{
    double amount;
    double GST = 1.13;
    double balance;
    int numLoonies;
    int numQuarters;
    int numDimes;
    int numNickels;
    int numPennies;
    int bal;

    printf("Please enter the amount to be paid: $"); //ask how much to be paid
    scanf("%lf", &amount);      //scans the input

    GST = amount * .13 + .005;

    printf("GST: 1.13\n");

    balance = (amount + GST);
    printf("Balance owing: $%.2lf\n", balance);

    numLoonies = balance;
    bal = ((balance - numLoonies)*100);

    printf("Loonies required: %d", numLoonies);
    printf(", balance owing $%1.2f\n", (float)bal/100);

    numQuarters = bal / 25;
    bal = bal % (numQuarters*25);

    printf("Quarters required: %d", numQuarters);
    printf(", balance owing $%1.2f\n", (float)bal / 100);

    numDimes = bal / 10;
    bal = bal % (numDimes * 10);

    printf("Dimes required: %d", numDimes);
    printf(", balance owing $%1.2f\n", (float)bal / 100);

    numNickels = bal / 5;
    bal = bal % (numNickels * 5);

    printf("Nickels required: %d", numNickels);
    printf(", balance owing $%1.2f\n", (float)bal / 100);

    numPennies = bal / 1;
    bal = bal % (numPennies * 1);

    printf("Pennies required: %d", numPennies);
    printf(", balance owing $%1.2f\n", (float)bal/100);

    return 0;
}

更新 因此,这是针对学校项目的,应该提到这一点,但是我必须使用mod来找到剩余的余额,并且我必须投放作为条件的一部分加倍为int。

是的,加拿大再也没有几分钱了,但我还是必须这样做

3 个答案:

答案 0 :(得分:1)

  

(即使我一开始使用的金额不同,如果硬币的数量也会不同,在这种情况下,在这种情况下,角钱的值为0)

这是一个大提示。那么,让我们来看一角钱...

bal = bal % (numDimes * 10);

因此,假设numDimes为0。那么(numDimes * 10)也为零。尝试计算bal % 0时会发生什么?如果不确定,可以将该表达式放入程序并尝试。

另一个问题是,我认为您并不是真的要说bal % (numDimes * 10)。假设numDimes等于3可以得到一些初始余额...在这种情况下,您有bal % (3 * 10)bal % 30。那真的是你想要的吗?当您可能真的想要一个小于单个角钱值的数字时,这将给出一个介于0到29之间的数字。如果我有79美分,并且我拿出 7角钱,那么79 % 70确实会给我应该留下的9,但这不是最直观的方法到达那里。

答案 1 :(得分:0)

第1步-将钱转换为最低单位的整数。

由于代码要计入“便士”,因此需要输入并四舍五入/转换为整数。

#include <math.h>
double amount;
printf("Please enter the amount to be paid: $"); //ask how much to be paid
scanf("%lf", &amount);
long iamount = lround(amount * 100);

与GST相同

double GST = 1.13;
long itax = lround(iamount * (GST - 1)); // round to the nearest penny
long ibalance = iamount + itax;
printf("Balance owing: $%.2lf\n", ibalance/100.0);

2)继续分解成硬币

OP的bal = bal % (numCoins*CoinValue);是不正确的方法。

long CoinDenomination = 100; // 100 cents to the Loonie
long iLoonies = ibalance/CoinDenomination;
printf("Loonies required: %ld", iLoonies);
ibalance %= CoinDenomination;

...

CoinDenomination = 10; // 10 cents to the Dime
long iDimes = ibalance/CoinDenomination;
printf("Dimes required: %ld", iDime);
ibalance %= CoinDenomination;

答案 2 :(得分:0)

  

由于计算numDimes的方式,只要numDimes> 0,它将始终产生正确的答案。但是bal%10会产生相同的结果。 –巴尔马

所以我最终做的事情比我原本想做的要容易,我只是使用mod 硬币价值。感谢您的所有评论:)

相关问题