2017-06-30 32 views
-2

我刚开始编程,所以我知道这可能是一个很基本的错误,但我一直在试图找出如何在我的代码修复逻辑错误对哈佛大学CS50课程的greedy.c作业没有成功。我已经寻找解决问题的办法,但他们似乎都以不同于我尝试的方式解决问题。我已经反向设计了其他例子,现在我明白了,但我真的想知道如何让自己的版本运行良好。新的程序员,我需要帮助greedy.c哈佛大学CS50

我想获得的一系列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); 
    } 
} 

编辑:

所以我发挥它周围多一点,并完成了 “如果” 语句。它对我来说会返回错误,那么程序结束时“欠”的值如何不等于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

对于某些示例输入,预期输出是什么?什么是*实际*输出?请修改您的问题以包含这些详细信息。同时请花些时间阅读Eric Lippert的[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+0

谢谢尤金。我将它改为整数计算。好主意。 –

回答

0

你可以不是一个真正的浮点数字与为一个整数(0),因为一些深组装机制的

你可以做什么:

  1. 根本就没有printf("You need %i coins\n", coins)条件

  2. if (owed <= 0.001 && owed >= -0.001) 
    { 
        printf("You need %i coins\n", coins); 
    } 
    

    它实际上是一个相当普遍的做法

+0

或者OP可以遵循EugeneSh。的评论,并且执行整数算术并且以美分完成所有的计数。 – Tardis

+0

谢谢!我正在想这些方面。我试图把它放到我的代码中,而且我仍然收到错误。我将值转换为if(欠款<0.01 &&欠款> -0.01) printf(“您需要%i硬币\ n”,硬币); } else { printf(“Error \ n”); },它仍然无法正常工作。有任何想法吗? –

+0

@ Dr.B是打印错误还是不循环 –

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"); 
    } 
} 

爱最后得到一个程序权的感觉。只希望我能够自己想出这件事:/然而,谢谢大家的建议。货币会计

+0

我可以这样写吗? 'float owed = round(get_float()* 100);'并删除了对tmp变量的需要? –