2013-07-12 71 views
3

所以我想要做的是做一個頻率的直方圖a,b,c,d和其他。我想知道爲什麼當我給程序輸入一個「abcde」的輸入時,'e'只出現一次時,'other'被計入多次。K&R練習1-14

我在尋找與輸入「ABCDE」的輸出爲

a:x 
b:x 
c:x 
d:x 
other:x 

實際ķ& [R運動是打印出現在輸入不同字符的直方圖。我的代碼看起來效率極低,所以我想知道我是否在正確的方向來回答這個練習。任何指導將不勝感激。代碼如下。

#include<stdio.h> 

main() 
{int i,c,na,nb, nc,nd,nother; 

na=nb=nc=nd=nother=0; 

while((c = getchar()) != EOF){ 
if(c== 'a') 
    na++; 
if(c== 'b') 
    nb++; 
if(c== 'c') 
    nc++; 
if(c== 'd') 
    nd++; 
else nother++; 
    } 
printf("a:"); 
for(i==0;i<na;i++){ 
putchar('x');} 

printf("\nb:"); 
for(i=0;i<nb;i++){ 
putchar('x');} 

printf("\nc:"); 
for(i=0;i<nc;i++){ 
putchar('x');} 

printf("\nd:"); 
for(i=0;i<nc;i++){ 
    putchar('x');} 

printf("\nother:"); 
for(i=0;i<nother;i++){ 
    putchar('x');} 



} 
+2

'for(i == 0; i Patashu

+0

順便說一句,有沒有人知道爲什麼此代碼(與接受的答案中提出的編輯)在Ideone工作,但無法在代碼塊中工作?即使我輸入的其他代碼在Ideone中都有效,但在代碼塊中不起作用。有誰知道爲什麼?我在CodeBlocks中輸入我的輸入,但沒有出現。 – MethequalsMath

回答

5

兩個問題。

第一個問題:

if(c== 'a') 
    na++; 
if(c== 'b') 
    nb++; 
if(c== 'c') 
    nc++; 
if(c== 'd') 
    nd++; 
else nother++; 

這不是其他人,如果鏈,因此它可以做多不同的事情。例如:

如果字符是,它擊中c == 'a'如果分支,也是「它不是c == 'd'」 else分支(因爲不等於「d」,它發展到了其他部分,並增加另) 。

修復它,如下所示:

if(c== 'a') 
    na++; 
else if(c== 'b') 
    nb++; 
else if(c== 'c') 
    nc++; 
else if(c== 'd') 
    nd++; 
else nother++; 

問題二:

for(i==0;i<na;i++){ 

當然應該

for(i=0;i<na;i++){ 
+2

另一個小問題是,字母后面的換行符也算作'其他'字符。 –

+1

@NithinBhaskar是不是取決於我在輸入中有多少換行符?如果輸入中只有一個換行符,您的建議是否會起作用? – MethequalsMath

+0

@MethequalsMath是的,它只適用於一個換行符。如果你想保持只有字符'a-z'的計數並且想要忽略空白和換行符等東西,你可以使用「isalpha()」api。 –

0

一是一些代碼來找出你的系統的特點:

#include<stdio.h> 

int main() 
{ 
    int i; 
    for(i=0; i < 128; ++i){ 
    printf("%3d ", i); 
    putchar(i); 
    printf("\n"); 
    } 
    } 

這會給你32到127的範圍。 然後這個工程

#include <stdio.h> 

/*Write a program to print a histogram of the frequencies of different characters 
in its input.*/ 

#define FIRST_CHAR 32 //characters start from here 
#define LAST_CHAR 127 // and last till here 
#define NUM_CHAR (LAST_CHAR - FIRST_CHAR) + 1 // total ~96 

main() 
{ 
    int i; 
    int nchars[NUM_CHAR]; 
    for(i = 0; i < NUM_CHAR; ++i) 
    nchars[i] = 0; 

    int input; 
    while((input=getchar()) != EOF) 
    if((input >= FIRST_CHAR) && (input <= LAST_CHAR)) 
     ++nchars[input- FIRST_CHAR]; //input minus first char 
            //will give pos in index. 
    int z; 
    char A = ' ';     //a char used 
    for(z=0; z < NUM_CHAR; ++z){ //for 
    printf("%2c", A);    //printing 
    ++A;       //list of letters 

    int counter = 0;    //use a counter to keep track of how many *'s printed 
    for(i=0; i < nchars[z]; i++){ 
     if(counter < 70){   //keep printing if less that 70 
    printf("*"); 
    ++counter;} 
     else{ 
    printf(">>>");   //else just print >>> and be done with it 
    counter = 0; 
    break;}     //breaks the nearest loop or switch 
     } 
    printf("\n");} 
} 
0
#include<stdio.h> 
main() 
{ 
    int charcount[95],c,i=0,max=0; 
    for(i=0;i<95;++i) /*Setting every array point to zero*/ 
    { 
     charcount[i]=0; 
    } 
    while((c=getchar())!=EOF) /*Frequency of each character*/ 
    {  
     ++charcount[c-32]; 
    } 
    for(i=0;i<95;++i) /*For character count of the longest word*/ 
    { 
     if(max<charcount[i]) 
    max=charcount[i]; 
    } 
    printf("\n"); 
    for(i=max;i>=0;--i) /*Printing the Vertical Histogram*/ 
    { 
     for(c=0;c<95;++c) 
    { 
     if(charcount[c]!=0) 
     { 
      if((charcount[c]-i)<0) 
     { 
      printf(" "); 
     } 
      else if(i==0)printf("=="); 
      else printf(" X"); 
     } 
    } 
     printf("\n"); 
    } 
    for(i=0;i<95;++i) /*Printing the Characters below the Histogram for reference*/ 
    { 
     if(charcount[i]!=0) 
    { 
     if(i==0)printf("sp "); /*We would write "sp" that stands for space*/ 
     else printf("%c ",i+32); 
    } 
    } 
    printf("\n"); 
} 

垂直對齊

輸出看起來像這樣: 「這不是斯巴達」,大叫士兵。信使走開了。 :)

X             
X             
X     X        
X     X        
X     X        
X     X        
X    X X      X  
X    X X   X   X  
X    X X X X X X   X X X  
X X  X  X X X X X X X X X X X X X X 
X X X X X X X X X X X X X X X X X X X X X X X X X 
================================================== 
sp ") , . : S T a d e g h i k l m n o p r s t w y