mac上的clion执行《c程序设计语言》中的习题,得不到直方图,输入了字符,啥都没输出,是哪块不对吗?
/*编写一个程序,打印输入中单词长度的直方图。水平方向的直方图比较容易绘制,垂直方向的直方图则要困难些。*/
/*水平方向直方图*/
int c, i, nc, state;
int overflow;
int len;
int maxvalue;
int wl[MAXWORD];
state = OUT;
nc = 0;
overflow = 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)
++wl[nc];
else
++overflow;
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 (overflow > 0)
printf("There are %d words >= %d\n", overflow, MAXWORD);