根据单词出现次数输出水平直方图编译能通过但是并没有直方图输出

新手上路,请多包涵

C源代码如下,请指点,谢谢!

#include <stdio.h>

#define		MAXHIST		18
#define		MAXWORD		16
#define		IN		1
#define		OUT		0

main()
{
	int c, i, nc, state;
	int len;
	int maxvalue;
	int ovflow;
	int wl[MAXWORD];
	
	state = OUT;
	nc = 0;
	ovflow = 0;
	for (i = 0; i < MAXWORD; i++)
		wl[i] = 0;
	while ((c = getchar()) != EOF) {
		if (c == ' ' || c == '\t' || c == '\n') {
			state = OUT;
			if (nc > 0)
				if (nc < MAXWORD)
					++nc;
				else
					++ovflow;
			nc = 0;
		} else if (state == OUT) {
			state = IN;
			nc = 1;
		} else
			++nc;
	}
	
	maxvalue = 0;
	for (i = 1; i < MAXWORD; ++i)
		if (wl[i] > maxvalue)
			maxvalue = wl[i];
			
	for (i = 1; i < MAXWORD; ++i) {
		printf("%5d - %5d : ", i, wl[i]);
		if (wl[i] > 0) {
			if ((len = wl[i] * MAXHIST / maxvalue) <= 0)
				len = 1;
		} else
			len = 0;
		while (len > 0) {
			putchar('*');
			--len;
		}
		putchar('\n');
	}
	if (ovflow > 0)
		printf("There are %d words >= %d\n", ovflow, MAXWORD);}
阅读 2.8k
1 个回答
✓ 已被采纳新手上路,请多包涵

找到问题所在了,在确定nc < MAXWORD 后应该自加的是wl[nc]而不是nc。

推荐问题
logo
101 新手上路
子站问答
访问
宣传栏