2017-03-17 45 views
-4

代碼功能:這是一個俄羅斯農民增殖計算器。
代碼:代碼不輸出,但它正在運行。 [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:這是使用fmodfloor()我的第一次。

+0

評論不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/138405/discussion-on-question-by-manav-dubey-streetsville-ss-the-code-isnt-outputti)。 –

回答

1

在閱讀您的代碼後,我發現您錯誤地使用了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可能會先寫回應(這是正確的),他/她沒有正確閱讀帖子。 正在使用需要浮動的fmod。因此,您需要使用我上面編寫的scanf。@ABusyProgrammer只有在使用%運算符時纔是正確的。

+0

另外,查看你的代碼,這將是一個好習慣。 –

1

你的代碼進入一個無限循環的原因是不是fmod()功能,因爲,而是因爲你已經初始化cand作爲float值,而不是作爲一個int值。更改candint值允許代碼繼續工作,打印出結果:

所以,變化:

float cand; 

要:

int cand; 

這至少可以防止代碼掛,就像@WeatherVane指出的那樣,它還修復了編譯器應爲以下行生成的警告:

scanf("%d", &cand); // "cand" is a float, but your format specifier says that it is an int. 
+0

這也將處理約'的scanf(「%d」的編譯器警告,&cand);''那裏是cand'一個'float'。 –

+0

@WeatherVane真的,我沒有注意到這一點。發現它是'float'這是導致該坑是夠硬。我會更新我的職務。謝謝你這一點! –

+0

@ABusyProgrammer,讀取正確後,用戶使用'fmod',還有它需要一個浮動。 –

相關問題