| 发表于:2007-05-25 10:31:45 楼主 |
...let is write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline), and of all other characters.... #include <stdio.h> /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != eof) if (c > = '0 ' && c <= '9 ') ++ndigit[c- '0 ']; else if (c == ' ' ¦ ¦ c == '\n ' ¦ ¦ c == '\t ') ++nwhite; else ++nother; printf( "digits = "); for (i = 0; i < 10; ++i) printf( " %d ", ndigit[i]); printf( ", white space = %d, other = %d\n ", nwhite, nother); } 我的问题是 ++ndigit[c- '0 '];为什么不能替换为 ++ndigit[c-0];或 ++ndigit[c]; 我觉得c- '0 '的值是int,c也是int,所以c-0 = c = c- '0 ',可是这时错的,我不太明白,望达人解惑。 |
|
|
|
|