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

时间:2018-07-03 17:16:41

标签: c

我已经开始使用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”中,则似乎不再正确地计数,并且打印的值对我而言似乎是任意的,并且由于某种原因,我不知道如何获得用于打印直方图的线。我做错了什么?

1 个答案:

答案 0 :(得分:0)

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");
}

此程序用于stdin:

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