对C程序设计语言练习1-13打印单词长度直方图的答案有所不解
以下是答案中的代码程序代码:
#include <stdio.h> #define MAXHIST 15 #define MAXWORD 12 #define IN 1 #define OUT 0 main() { int c, i, nc, len, state; int wl[MAXWORD]; int ovflow; int maxvalue; state = OUT; ovflow = 0; nc = 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 ++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为直方图中直方条的长度 len = 1; } else len = 0; while(len > 0) { putchar('*'); --len; } putchar('\n'); } if(ovflow > 0) printf("There are %d words >= %d\n", ovflow, MAXWORD); return 0; }
其中
程序代码:
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'); }
这里不能理解
1.理解不了len的这个计算方式怎么才会使它<=0
2.判断len == 0之后,使len = 1,到while循环的时候不是只会循环一次输出一个*吗
小白一名,卡在这里懵逼了很久,求解!