无法理解 C 中的字数统计

标签 c if-statement boolean state getchar

<分区>

[已解决] 我最近问了一个关于一些我认为我真正理解的代码的问题。

但几天后,当我回头修改时,同一段代码出现了新问题(摘自 THE C PROGRAMMING LANGUAGE Second Edition by Brian. W. Kernighnan (ISBN-13: 978-8131704943))。

代码如下:

#include <stdio.h>

#define IN  1
#define OUT 0

main()
{
    int nw, c, state;
    state = OUT;
    nw = 0;
    while ((c = getchar()) != EOF)
    {
        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if (state == OUT)
        {
            state = IN;
            ++nw;
        }
    }
    printf("%d", nw);
}

所以我做了一些随机测试,发现了一些我无法解释或理解的东西:

  1. 为什么我有代码却无法运行

    state = OUTnw = 0

  2. 之后

当我正常运行这段代码时,它给出了正确的编号。单词,但是当我交换 state = OUT 和 nw = 0 的顺序时,它总是返回等于 0 的答案,为什么会这样? 我知道顺序在 C 中很重要,但为什么只有这个特定的顺序??

2.if-else语句简述是什么意思??

3.累积。到书状态变种。被定义为定义 getchar 是否在一个单词内,但我不明白状态变量究竟是如何做的。这样做??

4.另外,作者对等价和相等的使用是什么? 我注意到他在提出条件时使用等价,否则使用等价,我说得对吗??

谢谢你...

最佳答案

这是您发布的带有一些评论的代码:

#include <stdio.h>
#define IN  1 // in the code below, whenever you see IN, it's replaced by 1
#define OUT 0 // same as IN, but for 0

int main(void) {
  int nw, c, state; // nw is the number of the words
  // c stands for the character we get from input and state is the state that are
  // currently.

  // init state as OUT
  state = OUT;
  // init counter to 0
  nw = 0;

  // while user doesn't give EOF
  while ((c = getchar()) != EOF) {

    // we found whitespace, newline or tab
    if (c == ' ' || c == '\n' || c == '\t')
      state = OUT;                            // put state as OUT,
    // so that we do not count them as words
    else if (state == OUT) {                  // enter here only if state is OUT
      // Now, we see that we found a letter/number, which means that a word
      // was typed by the user.

      // set state as IN, so that we remember that we are eating the characters of
      // the word given (in the next loops)
      state = IN;

      // increase the counter of the words
      ++nw;
    }
  }

  // print the number of words received by the user
  printf("%d", nw);
  return 0;
}

现在,让我们看一下示例输入:

sam
dad

当然输出(正如你应该从上面的评论中猜到的那样)是:

2

让我们针对输入 sam 逐步运行(有趣的)代码:

// input: "sam" (without the quotes)

while ((c = getchar()) != EOF) { // eat first character, i.e. 's'

  /****** 1st execution of the loop ********************/

  // 's' is not going into this if
  if (c == ' ' || c == '\n' || c == '\t')
    state = OUT;
  // first time we execute the loop so state is OUT,
  // thus we enter the loop
  else if (state == OUT) {

    // set state as IN
    state = IN;

    // increase the counter of the words
    ++nw;
  }

  /****************************************************/

  /****** 2nd execution of the loop ********************/
  // while loop's getchar() gives as 'a'

  // 'a' is not going into this if
  if (c == ' ' || c == '\n' || c == '\t')
    state = OUT;
  // state is IN, so we don't go into this loop
  else if (state == OUT) {
    state = IN;
    ++nw;
  }

  /****************************************************/


  /****** 3rd execution of the loop ********************/
  // same as 2nd, but for 'm'

  /****************************************************/


  /****** 4rth execution of the loop ********************/
  // while loop's getchar() gives as '\n'

  // '\n' is going into this if (the c == '\n' is true
  if (c == ' ' || c == '\n' || c == '\t')
    state = OUT;                // state sets to OUT

  // we are not going into this if else, since we already entered the above if!
  else if (state == OUT) {
    state = IN;
    ++nw;
  }

  /****************************************************/


  /****** 5th execution of the loop ********************/
  // while loop's getchar() gives as 'd' (first letter of dad)

  // 'd' is not going into this if
  if (c == ' ' || c == '\n' || c == '\t')
    state = OUT;
  // state is OUT, so we go into this loop
  else if (state == OUT) {
    // set state as IN
    state = IN;

    // increase counter
    ++nw;
  }

  /****************************************************/

  // and so on :)
}

首先确保您理解这一点,然后回答您自己提出的问题。 :)

另外,我们称之为代码,而不是脚本。不要急于进入脚本。

关于无法理解 C 中的字数统计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770847/

相关文章:

c - 写入失败,errno 0

Ruby if 语法(两个表达式)

python - pyspark boolean 配置与大写有关的问题

c++ - 如何同时检查多个索引的 `std::vector<bool>` 是否为真?

java - 解决java中没有无符号变量的问题

c - 从文件中获取一个字符后的整数

在 C 中转换转义序列

核心转储错误 c

java - Android:如何获取翻转器内布局的ID?

java - Java扫描程序字符串比较