代码没有输出,但它是 运行。 [C]
The code isn't outputting, but it's running. [c]
代码功能:俄罗斯农民乘法计算器
代码:
#include <stdio.h>
#include <math.h>
main(){
int ier, product = 0, check, i = 1; //var to store numbers
float cand; //Var to store decimal numbers
while(i != 0){
printf("\nInput only whole numbers!"); //Ask user for number to be multiplied
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
while(i != 0){
if((ier == 0 && cand > 0) || (ier > 0 && cand == 0) || (ier == 0 && cand < 0) || (ier < 0 && cand == 0)){//Regulations for the code
printf("Product: 0");
printf("\n\nInput only whole numbers!");
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
}
if((ier > 0 && cand > 0) || (ier < 0 && cand > 0) || (ier > 0 && cand < 0) || (ier < 0 && cand < 0)){
break;
}
if(ier == 0 && cand == 0){//If user inputs 0 and 0, end code
return 0;
}
}
while(i != 0){
if((ier < 0 && cand > 0) || (ier > 0 && cand < 0) || (ier < 0 && cand < 0)){//Regulations for the code
printf("Values must not be negative");
printf("\n\nInput only whole numbers!");
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
}
if((ier > 0 && cand > 0) || (ier == 0 && cand > 0) || (ier > 0 && cand == 0) || (ier == 0 && cand < 0) || (ier < 0 && cand == 0)){
break;
}
if(ier == 0 && cand == 0){//If user inputs 0 and 0, end code
return 0;
}
}
if(ier > 0 && cand > 0){
printf("Calculating Product:");//It should print this, but it isn't for some reason.
while(i != 0){
if(fmod(cand, 2) != 0){
if(fmod(cand, 1) != 0){
product = product + floor(cand);
cand = floor(cand);
printf("%d \t%f", ier, cand);
}
else{
product = product + cand;
printf("%d \t%f", ier, cand);
}
}
if(cand == 1){
printf("Product: %d", product);
break;
}
ier *= 2;
cand /= 2;
}
}
}
}
问题: 当我输入 运行 代码并输入例如 5 和 2 时,它会继续 运行ning 而不会输出任何内容。我认为代码中的问题是 fmod()
。我只使用 fmod
因为你不能在浮点变量上使用模数运算符 %
。
我所做的事情: 我将 fmod 更改为模数运算符 %
并将 cand 变为整数。那行得通,但是现在我对小数点有疑问,因为整数四舍五入。所以我回到了fmod
。
输入:
gcc version 4.6.3
Input only whole numbers!
Multiplier: 5
Multiplicand: 2
/*Runtime: infinite*/
我想要的输出
Multiplier: 57
Multiplicand: 86
Calculating product:
114 43
228 21
912 5
3648 1
Product: 4902
Multiplier: 48
Multiplicand: -36
Values must not be negative
Multiplier: 27
Multiplicand: 0
Product: 0
Multiplier: 0
Multiplicand: 0
P.S: 这是我第一次使用 fmod
和 floor()
.
您的代码进入无限循环的原因不是因为 fmod()
函数,而是因为您已将 cand
初始化为 float
值,而不是作为int
值。将 cand
更改为 int
值允许代码继续工作以打印出结果:
所以,改变:
float cand;
收件人:
int cand;
这至少可以防止代码挂起,而且,就像@WeatherVane 指出的那样,它还修复了编译器应该为以下行生成的警告:
scanf("%d", &cand); // "cand" is a float, but your format specifier says that it is an int.
阅读您的代码后,我发现您错误地使用了scanf
函数。在第一个 while
循环之后的代码中,您执行了以下操作:
while(i != 0){
printf("\nInput only whole numbers!"); //Ask user for number to be multiplied
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
如果你看看这句话上面的scanf
,你会发现你做到了(正如@MartinR 指出的那样):
scanf("%d", &cand);
您需要将代码更改为以下内容:
scanf("%f", &cand);
这将修复您的无限运行时间。另外,@ABusyProgrammer 可能先写了响应(这是正确的),he/she 没有正确读取 post。 您 正在使用需要浮动的 fmod
。因此,你需要使用我上面写的scanf
。 @ABusyProgrammer 只有在您使用 %
运算符时才是正确的。
代码功能:俄罗斯农民乘法计算器
代码:
#include <stdio.h>
#include <math.h>
main(){
int ier, product = 0, check, i = 1; //var to store numbers
float cand; //Var to store decimal numbers
while(i != 0){
printf("\nInput only whole numbers!"); //Ask user for number to be multiplied
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
while(i != 0){
if((ier == 0 && cand > 0) || (ier > 0 && cand == 0) || (ier == 0 && cand < 0) || (ier < 0 && cand == 0)){//Regulations for the code
printf("Product: 0");
printf("\n\nInput only whole numbers!");
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
}
if((ier > 0 && cand > 0) || (ier < 0 && cand > 0) || (ier > 0 && cand < 0) || (ier < 0 && cand < 0)){
break;
}
if(ier == 0 && cand == 0){//If user inputs 0 and 0, end code
return 0;
}
}
while(i != 0){
if((ier < 0 && cand > 0) || (ier > 0 && cand < 0) || (ier < 0 && cand < 0)){//Regulations for the code
printf("Values must not be negative");
printf("\n\nInput only whole numbers!");
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
}
if((ier > 0 && cand > 0) || (ier == 0 && cand > 0) || (ier > 0 && cand == 0) || (ier == 0 && cand < 0) || (ier < 0 && cand == 0)){
break;
}
if(ier == 0 && cand == 0){//If user inputs 0 and 0, end code
return 0;
}
}
if(ier > 0 && cand > 0){
printf("Calculating Product:");//It should print this, but it isn't for some reason.
while(i != 0){
if(fmod(cand, 2) != 0){
if(fmod(cand, 1) != 0){
product = product + floor(cand);
cand = floor(cand);
printf("%d \t%f", ier, cand);
}
else{
product = product + cand;
printf("%d \t%f", ier, cand);
}
}
if(cand == 1){
printf("Product: %d", product);
break;
}
ier *= 2;
cand /= 2;
}
}
}
}
问题: 当我输入 运行 代码并输入例如 5 和 2 时,它会继续 运行ning 而不会输出任何内容。我认为代码中的问题是 fmod()
。我只使用 fmod
因为你不能在浮点变量上使用模数运算符 %
。
我所做的事情: 我将 fmod 更改为模数运算符 %
并将 cand 变为整数。那行得通,但是现在我对小数点有疑问,因为整数四舍五入。所以我回到了fmod
。
输入:
gcc version 4.6.3
Input only whole numbers!
Multiplier: 5
Multiplicand: 2
/*Runtime: infinite*/
我想要的输出
Multiplier: 57
Multiplicand: 86
Calculating product:
114 43
228 21
912 5
3648 1
Product: 4902
Multiplier: 48
Multiplicand: -36
Values must not be negative
Multiplier: 27
Multiplicand: 0
Product: 0
Multiplier: 0
Multiplicand: 0
P.S: 这是我第一次使用 fmod
和 floor()
.
您的代码进入无限循环的原因不是因为 fmod()
函数,而是因为您已将 cand
初始化为 float
值,而不是作为int
值。将 cand
更改为 int
值允许代码继续工作以打印出结果:
所以,改变:
float cand;
收件人:
int cand;
这至少可以防止代码挂起,而且,就像@WeatherVane 指出的那样,它还修复了编译器应该为以下行生成的警告:
scanf("%d", &cand); // "cand" is a float, but your format specifier says that it is an int.
阅读您的代码后,我发现您错误地使用了scanf
函数。在第一个 while
循环之后的代码中,您执行了以下操作:
while(i != 0){
printf("\nInput only whole numbers!"); //Ask user for number to be multiplied
printf("\nMultiplier: ");
scanf("%d", &ier);
printf("Multiplicand: ");
scanf("%d", &cand);
如果你看看这句话上面的scanf
,你会发现你做到了(正如@MartinR 指出的那样):
scanf("%d", &cand);
您需要将代码更改为以下内容:
scanf("%f", &cand);
这将修复您的无限运行时间。另外,@ABusyProgrammer 可能先写了响应(这是正确的),he/she 没有正确读取 post。 您 正在使用需要浮动的 fmod
。因此,你需要使用我上面写的scanf
。 @ABusyProgrammer 只有在您使用 %
运算符时才是正确的。