代碼功能:這是一個俄羅斯農民增殖計算器。
代碼:代碼不輸出,但它正在運行。 [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;
}
}
}
}
問題:當我運行代碼,我輸入e.g 5和圖2,不斷沒有任何輸出運行。我認爲代碼中的問題是fmod()
。我只使用fmod
,因爲你不能在float變量上使用模數運算符%
。
事情我已經做了:我把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()
我的第一次。
評論不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/138405/discussion-on-question-by-manav-dubey-streetsville-ss-the-code-isnt-outputti)。 –