2017-04-22 71 views
-1

C代碼具有計數空格和製表程序奇怪的行爲

#include <stdio.h> 

int main() 
{ 
    int c , nother , new , ndigits [10] , white, tabs ; 

    for (int i = 0 ; i< 10 ; i++) 
     ndigits[i] = 0 ; 

    while ((c = getchar())!= EOF) 
    { 
     switch (c) 
     { 
      case '0' : case '1' : case '2' : 
      case '3' : case '4' : case '5' : 
      case '6' : case '7' : case '8' : 
      case '9' : 
       ndigits[c- '0' ]++ ; 
       break ; 

      case ' ' : 
       printf("w"); /*to see how many spaces */ 
       white++ ; 

      case '\t' : 
       printf("t"); 
       tabs++; 

      case '\n' : 
       printf("n"); 
       new++ ; 
       break ; 

      default : 
       nother++ ; 
       break ;  
     } 
    } 

    printf ("digits = ") ; 

    for (int i = 0 ; i < 10 ; i++) 
     printf ("%d" , ndigits[i]) ; 

    printf (",tabs = %d , new line = %d, spaces = %d , other = %d ", 
     tabs, white , new , nother); 

    return 0 ; 
} 
  1. 當我編譯它使用GCC,只按Ctrl + z它打印

    位數= 00000,製表符= 4200912,新行= 4194432,空格= 2293540 其他= 2147307520

    來自何處,這些號碼是多少?

  2. 我重新編譯它,然後輸入HELLO HELLO HELLO並點擊進入 和它打印wtnwtnwnn

    • 這是爲什麼(有3 N大於預期,爲什麼它計算三個選項卡)?否則
+3

編譯啓用了警告:'GCC -Wall代碼。c' –

+2

您將'ndigits [i]'初始化爲零。那麼'white','tab'等等呢? – dasblinkenlight

+0

是的,我忘記初始化其他變量:) 但 我再次編譯並輸入HELLO HELLO HELLO,然後單擊enter並打印wtnwtnwnn 爲什麼(有3 n比預期,爲什麼它計算三個選項卡)? –

回答

2

你的一些case S的缺失break s,這似乎並不是你想要的。

此外,您還沒有初始化nother,new,whitetabs,但仍在使用它們。這導致未定義的行爲。 (任何像樣的編譯器會給你一個關於這個警告。)

+1

確實'case'':'會打印'wtn'。 –

+0

我編輯了上面給出的code.its,但它給出了輸出,如果預期 – Billa

+0

@Downvoter:這或其他任何答案如何「沒有用」? – emlai

2

初始化爲零的計數器的初始值將是不可預測的。

int c , nother = 0 , new = 0 , ndigits [10] , white = 0, tabs = 0 ; 

同樣,每個case塊(除了趕上位數的那些)必須用break;被終止,以實現預期的結果。

如果你忽略他們的下一個指令將被執行。

white++; 
break; 

...

tabs++; 
break; 

底部注:你將自己的編譯器只允許警告發現這些錯誤。做到這一點:您將節省大量時間,發現天真的錯誤。

1

有幾個錯誤:

  1. 你剛纔聲明的變量和不進行初始化them.Thus垃圾值插入到這些變量
  2. 由於您沒有爲上述語句指定中斷,因此它正在打印'n'錯誤。
  3. 您在打印無。的空間彷彿線數(檢查printf (",tabs = %d , new line = %d, spaces = %d , other = %d " ,tabs ,white , new, nother) ;

下面的代碼生成所需的輸出:

#include <stdio.h> 

    int main() 
     { 
    int c=0,nother=0,new=0,ndigits[10],white=0,tabs=0,i=0 ; 
    for (i = 0 ; i< 10 ; i++) 
     ndigits[i] = 0 ; 
    while ((c = getchar())!= EOF) 
    { 
     switch (c) 
     { 
      case '0' : 
      case '1' : 
      case '2' : 
      case '3' : 
      case '4' : 
      case '5' : 
      case '6' : 
      case '7' : 
      case '8' : 
      case '9' : 
         ndigits[c- '0' ]++ ; 
         break ; 
      case ' ' : 
         printf("w"); /*to see how many spaces */ 
         white++ ; 
      case '\t' : 
         printf("t"); 
         tabs++; 
         break; 
      case '\n' : 
          { 
         printf("n"); 
         new++ ; 
         break ; 
          } 
      default : 
         nother++ ; 
         break ;  
     } 
    } 
    printf ("digits = ") ; 
    for (i = 0 ; i < 10 ; i++) 
     printf ("%d" , ndigits[i]) ; 
    printf (",tabs = %d , new line = %d, spaces = %d , other = %d " ,tabs , new,white , nother) ; 
    return 0 ; 
     } 
+0

檢查它,它運作良好。 – Billa