由于舍入,c 中的无限循环?
Infinite loop in c due to rounding?
以下代码无限循环。我相信这可能是一个四舍五入的问题,但并不完全确定。
我对 C 还很陌生,所以不确定为什么会出现无限循环。尽管不断循环,但代码似乎很有意义。
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
int main(void){
float quarter = .25;
int Qtr = 0;
float dime = .10;
int Dm = 0;
float nickel = .5;
int Nck = 0;
float penney = .01;
int Pn = 0;
float change;
float userInput;
float newspaperCost;
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
}
while(newspaperCost <= 0);
while(change > 0){
printf("%f\n", change);
while(change - quarter > 0){
change = change - quarter;
Qtr++;
}
while(change - dime > 0){
change = change - dime;
Dm++;
}
while(change - nickel > 0){
change = change - nickel;
Nck++;
}
while(change - penney > 0){
change = change - penney;
Pn++;
}
}
printf("Your change consists of %d quarters, %d dimes, %d nickels, and %d pennies\n", Qtr, Dm, Nck, Pn);
} //end main
- 不要使用浮点数来表示货币。
- 除非你有充分的理由,否则更喜欢双倍浮动。
- 便士*
- 您没有对浮点操作数使用任何相等运算符,因此您可能有逻辑错误。
您的代码:
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
} while(newspaperCost <= 0);
不会在 newspaperCost <= 0
时退出此循环。我假设您没有在
输入负数
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
这就是为什么你有一个无限循环。
你在最后一个 while
循环中有一个逻辑错误。
而不是
while(change - penney > 0){
change = change - penney;
Pn++;
}
使用
while(change > 0){
change = change - penney;
Pn++;
}
以下代码无限循环。我相信这可能是一个四舍五入的问题,但并不完全确定。
我对 C 还很陌生,所以不确定为什么会出现无限循环。尽管不断循环,但代码似乎很有意义。
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
int main(void){
float quarter = .25;
int Qtr = 0;
float dime = .10;
int Dm = 0;
float nickel = .5;
int Nck = 0;
float penney = .01;
int Pn = 0;
float change;
float userInput;
float newspaperCost;
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
}
while(newspaperCost <= 0);
while(change > 0){
printf("%f\n", change);
while(change - quarter > 0){
change = change - quarter;
Qtr++;
}
while(change - dime > 0){
change = change - dime;
Dm++;
}
while(change - nickel > 0){
change = change - nickel;
Nck++;
}
while(change - penney > 0){
change = change - penney;
Pn++;
}
}
printf("Your change consists of %d quarters, %d dimes, %d nickels, and %d pennies\n", Qtr, Dm, Nck, Pn);
} //end main
- 不要使用浮点数来表示货币。
- 除非你有充分的理由,否则更喜欢双倍浮动。
- 便士*
- 您没有对浮点操作数使用任何相等运算符,因此您可能有逻辑错误。
您的代码:
do{
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
printf("Amount Tendered: ");
userInput = GetFloat();
printf("You entered $%2.2f\n", userInput);
change = userInput - newspaperCost;
printf("Change: $%2.2f\n", change);
} while(newspaperCost <= 0);
不会在 newspaperCost <= 0
时退出此循环。我假设您没有在
printf("How much is the news paper today: \n");
newspaperCost = GetFloat();
这就是为什么你有一个无限循环。
你在最后一个 while
循环中有一个逻辑错误。
而不是
while(change - penney > 0){
change = change - penney;
Pn++;
}
使用
while(change > 0){
change = change - penney;
Pn++;
}