|
发表于 2008-8-5 01:45:58
|
显示全部楼层
Post by usertesting;1882563
貌似while循环内没加c=getchar();
同意楼上的。
我无聊中,就重写了你的小程序。
- #include <stdio.h>
- #include <ctype.h>
- typedef int bool;
- const bool false = 0;
- const bool true = 1;
- int main(int argc, char **argv)
- {
- int c; // 这个最好定义为 int 而不是 char
- int alpha_cnt, word_cnt, digit_cnt, space_cnt, other_cnt, total_cnt;
- alpha_cnt = word_cnt = digit_cnt = space_cnt = other_cnt = total_cnt = 0;
- bool is_word = false;
- while (true) {
- c = getchar();
- if (c == '\n' || c == EOF) {
- if (is_word) ++word_cnt;
- break;
- }
- if (isalpha(c)) {
- ++alpha_cnt;
- if (!is_word) {
- is_word = true;
- }
- } else if (isspace(c)) {
- ++space_cnt;
- } else if (isdigit(c)) {
- ++digit_cnt;
- } else {
- ++other_cnt;
- }
- if (is_word && !isalpha(c)) {
- is_word = false;
- ++word_cnt;
- }
- ++total_cnt;
- }
- printf ("统计结果:字母%d个;单词%d个;数字%d个;空格%d个;其它%d个\n",
- alpha_cnt, word_cnt, digit_cnt, space_cnt, other_cnt);
- return 0;
- }
复制代码 |
|