统计单词长度,输出直方图.输出垂直直方图的方法和像素扫描一样,可以计算出最高的那行,然后一行一行的输出
#include
#define IN 1
#define OUT 0
#define MAXLEN 20
int word[MAXLEN];
int main(void)
{
int c,i;
int stat;
int wc,maxvalue,overload;
stat = OUT;
overload = 0;
for(i = 0;i < MAXLEN;i ++)
word[i] = 0;
while((c = getchar()) != EOF){
if(c == ' ' || c == '\n' || c == '\t'){
stat = OUT;
if(wc > 0){
if(wc < MAXLEN){
//printf("wc %d \n",wc);
word[wc]++;
}else{
overload ++;
}
}
wc = 0;
}else if(stat == OUT){
stat = IN;
wc = 1;
}else{
wc ++;
}
}
maxvalue = 0;
for(i = 0;i < MAXLEN;i ++){
if(word[i] > maxvalue){
maxvalue = word[i];
}
}
int j;
for(i = maxvalue;i > 0;i --){
printf("%d |",i);
for(j = 0;j < MAXLEN;j ++){
if(word[j] >= i)
printf("* ");
else
printf(" ");
}
printf("\n");
}
printf(" ------------------------------------------------------------------------------------\n");
printf(" ");
for(i = 0;i < MAXLEN;i ++){
printf("%d ",i);
}
printf("\n");
printf("the overload word %d \n",overload);
return 0;
}
阅读(884) | 评论(0) | 转发(0) |