我终于让我的更改程序打印出一些数字而不是抛出浮点异常。然而,现在它稍微偏离了。该程序没有给出 2 个四分之一、1 个硬币、1 个镍和 2 个便士,而是只选择了 1 个便士,我无法通过对代码的条件或结构的任何检查来找出原因。我在每个 if 语句中添加了一些检查,但甚至没有打印出来,我现在非常困惑..
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int changeF(float change, float div)
{
int c = change/div;
return c;
}
int main(void)
{
printf("Enter the bill amount:");
float bill = GetFloat();
printf("Enter the payment amount:");
float payment = GetFloat();
float q = .25; float di = .1; float n = .05; float p = .01;
float change = payment - bill;
do
{
if (bill >= 0.0 && payment >= 0.0 && change >= 0.0)
{
if (change >= q)
{
int quarters = changeF(change, q);
printf("%d quarters\n", quarters);
change = change - (.25*quarters);
}
if (change >= di)
{
int dimes = changeF(change, di);
printf("%d dimes\n", dimes);
change = change - (.1*dimes);
}
if (change >= n)
{
int nickels = changeF(change, n);
printf("%d nickels\n", nickels);
change = change - (.05*nickels);
printf("%f change", change);
}
if (change >= p)
{
int pennies = changeF(change, p);
printf("%d pennies\n", pennies);
change = change - (.01*pennies);
printf("%f", change);
}
}
}
while (change > 0.0);
}