C 习题

作者: 吃柠檬的鸮 | 来源:发表于2019-03-23 16:29 被阅读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
$

相关文章

  • C 习题

    练习 1 - 12 编写一个程序,以每行一个单词的形式打印其输出。 编译运行结果:

  • C 习题

    练习 1 -13 编写一个程序,打印输入中单词长度的直方图。 编译运行结果如下:

  • C 习题

    练习 1 - 17 编写一个程序,打印长度大于 80 个字符的所有输入行。 编译运行结果: 练习 1 - 18 编...

  • C语言练习题: 函数部分

    C语言练习题:函数部分(9题) 上一篇: C语言练习题:循环部分 下一篇: C语言练习题:数组部分 斐波那契,函数...

  • C语言练习题:循环部分

    C语言练习题:循环部分(20题) 上一篇: C语言练习题:if语句部分 下一篇: C语言练习题:函数部分 求一正整...

  • 第2章练习题答案

    习题1 答: 习题2 答: 习题3 答: 习题4 如果使用gcc -wall -o e4 e4.c编译,则有警告信...

  • C++ Primer Plus习题及答案-第九章

    C++ Primer Plus习题及答案-第九章 习题选自:C++ Primer Plus(第六版)内容仅供参考,...

  • 软考-项目管理(下)

    习题答案 1.1 - 1.9 C A D C D B D A D2.1 - 2.6 C D D D A C 知识点...

  • 软考-法律法规(下)

    习题答案 1.1 - 1.9 A A A B C C B D C2.1 - 2.4 D D D B3.1 - 3....

  • 软考-测试调试(下)

    习题答案 1.1 - 1.6 A B A C D C2.1 - 2.8 B D C D B B A B 知识点整理...

网友评论

      本文标题:C 习题

      本文链接:https://www.haomeiwen.com/subject/jvysvqtx.html