繁体   English   中英

学习C,遇到程序计算字符并创建直方图的问题

[英]Learning C, ran into issue with program to count characters and create a histogram

我已经开始使用K&R书籍学习C的过程,并且遇到了一个练习,我尝试使用一个字符整数/计数程序并使其打印出数据的直方图,但是我正在设置直方图的初始边框,但遇到了一些问题。

#include <stdio.h>

int main() {
    int c, i, nwhite, nother, chartlen,
        chartline;  /* create variables for getchar, index number, whitespace
                       count, other char count, and establish variables for use
                       creating histogram*/
    int ndigit[10]; /* create list */
    nwhite = nother = 0;
    chartlen = 10;
    for (i = 0; i < 10; ++i) /* iterate through each item */
        ndigit[i] = 0;       /* Set each item to its initial count, 0 */

    while ((c = getchar()) != EOF)
        if (c >= '0' && c <= '9')
            ++ndigit[c - '0']; /* ASCII hex code subtraction to result in
                                  appropriate decimal interpretation */
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite; /*checks for white space chars and then adds one to the
                         count */
        else
            ++nother; /* if neither a number or white space is found, add to
                         other count */
    printf("digits =");
    for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d", nwhite, nother);

    for (i = 0; i < 10; ++i)
        if (ndigit[i] >
            chartlen) /*iterated through items from ndigit and check to see if
                         they're greater than the val of chartlen, if they are,
                         set chartlen to new value*/
            chartlen = ndigit[i];
        else
            continue;
    chartline = '-' * chartlen; /*create a line with the length of chartlen and
                                   then print it*/
    printf("%d\n", chartline);
}

当我第一次使用chartlen时,我一直遇到的问题就开始了。 随着节

for (i=0; i<10; ++i) 
    if (ndigit[i] > chartlen )

然后,如果已将其注释掉,则程序可以正常运行。 但是,如果新节留在“ nother”中,则似乎不再正确地计数,并且打印的值对我而言似乎是任意的,并且由于某种原因,我不知道如何获得用于打印直方图的线。 我做错了什么?

else continue; 只是令人困惑,并且chartline = '-' * chartlen; 只是chartline = 45 * chartlen; 更令人困惑。 printf("%d\\n", chartline); 只会打印一个数字:输入中最常用的数字乘以45。
找到直方图的高度并将其保存在chartlen ,没关系。 然后为每个数字的每一列打印直方图或空格的字符。

#include <stdio.h>

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

    nwhite = 0;
    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(", white space = %d, other = %d\n", nwhite, nother);

    printf("\n");
    printf("Histogram horizontally:\n");
    for (i = 0; i < 10; ++i) {
        int j;
        printf("%2d ", i);
        for (j = 0; j < ndigit[i]; ++j) {
            printf("*");
        }
        printf("\n");
    }
    printf("\n");
    printf("Histogram vertically:\n");
    for (chartlen = 0, i = 0; i < 10; ++i) {
        if (chartlen < ndigit[i])
            chartlen = ndigit[i];
    }
    for (i = 0; i < chartlen; ++i) {
        int j;
        printf(" ");
        for (j = 0; j < 10; ++j) {
            if (ndigit[j] >= chartlen - i) {
                printf("*");
            } else {
                printf(" ");
            }
            printf(" ");
            /* or shorter: printf("%c ", ndigit[j] >= chartlen - i ? '*' : ' '); */
        }
        printf("\n");
    }
    printf(" 0 1 2 3 4 5 6 7 8 9");
}

该程序为标准输入:

1234567899876352681543681254169234659237856427895653848467567846545438555463480000000329190328cfasvfhd adk af

将打印:

digits = 8 5 8 10 12 14 12 6 12 7, white space = 2, other = 1

Histogram horizontally:
 0 ********
 1 *****
 2 ********
 3 **********
 4 ************
 5 **************
 6 ************
 7 ******
 8 ************
 9 *******

Histogram vertically:
           *         
           *         
         * * *   *   
         * * *   *   
       * * * *   *   
       * * * *   *   
 *   * * * * *   *   
 *   * * * * *   * * 
 *   * * * * * * * * 
 * * * * * * * * * * 
 * * * * * * * * * * 
 * * * * * * * * * * 
 * * * * * * * * * * 
 * * * * * * * * * * 
 0 1 2 3 4 5 6 7 8 9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM