本书的C数组练习的直观方法。 [ex1-13]

时间:2019-06-24 22:36:37

标签: c arrays

我想根据用户输入来创建符号直方图,详细说明其段落的字数:

我试图找到每个单词的len并将其记录在数组中(++表示1-3个字母,3-5个单词的每个单词),然后将它们打印出来。

/* Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histrogram with bars, horizontal; a vertical oreientation is more challenging */
#include <stdio.h>
int main()
{
    int wordCountHistogram[5];
    int len = -1;
    char c;
    while ((c = getchar()) != EOF) {
        ++len;
        if (c == '\t' || c == '\n' || c == ' '){
            if (0 <= len <= 3)
                ++wordCountHistogram[0];

            if (4 <= len <= 6)
                ++wordCountHistogram[1];

            if (7 <= len <= 8)
                ++wordCountHistogram[2];

            if (9 <= len <= 13)
                ++wordCountHistogram[3];

            if (len > 14)
                ++wordCountHistogram[4];

            /*if (12 <= len <= 14)
                wordCountHistogram[5]++;

            if (14 <= len < 15)
                wordCountHistogram[6]++;

            if (16 <= len < 17)
                wordCountHistogram[7]++;

            if (17 <= len < 18)
                wordCountHistogram[8]++;

            if (19 <= len < 9999)
                wordCountHistogram[9]++;
        */
            printf("%d", len);
            len = -1;
    }
}

    for(int i = 0; i < 5 ;i++){
        printf("%d \n",wordCountHistogram[i]);
        for(int n = wordCountHistogram[i];n >= 0;n--){//Histogram sizeof(array) / sizeof(array[0]))
            printf("+");}
        printf("\n");
    }

}

我知道每次打印都会记录“符号”长度,我希望将记录的每个len记录到数组的相应部分中(将数组下标值增加1),然后从此类数据中打印出直方图。但是,我得到了一个相当时髦的错误,结果导致直方图错误地打印出来。

我知道打印功能存在一些时髦的问题,但问题的核心仍然在于wordCountHistogram的分配。

3 个答案:

答案 0 :(得分:3)

几件事:

  1. 您需要将直方图数组初始化为0:
    int wordCountHistogram[5] = {0};
  2. 0 <= len <= 3这样的表达式无法按您期望的方式工作-您需要使用0 <= len && len <= 3
  3. 您可以使用isspace函数(在ctype.h中声明)来检查字符是否为空格:
    if ( isspace( c ) )
    {
      // update histogram
    }
    
  4. 将合并操作抽象到一个单独的函数中可能是一个好主意,例如:
    int bin( int len )
    {
      if ( 0 <= len && len <= 3 )
        return 0;
      if ( 4 <= len && len <= 6 )
        return 1;
      if ( 7 <= len && len <= 8 )
        return 2;
      ...
    }
    int main( void )
    {
      ...
      if ( isspace( c ) )
        ++wordCountHistogram[ bin( len ) ];
      ...
    }
    
    
    注意-c应该声明为int,而不是char-getchar()返回int。 您将需要添加一些完整性检查,以确保bin的返回值不会超出数组的范围。

答案 1 :(得分:1)

您没有初始化数组,这使您假设要使用值填充它。当您按ctrl + D打印出“ +”号时,这可能会导致数组中有垃圾。我建议在声明它时对其进行定义:

int wordCountHistogram[5] = {0};

我进行了更改,同时更改了您的评估:

if ((0 <= len) && (len  <= 3))

&您的程序给了我一个直方图。

答案 2 :(得分:0)

/* Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histrogram with bars, horizontal; a vertical oreientation is more challenging */
#include <stdio.h>
int main()
{
    int wordCountHistogram[5] = {};
    int len = -1;
    char c;
    while ((c = getchar()) != EOF) {
        ++len;
        if (c == '\t' || c == '\n' || c == ' '){
            if ((0 <= len) && (len  <= 3))
                ++wordCountHistogram[0];

            if ((4 <= len) && (len  <= 6))
                ++wordCountHistogram[1];

            if ((6 <= len) && (len  <= 8))
                ++wordCountHistogram[2];

            if ((9 <= len) && (len  <= 13))

                ++wordCountHistogram[3];

            if (len > 14)
                ++wordCountHistogram[4];

            /*if (12 <= len <= 14)
                wordCountHistogram[5]++;

            if (14 <= len < 15)
                wordCountHistogram[6]++;

            if (16 <= len < 17)
                wordCountHistogram[7]++;

            if (17 <= len < 18)
                wordCountHistogram[8]++;

            if (19 <= len < 9999)
                wordCountHistogram[9]++;
        */
            printf("%d", len);
            len = -1;
    }
}

    for(int i = 0; i < 5 ;i++){
        //printf("%d \n",wordCountHistogram[i]);

        for(int n = wordCountHistogram[i];n >= 0;n--){//Histogram sizeof(array) / sizeof(array[0]))
                    printf("+");}
        printf("\n");
    }

}

谢谢-经验教训。