K&R在C中计算1.5.4字数

1 c if-statement word-count

我是一名崭露头角的C程序员,我编写了这个程序来计算第二版K&R 1.5.4的单词.我的if语句有问题吗?代码似乎在不应该增加变量时增加,因为它不符合初始测试.

#include <stdio.h>
#define IN 1
#define OUT 0


main()
{
      int word, state, c;

      word = state = OUT;

      while((c = getchar()) != EOF)
      {
               if(c != ' ' || c != '\n' || c != '\t')
               {
                    if(state == OUT)
                    {
                             word++;
                             state = IN;
                    }

                    /*else
                    {
                         state = IN;
                    }*/
               }

               if(c == ' ' || c == '\n' || c == '\t')
               {
                   state = OUT;
               }

               printf("char: %c %x | state: %d | word: %d\n", c, c, state, word); 
      }

      printf("\n//maniac: %d\n", word);
Run Code Online (Sandbox Code Playgroud)

这导致:

>count_word_my.exe
Hello  he   he
char: H 48 | state: 1 | word: 1
char: e 65 | state: 1 | word: 1
char: l 6c | state: 1 | word: 1
char: l 6c | state: 1 | word: 1
char: o 6f | state: 1 | word: 1
char:   20 | state: 0 | word: 1
char:   20 | state: 0 | word: 2
char: h 68 | state: 1 | word: 3
char: e 65 | state: 1 | word: 3
char:   20 | state: 0 | word: 3
char:   20 | state: 0 | word: 4
char:   20 | state: 0 | word: 5
char: h 68 | state: 1 | word: 6
char: e 65 | state: 1 | word: 6
char:
 a | state: 0 | word: 6

char:
 a | state: 0 | word: 7

//maniac: 7
Run Code Online (Sandbox Code Playgroud)

我修改过的K&R代码:

#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

/* count lines, words, and characters in input */
main()
{
      int c, nl, nw, nc, state;

      state = OUT;
      nl = nw = nc = 0;
      while ((c = getchar()) != EOF) {
            ++nc;
            if (c == '\n')
               ++nl;
            if (c == ' ' || c == '\n' || c == '\t')
               state = OUT;
            else if (state == OUT) {
                 state = IN;
                 ++nw;
            }
            printf("char: %c %x | state: %d | word: %d\n", c, c, state, nw);
      }
      printf("%d %d %d\n", nl, nw, nc);
}
Run Code Online (Sandbox Code Playgroud)

K&R代码导致:

>count_word_test.exe
Hello  he   he
char: H 48 | state: 1 | word: 1
char: e 65 | state: 1 | word: 1
char: l 6c | state: 1 | word: 1
char: l 6c | state: 1 | word: 1
char: o 6f | state: 1 | word: 1
char:   20 | state: 0 | word: 1
char:   20 | state: 0 | word: 1
char: h 68 | state: 1 | word: 2
char: e 65 | state: 1 | word: 2
char:   20 | state: 0 | word: 2
char:   20 | state: 0 | word: 2
char:   20 | state: 0 | word: 2
char: h 68 | state: 1 | word: 3
char: e 65 | state: 1 | word: 3
char:
 a | state: 0 | word: 3

char:
 a | state: 0 | word: 3
2 3 16
^C
Run Code Online (Sandbox Code Playgroud)

当我的代码在第一个if语句中不满足测试时,在'Hello'之后处理第二个空格(0x20)时,我的代码如何递增word/nw?即使它确实达到了第二个if语句,我也认为它会将'state'变量设置为1(IN).我错过了一些关键的东西.我非常感谢给予的任何帮助.谢谢.

MBy*_*ByD 7

好吧,每个人char都会认为这是真的,if(c != ' ' || c != '\n' || c != '\t')因为没有炭都是' ','\n'而且'\t'.

应该是:

if(c != ' ' && c != '\n' && c != '\t')
Run Code Online (Sandbox Code Playgroud)