试图理解这段代码和getchar函数

时间:2014-08-31 18:40:22

标签: c algorithm

我正在尝试理解这段代码而且我很困惑: 我无法理解为什么需要三次getchar函数,我无法理解程序何时使用getchar函数。 代码工作得非常好,它从这里开始: http://www.zetadev.com/svn/public/k&r/exercise.1-13.c

#include <stdio.h>

/* Exercise 1-13: Write a program to print a histogram of the lengths of words
   in its input. It is easy to draw the histogram with the bars horizontal; a
   vertical orientation is more challenging. */

/* At this point we haven't learned how to dynamically resize an array, and we
   haven't learned how to buffer input, so that we could loop through it twice.
   Therefore, I'm going to make an assumption that no word in the input will be
   longer than 45 characters (per Wikipedia). */


#define MAX_WORD_LENGTH     45      /* maximum word length we will support */

main()
{
    int i, j;                     /* counters */
    int c;                        /* current character in input */
    int length;                   /* length of the current word */
    int lengths[MAX_WORD_LENGTH]; /* one for each possible histogram bar */
    int overlong_words;           /* number of words that were too long */

    for (i = 0; i < MAX_WORD_LENGTH; ++i)
        lengths[i] = 0;
    overlong_words = 0;

    while((c = getchar()) != EOF)
        if (c == ' ' || c == '\t' || c == '\n')
            while ((c = getchar()) && c == ' ' || c == '\t' || c == '\n')
                ;
        else {
            length = 1;
            while ((c = getchar()) && c != ' ' && c != '\t' && c != '\n')
                ++length;
            if (length < MAX_WORD_LENGTH)
                ++lengths[length];
            else
                ++overlong_words;

        }

    printf("Histogram by Word Lengths\n");
    printf("=========================\n");
    for (i = 0; i < MAX_WORD_LENGTH; ++i) {
        if (lengths[i] != 0) {
            printf("%2d ", i);
            for (j = 0; j < lengths[i]; ++j)
                putchar('#');
            putchar('\n');
        }
    }
}

2 个答案:

答案 0 :(得分:1)

getchar()用于确保在达到EOF时,程序 离开了while循环。

while((c = getchar()) != EOF)

这个getchar也在while循环中。它确保跳过空格字符' ''\t''\n'

        while ((c = getchar()) && c == ' ' || c == '\t' || c == '\n')
            ;

为了使程序更健壮,上面的代码应该是:

        while ((c = getchar()) != EOF && isspace(c))

这个getchar也在while循环中。它认为任何不是' ''\t''\n'的字符都是单词字符,并为每个这样的字符增加长度。

        while ((c = getchar()) && c != ' ' && c != '\t' && c != '\n')
            ++length;

再一次,为了使程序更加健壮,上面的代码应该是:

        while ((c = getchar()) != EOF && !isspace(c))

答案 1 :(得分:0)

函数getchar()从标准输入设备读取并返回一个字符。

语法为variable name = getchar(),变量可以是intchar类型。

以下是每次getchar()来电的解释。

  1. while((c = getchar()) != EOF)

    这将检查您是否输入EOF(即unix中的Ctrl + D和Windows系统中的Ctrl + Z)

  2. if (c == ' ' || c == '\t' || c == '\n') while ((c = getchar()) && c == ' ' || c == '\t' || c == '\n');

    这将检查您是否输入空格,制表符和换行符,如果是,则跳过这些并返回while((c = getchar()) != EOF)

  3. length = 1;

    while ((c = getchar()) && c != ' ' && c != '\t' && c != '\n')

    ++length;

    空格,制表符和换行符之外的任何其他字符都将导致执行++length