C:打印输入单词长度的直方图

#include<stdio.h>
#define IN 1
#define OUT 0
void main() {
    int c;
    int len[10];
    int state=OUT;
    int count=0;
    int i,j,k,m;
    for(i=0; i<10; i++) {
        len[i]=0;
    }
    while((c=getchar())!=EOF) {
        if(c==' '||c=='\t'||c=='\n') {
            state=OUT;
            if(count>=10)
                len[9]++;
            else if(count!=0)
                len[count-1]++;
            count=0;
        } else if(state==OUT) {
            state=IN;
            ++count;
        } else {
            ++count;
        }
    }
    int max=len[0];
    for(j=1; j<10; j++) {
        if(len[j]>max)
            max=len[j];
    }
    for(; max>=1; max--) {
        printf("%d|",max);
        for(k=0; k<10; k++) {
            if(len[k]>=max)
                printf("%s"," *");
            else
                printf("%s","  ");
        }
        printf("\n");
    }
    printf(" + - - - - - - - - - -\n");
    printf("  ");
    for(m=1; m<10; m++) {
        printf(" %d",m);
    }
    printf(" >");
}
C:打印输入单词长度的直方图_第1张图片

你可能感兴趣的:(C:打印输入单词长度的直方图)