C语言 K&R第1.5.4章:“你如何测试单词计数程序?”

z4iuyo4d  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(122)

新手在此。
在K&R的ANSI C教科书第20页中,他们问:你将如何测试字数统计程序?
我完全照搬了教科书上的内容,使用的是CodeBlocksIDE,控制台应用程序。我在网上看到过很多很棒的输入测试,但我的问题更愚蠢。我实际上如何输入一些东西?当我按Enter键时没有任何React。我有这个问题是因为我使用的是IDE,因此没有学习如何正确运行C程序吗?提前谢谢。I added a picture to show you what I mean
代码如下:

#include <stdio.h>

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

/* counts lines, words and characters as input */

main()
{
    int c, nl, nw, nc, state;

    state = OUT;
    /* set these three constants to 0: */
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF){
        ++nc;
        if (c == '\n')
            ++nl;
            /* || == OR (&& == AND)
            evaluation of the following line
            will stop as soon as the truth or
            falsehood is known, so the order matters */
        if (c == ' ' || c == '\n' == c == '\t')
            state = OUT;
        else if (state == OUT){
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl, nw, nc);
}

字符串

nfs0ujit

nfs0ujit1#

在MacOS上:

gcc kr_wc.c -o kr_wc
./kr_wc < example_text.txt

字符串
输出示例:

40 260 1397


其中example_text.txt是此文件:

1.5.4 Word Counting

The fourth in our series of useful programs counts lines, words, and
characters, with the loose definition that a word is any sequence of
characters that does not contain a blank, tab or newline. This is a
bare-bones version of the UNIX program wc.

#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
int 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("%d %d %d\n", nl, nw, nc);
}

Every time the program encounters the first character of a word, it
counts one more word. The variable state records whether the program
is currently in a word or not; initially it is "not in a word", which
is assigned the value OUT. We prefer the symbolic constants IN and
OUT to the literal values 1 and 0 because they make the program more
readable. In a program as tiny as this, it makes little difference,
but in larger programs, the increase in clarity is well worth the
modest extra effort to write it this way from the beginning. You'll
also find that it's easier to make extensive changes in programs
where magic numbers appear only as symbolic constants.

6ovsh4lw

6ovsh4lw2#

K&R第二版中的单词计数程序是在一个环境中运行的,在这个环境中,你可以以某种方式发出输入结束的信号。通常情况下,因为他们一直使用UNIX,他们使用Ctrl-D序列(如果你在Linux或任何类似Unix的操作系统中运行程序,这是有效的)。
Windows通过输入Ctrl-Z(可能后跟键盘回车键)来表示控制台应用程序中的输入结束
如果你从一个文件重定向输入(比如你说a.out <my_input_file.txt),你会在文件结束时得到单词的数量,当文件中没有更多的输入时。
你在IDE中运行程序,这通常会隐藏你的标准输入和标准输出,或者如何向你显示的窗口发出信号,告诉你程序没有更多的输入。
为了让程序结束,你必须首先知道如何结束输入。

nhaq1z21

nhaq1z213#

K&R中的例子省略了main的返回类型,这在现代C中是无效的,所以在main()之前添加int

#include <stdio.h>

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

/* counts lines, words and characters as input */

int main()
{
    int c, nl, nw, nc, state;

    state = OUT;
    /* set these three constants to 0: */
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF){
        ++nc;
        if (c == '\n')
            ++nl;
            /* || == OR (&& == AND)
            evaluation of the following line
            will stop as soon as the truth or
            falsehood is known, so the order matters */
        if (c == ' ' || c == '\n' == c == '\t')
            state = OUT;
        else if (state == OUT){
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl, nw, nc);
}

字符串
“* 我实际上是如何输入东西的?当我按回车键时,什么也没有发生。
如果你的IDE有问题,只需运行online
你如何测试单词计数程序 *"?
引用一组K&R解决方案的作者对这个特定问题here的回答:
听起来他们真的想让程序员学习如何做单元测试。我会提交以下内容:
1.输入文件包含零个字
1.输入文件包含1个巨大的单词,没有任何换行符
1.输入文件包含全部白色空格,不带换行符
1.输入文件包含66000个换行符
1.输入文件包含word/{巨大的不同类型的空格序列}/word
1.输入文件包含66000个单字母单词,66到一行
1.输入文件包含66000字,没有任何换行符
1.输入文件为/usr/dict contents(或等效文件)
1.输入文件是moby单词的完整集合
1.输入文件是二进制的(例如,它自己的可执行文件)
1.输入文件为/dev/null(或等效文件)
选择66000来检查小整数机器上的整数溢出。

相关问题