英単語の出現頻度を求めるプログラムを作りたいです。
今は頭文字が同じだと同じ単語と判別されてしまいます。
どうすればいいですか?
C言語
1#include<stdio.h> 2#include<stdlib.h> 3#include<string.h> 4 5 6 struct node{ 7 char word[50]; 8 int kaisu; 9 char tango[100]; 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[100]=a; 37 x->kaisu=1; 38 x->left=NULL; 39 x->right=NULL; 40 }else if(a== x->tango[100]) 41 x->kaisu++; 42 else if(a< x->tango[100]) 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 %5d\n",x->tango[100],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
入力する英文
This is a test file.
This file has two sentences.
END
実行結果
T 2
a 1 f 2 h 1 i 1 s 1 t 2
回答3件
あなたの回答
tips
プレビュー