C //练习 1-11 你准备如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢?

C程序设计语言 (第二版) 练习1-11

练习 1-11 你准备如何测试单词计数程序?如果程序中存在某种错误,那么什么样的输入最可能发现这类错误呢?

注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
#include <stdio.h>
#include <stdlib.h>

#define IN 1
#define OUT 0

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);

	system("pause");
	return 0;
}

如果输入非字母字符,也会统计入单词。

猜你喜欢

转载自blog.csdn.net/navicheung/article/details/135363022