英文を入力し、英単語の出現頻度を求めるプログラムで、出力が文字化けしてしまいます。
「END」を検出したらプログラムを終了し、例えば「This is a pen. This is a book.」という英文を入力したら、
This 2
is 2
a 2
pen. 1
book. 1
と表示されるようにしたいです。
同じ単語であっても大文字と小文字の判定はしていないので違う単語と識別します。ピリオドがついている単語は違う単語と識別します。
元のプログラム↓
C言語
1#include<stdio.h> 2#include<stdlib.h> 3#include<string.h> 4 5 int strcmp(const char *s1,const char *s2); 6 struct node{ 7 char word[50]; 8 int kaisu; 9 char tango; 10 struct node *left; 11 struct node *right; 12}; 13 14 char wd[50]; 15 16 char *GetWord() 17 { 18 char a; 19 int len; 20 21 a=getchar(); 22 while(a==' ' || a=='\n') a=getchar(); 23 len=0; 24 while(a!=' ' && a!='\n'){ 25 wd[len++]=a; 26 a=getchar(); 27 } 28 wd[len]='\0'; 29 return &wd[0]; 30 } 31 32struct node *insert(struct node *x,int a) 33{ 34 if(x==NULL){ 35 x=(struct node *)malloc(sizeof(struct node)); 36 x->tango=a; 37 x->kaisu=1; 38 x->left=NULL; 39 x->right=NULL; 40 }else if(a== x->tango) 41 x->kaisu++; 42 else if(a< x->tango) 43 x->left=insert(x->left,a); 44 else 45 x->right=insert(x->right,a); 46 return x; 47 } 48 49void treeprint(struct node *x) 50{ 51 if(x!=NULL){ 52 treeprint(x->left); 53 printf("%5c %5c\n",x->tango,x->kaisu); 54 treeprint(x->right); 55 } 56} 57 58 int main(void) 59 { 60 struct node *root; 61 char *w; 62 63 root=NULL; 64 w=GetWord(); 65 while(strcmp(w,"***END***")!=0){ 66 root=insert(root,w); 67 w=GetWord(); 68 } 69 treeprint(root); 70 return 0; 71} 72
エラーコード
simple-getword.c: In function ‘main’:
simple-getword.c:66:24: warning: passing argument 2 of ‘insert’ makes integer from pointer without a cast [-Wint-conversion]
root=insert(root,w);
^
simple-getword.c:32:14: note: expected ‘int’ but argument is of type ‘char *’
struct node *insert(struct node *x,int a)
出力結果
This is a pen. This is a book.
END
�
�
�
�
�
�
�
�
修正したプログラム↓
C言語
1#include<stdio.h> 2#include<stdlib.h> 3#include<string.h> 4 5 int strcmp(const char *s1,const char *s2); 6 struct node{ 7 char word[50]; 8 int kaisu; 9 struct node *left; 10 struct node *right; 11}; 12 13 char wd[50]; 14 15 char *GetWord() 16 { 17 char a; 18 int len; 19 20 a=getchar(); 21 while(a==' ' || a=='\n') a=getchar(); 22 len=0; 23 while(a!=' ' && a!='\n'){ 24 wd[len++]=a; 25 a=getchar(); 26 } 27 wd[len]='\0'; 28 return &wd[0]; 29 } 30 31struct node *insert(struct node *x,char a) 32{ 33 if(x==NULL){ 34 x=(struct node *)malloc(sizeof(struct node)); 35 x->word=a; 36 x->kaisu=1; 37 x->left=NULL; 38 x->right=NULL; 39 }else if(strcmp(a,x->word)==0) 40 x->kaisu++; 41 else if(strcmp(a,x->word)<0) 42 x->left=insert(x->left,a); 43 else 44 x->right=insert(x->right,a); 45 return x; 46 } 47 48void treeprint(struct node *x) 49{ 50 if(x!=NULL){ 51 treeprint(x->left); 52 printf("%s %d\n",x->word,x->kaisu); 53 treeprint(x->right); 54 } 55 } 56 57 int main(void) 58 { 59 struct node *root; 60 char *w; 61 62 root=NULL; 63 w=GetWord(); 64 while(strcmp(w,"***END***")!=0){ 65 root=insert(root,w); 66 w=GetWord(); 67 } 68 treeprint(root); 69 return 0; 70}
エラーコード
getword2.c: In function ‘insert’:
getword2.c:35:12: error: assignment to expression with array type
x->word=a;
^
getword2.c:39:19: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
}else if(strcmp(a,x->word)==0)
^
In file included from getword2.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
^
getword2.c:41:19: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
else if(strcmp(a,x->word)<0)
^
In file included from getword2.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
^
getword2.c: In function ‘main’:
getword2.c:65:24: warning: passing argument 2 of ‘insert’ makes integer from pointer without a cast [-Wint-conversion]
root=insert(root,w);
^
getword2.c:31:14: note: expected ‘char’ but argument is of type ‘char *’
struct node *insert(struct node *x,char a)
warningとnoteの改善策もよければ教えていただきたいです
回答4件
あなたの回答
tips
プレビュー