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

huangapple go评论59阅读模式
英文:

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

问题

以下是您要翻译的代码部分:

我目前正在进行CS50第1周的现金作业,一切都很顺利,直到我处理了零钱部分。任务是从用户获取他们欠多少零钱的输入,然后创建函数来找到返回的最小零钱(例如,欠60美分,然后显示3,表示225美分和110美分)。
顶部部分是已经提供的部分:

现在我实现了while函数,因为我知道我只想在特定条件下执行它,但我无法让它返回特定的函数到主函数。这是我做的一切:

我已经将代码部分翻译成中文,如您所需。

英文:

I'm currently working on the Cash assignment for Week 1 in CS50, and everything was running smoothly until I moved past quarters. The objective is to get the input form the user on how much change they owe, then create functions that find the least minimum change possible to give back (i.e. 60 cents is owed, then it would show 3 to represent 2 quarters and 1 dime).
The top part is what was already provided:

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

Now I implemented the while function because I know I want it to do it only under a certain condition, but I can't get it to return the specific function back to the main.
This is everything I've done:

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

答案1

得分: 1

以下是您提供的代码的翻译部分:

在函数 calculate_quarters 中,当 cents 大于 24 时,存在一个无限循环:

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 这样的魔术数字。

请注意,为了防止使用负值,最好使用无符号整数类型 unsigned int 而不是带符号类型 int 作为变量类型。

如果使用类型 int,则您的程序可以更简单地使用位于头文件 <stdlib.h> 中声明的标准 C 函数 div 来定义函数,例如:

#include &lt;stdlib.h&gt;

//...

// 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;cs50.h&gt;    

int get_cents( void )
{
	int cents;

	do
	{
		cents = get_int( &quot;Change Owed: &quot; );
	} while (cents &lt; 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( &quot;%d\n&quot;, coins );
}
英文:

At least in the function calculate_quarters you have an inifinte while loop when cents are greater than 24

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

because the variable cents is not being changed. It seems you need to define the function simply like

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

Similar problems exist for other functions like calculate_dimes, calculate_nickels, and calculate_pennies.

And this call

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

is just redundant.

Also try to use named constants instead of magic numbers like for example 25.

Pay attention to that instead of using the signed type int for your variables it is much better to use unsigned integer type unsigned int to prevent using negative values.

If to use the type int then your program can look simpler without defining the functions by means of the standard C function div declared in header &lt;stdlib.h&gt;

For example

#include &lt;stdlib.h&gt;

//...


// 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

In this case your program can look like

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;cs50.h&gt;    

int get_cents( void )
{
	int cents;

	do
	{
		cents = get_int( &quot;Change Owed: &quot; );
	} while (cents &lt; 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( &quot;%d\n&quot;, coins );
}

答案2

得分: 0

以下是您要翻译的内容:

"你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。以下是要翻译的内容:

The logic you have in your calculate functions are not correctly dividing the coins. The while loop you have set up will continue infinitely because the value of cents is never changing within the loop. Instead, you can simply use integer division to calculate the number of coins and return that value. The remainder of the division (the change left after giving the coins) will be handled in the main function as you have it set up already.

Here's the corrected code:

#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(&quot;%i\n&quot;, 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;
}

This program will correctly calculate the least number of coins to give as change. It first calculates the maximum number of quarters, then the maximum number of dimes from the remaining change, then nickels, and finally pennies."

希望这对您有帮助。

英文:

The logic you have in your calculate functions are not correctly dividing the coins. The while loop you have set up will continue infinitely because the value of cents is never changing within the loop. Instead, you can simply use integer division to calculate the number of coins and return that value. The remainder of the division (the change left after giving the coins) will be handled in the main function as you have it set up already.

Here's the corrected code:

#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;

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(&quot;%i\n&quot;, coins);
}

int get_cents(void)
{
    int cents;
    do{
        cents = get_int(&quot;Change Owed: &quot;);
    }
    while (cents&lt;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;
}

This program will correctly calculate the least number of coins to give as change. It first calculates the maximum number of quarters, then the maximum number of dimes from the remaining change, then nickels, and finally pennies.

huangapple
  • 本文由 发表于 2023年5月18日 02:21:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定