[英]This is Cs50 problem set1 cash. I don't know what's wrong with this code can someone help me
This is Cs50 problem set1 cash.这是 Cs50 问题 set1 现金。 I don't know what's wrong with this code can someone help me.
我不知道这段代码有什么问题有人可以帮助我。 I just added the quotes because stackoverflow was not letting me post it.
我只是添加了引号,因为 stackoverflow 不允许我发布它。 Your program should behave per the examples below.
您的程序应该按照下面的示例运行。
$ ./cash
Change owed: 0.41
4
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
int i = 0;
int a = 0;
int b = 0;
int c = 0;
float change_owed;
do
{
change_owed = get_float("change owed: ");
}
while (change_owed <=0);
change_owed=round(change_owed*100);
printf("%f\n", change_owed);
if (change_owed>25)
;
{
while (change_owed>=25);
;
{
change_owed=change_owed-25;
i++;
}
}
if (change_owed>=10)
;
{
while (change_owed>=10);
;
{
change_owed=change_owed-10;
a++;
}
}
if (change_owed>=5)
;
{
while (change_owed>=5;c++;)
;
{
change_owed=change_owed-5;
}
}
if (change_owed>=1)
;
{
while(change_owed>=1)
;
{
change_owed=change_owed-1;
b++;
}
}
int final = i+a+b+c;
printf("%d + %d + %d+ %d = %d", i, a, b, c, final);
printf("\n");"
}
}
This is Cs50 problem set1 cash.这是 Cs50 问题 set1 现金。 I don't know what's wrong with this code can someone help me
我不知道这段代码有什么问题有人可以帮助我
The program does not crash , it loops for ever in while (change_owed>=25);
程序不会崩溃,它会在
while (change_owed>=25);
永远循环while (change_owed>=25);
There is a fundamental misunderstanding regarding the syntax of basic and control statements.关于基本语句和控制语句的语法存在根本性的误解。
;
ends a statement, including the empty statement.结束一个语句,包括空语句。 So
while (change_owed>=25);
所以
while (change_owed>=25);
is parsed as被解析为
while (change_owed >= 25)
/* empty statement */;
Since change_owned
does not change in the loop body, the loop runs for ever is change_owed
was >=
25 at the beginning.由于
change_owned
在循环体中没有改变,循环永远运行是change_owed
在开始时>=
25。
Similarly相似地
if (change_owed>=1)
;
Is parsed as被解析为
if (change_owed >= 1)
/* do nothing */;
which has no effect.这没有效果。
Here is modified version:这是修改后的版本:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
int i = 0;
int a = 0;
int b = 0;
int c = 0;
float change_owed;
do {
change_owed = get_float("change owed: ");
} while (change_owed <= 0);
change_owed = round(change_owed * 100);
printf("%f\n", change_owed);
if (change_owed > 25) {
while (change_owed >= 25) {
change_owed = change_owed - 25;
i++;
}
}
if (change_owed >= 10) {
while (change_owed >= 10) {
change_owed = change_owed - 10;
a++;
}
}
if (change_owed >= 5) {
while (change_owed >= 5) {
change_owed = change_owed - 5;
b++;
}
}
if (change_owed >= 1) {
while (change_owed >= 1) {
change_owed = change_owed - 1;
c++;
}
}
int final = i + a + b + c;
printf("%d + %d + %d+ %d = %d", i, a, b, c, final);
printf("\n");
return 0;
}
Note that the if
statements are redundant, as well as the last loop, and the code would be more readable with more explicit variable names:请注意,
if
语句以及最后一个循环都是多余的,并且使用更明确的变量名称会使代码更具可读性:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
float change_owed;
do {
change_owed = get_float("change owed: ");
} while (change_owed <= 0);
int cents = round(change_owed * 100);
printf("%d\n", cents);
while (cents >= 25) {
cents -= 25;
quaters++;
}
while (cents >= 10) {
cents -= 10;
dimes++;
}
while (cents >= 5) {
cents -= 5;
nickels++;
}
pennies = cents;
int coins = quarters + dimes + nickels + pennies;
printf("%d + %d + %d + %d = %d\n", quarters, dimes, nickels, pennies, coins);
return 0;
}
Finally, instead of loops, you can use integer division:最后,您可以使用整数除法代替循环:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
float change_owed;
do {
change_owed = get_float("change owed: ");
} while (change_owed <= 0);
int cents = round(change_owed * 100);
printf("%d\n", cents);
int quarters = cents / 25;
cents -= quarters * 25; // or cents %= 25;
int dimes = cents / 10;
cents -= dimes * 10; // or cents %= 10;
int nickels = cents / 5;
int pennies = cents % 5;
int total = quarters + dimes + nickels + pennies;
printf("%d + %d + %d + %d = %d\n", quarters, dimes, nickels, pennies, total);
return 0;
}
Or even simpler:或者更简单:
Finally, instead of loops, you can use integer division:最后,您可以使用整数除法代替循环:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void) {
float change_owed;
do {
change_owed = get_float("change owed: ");
} while (change_owed <= 0);
int cents = round(change_owed * 100);
printf("%d\n", cents);
int quarters = cents / 25;
int dimes = cents % 25 / 10;
int nickels = cents % 25 % 10 / 5;
int pennies = cents % 5;
int total = quarters + dimes + nickels + pennies;
printf("%d + %d + %d + %d = %d\n", quarters, dimes, nickels, pennies, total);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.