运行我的函数时,它不会返回我一个值,我做错了什么?

When running my function it won't return me a value, what am I doing wrong?

提问人:Depps 提问时间:5/18/2023 更新时间:5/18/2023 访问量:76

问:

我目前正在处理 CS50 第 1 周的现金任务,一切都很顺利,直到我过去几个季度。目标是从用户那里获得关于他们欠多少零钱的输入,然后创建函数来找到尽可能少的最小零钱(即欠 60 美分,然后它会显示 3 代表 2 个季度和 1 角钱)。 顶部是已经提供的内容:

#include <cs50.h>
#include <stdio.h>

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);

int main(void)
{
    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    int quarters = calculate_quarters(cents);
    cents = cents - quarters * 25;

    // Calculate the number of dimes to give the customer
    int dimes = calculate_dimes(cents);
    cents = cents - dimes * 10;

    // Calculate the number of nickels to give the customer
    int nickels = calculate_nickels(cents);
    cents = cents - nickels * 5;

    // Calculate the number of pennies to give the customer
    int pennies = calculate_pennies(cents);
    cents = cents - pennies * 1;

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf("%i\n", coins);
}

现在我实现了 while 函数,因为我知道我希望它只在特定条件下执行此操作,但我无法让它将特定函数返回回 main。 这就是我所做的一切:

int get_cents(void)
{
    int cents;
    do{
        cents = get_int("Change Owed: ");
    }
    while (cents<0);
    return cents;
}

int calculate_quarters(int cents)
{
    // TODO
    int quaters;
    while(24<cents)
    {
        quaters = cents/25;
    }
    return quaters;
}

int calculate_dimes(int cents)
{
    // TODO
    int dimes;
    while(9<cents)
    {
        dimes = cents/10;
    }
    return dimes;
}

int calculate_nickels(int cents)
{
    // TODO
    int nickels;
    while(4<cents)
    {
        nickels= cents/5;
    }
    return nickels;
}

int calculate_pennies(int cents)
{
    // TODO
    int pennies;
    while(0<cents)
    {
        pennies=cents/1;
    }
    return pennies;
}

C 函数 while-loop 返回值 CS50

评论

2赞 chux - Reinstate Monica 5/18/2023
你认为为什么应该停止迭代? 永远不会改变。while(24<cents) { quaters = cents/25; }cents
0赞 user253751 5/18/2023
你认为它为什么应该回来?你告诉它,如果 24< 美分,它应该永远循环,所以它永远不会得到返回指令

答:

1赞 Vlad from Moscow 5/18/2023 #1

至少在函数中,你有一个 inifinte while 循环,当大于calculate_quarterscents24

int calculate_quarters(int cents)
{
    // TODO
    int quaters;
    while(24<cents)
    {
        quaters = cents/25;
    }
    return quaters;
}

因为变量没有被更改。看来您需要简单地定义函数cents

int calculate_quarters(int cents)
{
    return cents / 25;
}

其他函数(如 、 和 )也存在类似的问题。calculate_dimescalculate_nickelscalculate_pennies

而这个电话

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies( cents );

只是多余的。

此外,请尝试使用命名常量而不是幻数,例如 .25

请注意,与其对变量使用有符号类型,不如使用无符号整数类型来防止使用负值。intunsigned int

如果使用该类型,那么您的程序看起来更简单,而无需通过标头中声明的标准 C 函数来定义函数intdiv<stdlib.h>

例如

#include <stdlib.h>

//...


// Calculate the number of quarters to give the customer 
div_t result = div( cents, 25 );
int quarters = result.quot;
cents = result.rem;

// Calculate the number of dimes to give the customer
result = div( cents, 10 );
int dimes = result.quot;
cents = result.rem;

// and so on

在这种情况下,您的程序可能如下所示

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

int get_cents( void )
{
    int cents;

    do
    {
        cents = get_int( "Change Owed: " );
    } while (cents < 0);

    return cents;
}

int main( void )
{
    const int CENTS_PER_QUATER = 25;
    const int CENTS_PER_DIME   = 10;
    const int CENTS_PER_NICKEL = 5;

    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    div_t result = div( cents, CENTS_PER_QUATER );
    int quarters = result.quot;
    cents = result.rem;

    // Calculate the number of dimes to give the customer
    result = div( cents, CENTS_PER_DIME );
    int dimes = result.quot;
    cents = result.rem;

    // Calculate the number of nickels to give the customer
    result = div( cents, CENTS_PER_NICKEL );
    int nickels = result.quot;
    cents = result.rem;

    // Calculate the number of pennies to give the customer
    int pennies = cents;

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf( "%d\n", coins );
}

评论

0赞 Depps 5/18/2023
非常感谢你,出于某种原因,我只是无法看穿问题,我一直在绕过它。它完美地工作<3
0赞 Vlad from Moscow 5/18/2023
@Depps 完全没有。不客气:)
0赞 Antero 5/18/2023 #2

计算函数中的逻辑没有正确划分硬币。您设置的 while 循环将无限继续,因为美分的值在循环中永远不会改变。相反,您可以简单地使用整数除法来计算硬币的数量并返回该值。除法的其余部分(给硬币后剩下的零钱)将在主函数中处理,因为您已经设置好了。

以下是更正后的代码:

#include <cs50.h>
#include <stdio.h>

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);

int main(void)
{
    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    int quarters = calculate_quarters(cents);
    cents = cents - quarters * 25;

    // Calculate the number of dimes to give the customer
    int dimes = calculate_dimes(cents);
    cents = cents - dimes * 10;

    // Calculate the number of nickels to give the customer
    int nickels = calculate_nickels(cents);
    cents = cents - nickels * 5;

    // Calculate the number of pennies to give the customer
    int pennies = calculate_pennies(cents);

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf("%i\n", coins);
}

int get_cents(void)
{
    int cents;
    do{
        cents = get_int("Change Owed: ");
    }
    while (cents<0);
    return cents;
}

int calculate_quarters(int cents)
{
    return cents / 25;
}

int calculate_dimes(int cents)
{
    return cents / 10;
}

int calculate_nickels(int cents)
{
    return cents / 5;
}

int calculate_pennies(int cents)
{
    return cents / 1;
}

该程序将正确计算作为零钱提供的最小硬币数量。它首先计算最大四分之一数,然后计算剩余零钱的最大一角硬币数,然后是镍币,最后是便士。