C 习题

2019-03-23  本文已影响0人  吃柠檬的鸮

练习 1 -13 编写一个程序,打印输入中单词长度的直方图。

/* histogramh.c */
/* 打印水平方向的直方图 */
#include <stdio.h>

int main() {
    int c, i, nwhite, nother;
    int ndigit[10];

    nwhite = nother = 0;
    for (i = 0; i < 10; ++i)
        ndigit[i] = 0;

    /* 字符统计 */
    while (( c = getchar()) != EOF)
        if (c >= '0' && c <= '9')
            ++ndigit[c - '0'];
        else if (c == ' ' || c == '\n' || c == '\t') 
            ++nwhite;
        else
            ++nother;
    
    printf("digits = ");
    for (i = 0; i < 10; ++i)
        printf(" %d", ndigit[i]);
    printf("\nwhite space = %d\nother = %d\n", nwhite, nother);

    /* 打印水平方向的直方图 */
    printf("value\t↑\n");
    for (i = 0; i < 10; ++i) {
        int j;
        printf("%d \t|", i);
        for (j = 0; j < ndigit[i]; ++j) {
            printf("■");
        }
        printf("\n\t|\n");
    }

    printf("space\t|");
    for (i = 0; i < nwhite; ++i) {
        printf("■");
    }
    printf("\n\t|\n");

    printf("other\t|");
    for (i = 0; i < nother; ++i) {
        printf("■");
    }
    printf("\n\t|--------------------------------------→ number\n\n");

    return 0;
}

编译运行结果如下:

$ ./histogramh.out 
123 445 67890 111   451
,   abcdef ghi 152670
digits =  2 6 2 1 3 3 2 2 1 1
white space = 9
other = 10
value   ↑
0       |■■
        |
1       |■■■■■■
        |
2       |■■
        |
3       |■
        |
4       |■■■
        |
5       |■■■
        |
6       |■■
        |
7       |■■
        |
8       |■
        |
9       |■
        |
space   |■■■■■■■■■
        |
other   |■■■■■■■■■■
        |--------------------------------------→ number

$ 
/* histogramv.c  */
/* 打印垂直方向的直方图 */
#include <stdio.h>

int main() {
    int c, i, nwhite, nother;
    int ndigit[10];
    int max;

    max = nwhite = nother = 0;
    for (i = 0; i < 10; ++i) {
        ndigit[i] = 0;
    }
    
    while ((c = getchar()) != EOF) {
        if (c >= '0' && c <= '9') {
            ++ndigit[c - '0'];
        } else if (c == ' ' || c == '\n' || c == '\t') {
            ++nwhite;
        } else {
            ++nother;
        }
    }
    
    printf("digits = ");
    for (i = 0; i < 10; ++i) {
        printf(" %d", ndigit[i]);
        max = max < ndigit[i] ? ndigit[i] : max;
    }
    printf(", white space = %d, other = %d\n", nwhite, nother);
    max = max < nwhite ? nwhite : max;
    max = max < nother ? nother : max;

    /* draw histogram */
    printf("nums\t↑\n\t|");

    for (i = max; i > 0; --i) {
        printf("\n\t|");

        int j;
        for (j = 0; j < 10; ++j) {
            if (ndigit[j] >= i) 
                printf(" ■  ");
            else 
                printf("    ");
        }

        if (nwhite >= i)
            printf("  ■  ");
        else 
            printf("     ");

        if (nother >= i)
            printf("   ■  ");
        else 
            printf("      ");
    }

    printf("\n\t|----------------------------------------------------→ value\n");
    printf("\t");
    for (i = 0; i < 10; ++i)
        printf("  %d ", i);
    printf("  space  ohter\n");
    

    return 0;
}
$ ./histogramv.out 
1234567890  2,4,8,16    32
64,128,256  abcdefg
digits =  1 3 5 2 3 2 4 1 3 1, white space = 5, other = 12
nums  ↑
      |
      |                                                ■  
      |                                                ■  
      |                                                ■  
      |                                                ■  
      |                                                ■  
      |                                                ■  
      |                                                ■  
      |         ■                                ■     ■  
      |         ■               ■                ■     ■  
      |     ■   ■       ■       ■       ■        ■     ■  
      |     ■   ■   ■   ■   ■   ■       ■        ■     ■  
      | ■   ■   ■   ■   ■   ■   ■   ■   ■   ■    ■     ■  
      |----------------------------------------------------→ value
        0   1   2   3   4   5   6   7   8   9   space  ohter
$
上一篇 下一篇

猜你喜欢

热点阅读