我有以下的c程序,应该打印我们的输入中的单词长度的垂直直方图。C程序,没有在终端打印
#include <stdio.h>
#define MAX_WORD_LENGTH 35 /* maximum word length we will support */
int main(void)
{
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');
}
}
}
我有这个编译成a.out的,在我做的./a.out终端,我输入一个词,没有任何反应。任何帮助?我是C新手,只是想学习。
正确地使用缩进格式化您的代码,并将大括号放在大的if和while中。我无法遵循这个代码。 – 2015-02-06 17:31:14