我刚刚开始编程,所以我知道这可能是一个非常基本的错误,但我一直试图找出如何修复我的代码中的逻辑错误,以便从哈佛大学的CS50课程中获得greedy.c,但没有成功。我已经找到了问题的解决方案,但他们似乎都以与我尝试不同的方式解决了问题。我已经对其他示例进行了逆向工程,现在我对它们有所了解,但我真的想知道如何使我自己的版本运行良好。
我试图通过一系列while循环来完成问题,每个循环从总欠款中减去某个硬币值,并在总硬币数上加一个硬币。对我来说,似乎逻辑上它是有道理的,但是当我运行程序时,它并没有给我预期的输出。它只是不在底部执行printf语句。我希望你们其中一个人能够帮我一臂之力!谢谢你的帮助!
继承我的代码:
#include <stdio.h>
#include <cs50.h>
int main (void)
{
printf("How much change is needed?\n");
float owed = get_float();
int coins = 0;
/*While loops subtracting one coin from change owed, and adding one to coin count*/
while (owed >= 0.25)
{
owed = owed - 0.25;
coins = coins + 1;
}
while (owed >= 0.1)
{
owed = owed - 0.1;
coins = coins + 1;
}
while (owed >= 0.05)
{
owed = owed - 0.05;
coins = coins + 1;
}
while (owed >= 0.01)
{
owed = owed - 0.01;
coins = coins + 1;
}
/*While loops done, now print value of "coins" to screen*/
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
}
编辑:
所以我多玩了一下,并完成了“if”声明。它为我返回错误,那么程序结束时“欠”的值如何不等于0?
#include <stdio.h>
#include <cs50.h>
int main (void)
{
printf("How much change is needed?\n");
float owed = get_float(); //Gets amount owed from user in "x.xx" format
int coins = 0; //Sets initial value of the coins paid to 0
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed > 0.25)
{
owed = owed - 0.25;
coins = coins + 1;
}
while (owed > 0.1)
{
owed = owed - 0.1;
coins = coins + 1;
}
while (owed > 0.05)
{
owed = owed - 0.05;
coins = coins + 1;
}
while (owed > 0.01)
{
owed = owed - 0.01;
coins = coins + 1;
}
//While loops done, now print value of "coins" to screen
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
else
{
printf("Error\n");
}
}
编辑:
所以一旦我的代码工作,我开始摆弄它和过度工程。继续最后(现在)版本!
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <time.h>
int main (void)
{
srand(time(0)); //generates random seed
float price = round(rand()%500); //generates random price between 0 and 500 cents
printf("You owe %f. How much are you paying?\n", price/100); //shows user their price between 0 and 5 dollars
printf("Dollars: ");
float paymnt = get_float()*100; //gets the amount user will pay in dollars then converts to cents
int owed = round (paymnt - price); //calculates the change owed by paymnt-price
int coins = 0; //Sets initial value of the coins paid to 0
int quarters= 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
if (owed ==0 && price >0) //If someone pays in exact
{
printf("You paid the exact amount!\n");
}
else if (owed < 0) //If someone doesn't pay enough
{
printf("You didn't give us enough money!\n");
}
else //Else(We owe them change)
{
printf("Your change is %i cents\n", owed);
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed >= 25)
{
owed = owed - 25;
quarters = quarters + 1;
}
while (owed >= 10)
{
owed = owed - 10;
dimes = dimes + 1;
}
while (owed >= 5)
{
owed = owed - 5;
nickels = nickels + 1;
}
while (owed >= 1)
{
owed = owed - 1;
pennies = pennies + 1;
}
//While loops done, now print each coin and total coins needed to screen
if (owed == 0)
{
coins = quarters + dimes + nickels + pennies;
printf("You need %i coins (%i quarters, %i dimes, %i nickels, and %i pennies)\n", coins, quarters, dimes, nickels, pennies);
}
else
{
printf("Error\n");
}
}
}
答案 0 :(得分:0)
您无法将浮点数与整数(0)进行比较,因为某些深层装配机制
你能做什么:
只需{无条件<{1}}
待办事项
printf("You need %i coins\n", coins)
这实际上是一种很常见的做法
答案 1 :(得分:0)
谢谢大家的帮助。我最终遵循了Tardis的建议,并使用整数进行计算。这是成功的!这是我最终得到的代码:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main (void)
{
printf("How much change is needed?\n");
float tmp = get_float() * 100;//Gets amount owed from user in "x.xx" format
int owed = round(tmp);
int coins = 0; //Sets initial value of the coins paid to 0
//While loops subtracting one coin from change owed, and adding one to coin count
while (owed >= 25)
{
owed = owed - 25;
coins = coins + 1;
}
while (owed >= 10)
{
owed = owed - 10;
coins = coins + 1;
}
while (owed >= 5)
{
owed = owed - 5;
coins = coins + 1;
}
while (owed >= 1)
{
owed = owed - 1;
coins = coins + 1;
}
//While loops done, now print value of "coins" to screen
if (owed == 0)
{
printf("You need %i coins\n", coins);
}
else
{
printf("Error\n");
}
}
喜欢最终获得正确程序的感觉。只是希望我能够自己想出这个:/尽管如此,感谢大家的建议。