我试图通过创建一个水平方向的直方图来解决K& R C练习1-13,该直方图测量用户输入中不同长度的单词的频率。这是我的代码:
#include <stdio.h>
main(){
int a, b, c, d; //array_position, word_length, getchar(), word_count
int y[10]; //array representing word lengths from 1-10 letters
b = 0; //word_length initializes with a zero value
a = 1; //array_position initializes with a value of one (no words contain zero characters)
for(y[a] = 0; (c = getchar()) != EOF;){ //current array position initializes with a zero value, and c initializes as the current input character
if(c != ' ' && c != '\n' && c != '\t') //if c is anything other than a blank, newline or tab
++b; //increment the word_length by one
else //otherwise
++a; //increment the array_position by one
b = 0; //and reset the word_length to zero
}
a = 1; //reset the array_position to one
do{
if(y[a] > 0){ //if current array_position holds a value greater than 0
printf("%i",a); //print the name of the array_position (1,2,3...)
for(d = 0; d < y[a]; ++d){ //reset the word_length--if the word_length is less than the value contained in the current array position, increment the word length by one
putchar('-'); //print a dash and re-evaluate the condition
}
if(d == y[a]){ //when the word_length is equal to the value contained in the current array position
putchar('-'); //print a final dash
putchar('\n'); //and a newline
}
}
++a; //increment the array position by one
}while(a > 11); //repeat until the current array position is 11
}
该代码旨在生成一个非常简单的图表,看起来像这样,排序长度为1-10个字符的单词:
1---
2----------
3-----
4--
等等。它还将省略任何未被输入中的一个或多个单词表示的长度。但是,上面显示的代码会在所有中返回 no 输出,并且我已经在这个问题上工作了三天。
我无法看到是阻止我的代码产生所需的输出?
答案 0 :(得分:1)
在这个循环中
a = 1; //reset the array_position to one
do{
if(y[a] > 0){ //if current array_position holds a value greater than 0
printf("%i",a); //print the name of the array_position (1,2,3...)
for(d = 0; d < y[a]; ++d){ //reset the word_length--if the word_length is less than the value contained in the current array position, increment the word length by one
putchar('-'); //print a dash and re-evaluate the condition
}
if(d == y[a]){ //when the word_length is equal to the value contained in the current array position
putchar('-'); //print a final dash
putchar('\n'); //and a newline
}
}
++a; //increment the array position by one
}while(a > 11);
您需要检查while (a < 11)
而不是while (a > 11)
并且,(正如@WeatherVane指出的那样):
if(c != ' ' && c != '\n' && c != '\t') //if c is anything other than a blank, newline or tab
++b; //increment the word_length by one
else //otherwise
++a; //increment the array_position by one
b = 0;
看起来很可疑,你是否忘记了第二个街区附近的牙箍?
if(c != ' ' && c != '\n' && c != '\t') //if c is anything other than a blank, newline or tab
++b; //increment the word_length by one
else { //otherwise
++a; //increment the array_position by one
b = 0;
}
答案 1 :(得分:1)
两个主要问题:
首先,如前所述,您的do..while
循环应检查a < 11
而不是a > 11
。
第二个问题出现在您的第一个for
循环中:
for(y[a] = 0; (c = getchar()) != EOF;){
if(c != ' ' && c != '\n' && c != '\t')
++b;
else
++a;
b = 0;
}
除了else
部分周围缺少大括号之外,当你第一次进入循环时,除了位置1之外,你永远不会为y
的元素赋值。
您需要将数组元素初始化为0,然后需要增加y[b]
而不是a
:
for (d=0;d<10;d++) {
y[d] = 0;
}
while ((c = getchar()) != EOF) {
if(c != ' ' && c != '\n' && c != '\t') {
++b;
} else {
++y[b];
b = 0;
}
}