2016-02-10 22 views
-1

我試圖通過創建一個水平方向的直方圖來測量用戶輸入中不同長度的單詞的頻率來解決K練習1-13。這裏是我的代碼:C字數直方圖應用程序返回無輸出

#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-- 

等。它也會忽略任何不是由輸入中的一個或多個單詞表示的長度。但是,上面顯示的代碼返回輸出根本沒有,我一直在處理這個問題三天。

我無法看到,這阻止了我的代碼產生所需的輸出?

+0

'做{...}而(一> 11)'應該是'爲(一= 1; a <= 10; a ++)',但請注意數組需要聲明爲int y [11]',以便數組中的有效索引爲0到10。在使用之前。另外,在更新數組之前,一定要檢查字長是否爲「<= 10」。許多單詞長於10個字母。 – user3386109

+0

循環應至少打印一次,但在遇到(a> 11)'時第一次停止。你確定它不應該像'a <11'那樣嗎? (打印語句取決於「if」 - 顯然你永遠不會到達那裏)。 – usr2564301

+1

你在第一個'else'塊中是否缺少'{大括號}?無論如何,'b'將被重置爲'0'。爲什麼你要遵循一個過時的教程,該教程具有'main()'而不是'int main(void)'? –

回答

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

兩個主要問題:

首先,正如前面提到的,你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;  
    }       
}