#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 ;
}
當我編譯它使用
GCC
,只按Ctrl + z
它打印位數= 00000,製表符= 4200912,新行= 4194432,空格= 2293540 其他= 2147307520
來自何處,這些號碼是多少?
我重新編譯它,然後輸入
HELLO HELLO HELLO
並點擊進入 和它打印wtnwtnwnn
- 這是爲什麼(有3 N大於預期,爲什麼它計算三個選項卡)?否則
編譯啓用了警告:'GCC -Wall代碼。c' –
您將'ndigits [i]'初始化爲零。那麼'white','tab'等等呢? – dasblinkenlight
是的,我忘記初始化其他變量:) 但 我再次編譯並輸入HELLO HELLO HELLO,然後單擊enter並打印wtnwtnwnn 爲什麼(有3 n比預期,爲什麼它計算三個選項卡)? –