cs50x 2022 中的错误 cash.c
Bug in cs50x 2022 cash.c
我的代码完美运行,它在我的计算机上完美编译,但当我检查它时它不起作用,它给出了正确的结果,而且看起来优化得很好。当我使用 check50 命令时,它显示:
:) cash.c exists
:( cash.c compiles
code failed to compile
:| get_cents returns integer number of cents
can't check until a frown turns upside down
:| get_cents rejects negative input
can't check until a frown turns upside down
:| get_cents rejects a non-numeric input of "foo"
can't check until a frown turns upside down
:| calculate_quarters returns 2 when input is 50
can't check until a frown turns upside down
:| calculate_quarters returns 1 when input is 42
can't check until a frown turns upside down
:| calculate_dimes returns 1 when input is 10
can't check until a frown turns upside down
:| calculate_dimes returns 1 when input is 15
can't check until a frown turns upside down
:| calculate_dimes returns 7 when input is 73
can't check until a frown turns upside down
:| calculate_nickels returns 1 when input is 5
can't check until a frown turns upside down
:| calculate_nickels returns 5 when input is 28
can't check until a frown turns upside down
:| calculate_pennies returns 4 when input is 4
can't check until a frown turns upside down
:| input of 41 cents yields output of 4 coins
can't check until a frown turns upside down
:| input of 160 cents yields output of 7 coins
can't check until a frown turns upside down
代码如下:
#include <stdio.h>
#include <cs50.h>
int greedy_calculation(int greedyInput);
int main(void)
{
int changeOwed = 0;
do
{
changeOwed = get_int("Changed owed: ");
}
while(changeOwed < 1);
changeOwed = greedy_calculation(changeOwed);
printf("%i\n", changeOwed);
}
int greedy_calculation(int greedyInput)
{
int finalResult = 0;
do
{
while((greedyInput - 25) >= 0)
{
greedyInput -= 25;
finalResult++;
}
while((greedyInput - 10) >= 0)
{
greedyInput -= 10;
finalResult++;
}
while((greedyInput - 5) >= 0)
{
greedyInput -= 5;
finalResult++;
}
while((greedyInput - 1) >= 0)
{
greedyInput -= 1;
finalResult++;
}
}
while(greedyInput != 0);
return finalResult;
}
我很清楚我仍然需要添加评论,但我更困惑为什么它不起作用,非常感谢你的帮助:D
来自spec(强调已添加):
CS50x 2022’s version of Cash is quite different than CS50x 2021’s version. It will be in your best interest to do this problem from scratch, if you do not have credit for the work you did in 2021. Last year’s version will fail to compile when checked by check50 due to the fact that in this new version, you must implement functions which the testing suite will test independently, beyond just checking for the final answer (as last year’s versiondid).
check50 希望您从提供的 (2022) 发行版代码开始并遵循规范说明。
我的代码完美运行,它在我的计算机上完美编译,但当我检查它时它不起作用,它给出了正确的结果,而且看起来优化得很好。当我使用 check50 命令时,它显示:
:) cash.c exists
:( cash.c compiles
code failed to compile
:| get_cents returns integer number of cents
can't check until a frown turns upside down
:| get_cents rejects negative input
can't check until a frown turns upside down
:| get_cents rejects a non-numeric input of "foo"
can't check until a frown turns upside down
:| calculate_quarters returns 2 when input is 50
can't check until a frown turns upside down
:| calculate_quarters returns 1 when input is 42
can't check until a frown turns upside down
:| calculate_dimes returns 1 when input is 10
can't check until a frown turns upside down
:| calculate_dimes returns 1 when input is 15
can't check until a frown turns upside down
:| calculate_dimes returns 7 when input is 73
can't check until a frown turns upside down
:| calculate_nickels returns 1 when input is 5
can't check until a frown turns upside down
:| calculate_nickels returns 5 when input is 28
can't check until a frown turns upside down
:| calculate_pennies returns 4 when input is 4
can't check until a frown turns upside down
:| input of 41 cents yields output of 4 coins
can't check until a frown turns upside down
:| input of 160 cents yields output of 7 coins
can't check until a frown turns upside down
代码如下:
#include <stdio.h>
#include <cs50.h>
int greedy_calculation(int greedyInput);
int main(void)
{
int changeOwed = 0;
do
{
changeOwed = get_int("Changed owed: ");
}
while(changeOwed < 1);
changeOwed = greedy_calculation(changeOwed);
printf("%i\n", changeOwed);
}
int greedy_calculation(int greedyInput)
{
int finalResult = 0;
do
{
while((greedyInput - 25) >= 0)
{
greedyInput -= 25;
finalResult++;
}
while((greedyInput - 10) >= 0)
{
greedyInput -= 10;
finalResult++;
}
while((greedyInput - 5) >= 0)
{
greedyInput -= 5;
finalResult++;
}
while((greedyInput - 1) >= 0)
{
greedyInput -= 1;
finalResult++;
}
}
while(greedyInput != 0);
return finalResult;
}
我很清楚我仍然需要添加评论,但我更困惑为什么它不起作用,非常感谢你的帮助:D
来自spec(强调已添加):
CS50x 2022’s version of Cash is quite different than CS50x 2021’s version. It will be in your best interest to do this problem from scratch, if you do not have credit for the work you did in 2021. Last year’s version will fail to compile when checked by check50 due to the fact that in this new version, you must implement functions which the testing suite will test independently, beyond just checking for the final answer (as last year’s versiondid).
check50 希望您从提供的 (2022) 发行版代码开始并遵循规范说明。