C程序设计语言 (第二版) 练习1-11
练习 1-11 你准备如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢?
注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
#include <stdio.h>
#include <stdlib.h>#define IN 1
#define OUT 0int 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);system("pause");return 0;
}