C language counts the number of words in the input and prints a histogram of word lengths

Preface

This program comes from exercises 1-13 in K&R's "C Programming Language" book. The original question requires drawing histograms in both horizontal and vertical directions.

Ideas

When you see the question, first think about how to store it. This question just uses an array to store the number of words of different lengths. Different subscripts represent different lengths. Be careful to limit the maximum length to determine the size of the array.
As for how to count the number of words and word length, the rough procedure has been given in the previous pages of this exercise, and it can be modified slightly.
The horizontal histogram is relatively simple and only requires two loops. To draw the vertical histogram, I first counted the maximum value of the quantity, that is, the height of the highest column in the histogram, and drew the vertical histogram layer by layer based on this parameter. The following is the code and running results:

code

/*
* Author: LiYuqi
* Date: 2023/7/16
* Description: 统计输入的单词个数,并根据单词长度绘制直方图,最大长度此处限定为MAXLENGTH.
*/

#include <stdio.h>

#define IN 1            /* 在单词内部 */
#define OUT 0           /* 在单词外部 */
#define MAXLENGTH 12    /* 允许的最大单词长度 */

int main()
{
    
    
    int length[MAXLENGTH]; /* 记录长度的数组 */
    int c, wordNum, wordLength, maxNum, state;

    state = OUT;
    maxNum = -1; /* maxNum用于记录单词长度最多的数量,以方便垂直直方图的绘制*/
    wordNum = wordLength = 0;
    for (int i = 0; i < MAXLENGTH; i++) {
    
    
        length[i] = 0;
    }
    
    while ((c=getchar()) != EOF) {
    
    
        if (c == ' ' || c == '\n' || c == '\t') {
    
    
            state = OUT;
            if (wordLength != 0 && wordLength <= MAXLENGTH) {
    
    
                ++length[wordLength-1]; //由于不存在长度为0的单词,所以长度从1开始,下标=长度-1.
                maxNum = length[wordLength-1]>maxNum? length[wordLength-1]: maxNum;
            }
            wordLength = 0;
        }
        else {
    
    
            if (state == OUT) ++wordNum;
            state = IN;
            ++wordLength;
        }
    }
    
    printf("\n单词总数为:%d\n", wordNum);
    printf("各单词的长度直方图为:\n");
    /* 水平直方图 */
    for (int i = 0; i < MAXLENGTH; i++) {
    
    
        printf("%2d: ", i+1);
        for (int j = 0; j < length[i]; j++) {
    
    
            printf("*");
        }
        printf("\n");
    }
    /* 垂直直方图 */
    printf("--------------分割线--------------\n");
    for (int i = maxNum; i > 0; i--)
    {
    
    
        for (int j = 0; j < MAXLENGTH; j++)
        {
    
    
            if (length[j] - i >= 0) {
    
    
                printf("* ");
                --length[j];
            } else {
    
    
                printf("  ");
            }
        }
        printf("\n");
        
    }
    for (int i = 0; i < MAXLENGTH; i++)
    {
    
    
        printf("%d ", i+1);
    }
    
    return 0;
}

operation result

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43390123/article/details/131752768