ヘッダーファイルを作成し、読み取ったtxtの文章に出現する各単語の出現回数を求めるプログラムを実装したいです。
方針としましては
1.Node構造体に出現頻度を表すメンバint freqを追加する
2.出現単語をinsertする際,freqをインクリメントすることで,
各単語の出現頻度を数え上げる
3.全単語をshowする際,"{id+1}¥t{str}¥t{freq}¥n"をprintfする
といった流れで作業したのですが、エラーメッセージを見ても修正すべき点が分かりません。どうかご教授お願い致します。
ヘッダーファイル↓(trie.h)
C
1#define NUM_OF_CHARS 26 2 3typedef struct node Node; 4struct node{ 5 int id; 6 int freq; 7 Node* next_char[NUM_OF_CHARS]; 8}; 9 10 11Node* create(){ 12 Node* node = (Node*) malloc(sizeof(Node)); 13 node->id = -1; 14 for(int i = 0; i < NUM_OF_CHARS; i++) node->next_char[i] = NULL; 15 return node; 16} 17 18void show(Node *curr, char *str, int pos){ 19 if(curr == NULL) return; 20 if(curr->id != -1){ 21 str[pos] = '\0'; 22 printf("%d\t%s\n", curr->id+1, str); 23 printf({id+1}\t{str}\t{freq}\n); 24 } 25 for(int i = 0; i < NUM_OF_CHARS; i++){ 26 if(curr->next_char[i] != NULL){ 27 str[pos] = i+'a'; 28 show(curr->next_char[i], str, pos+1); 29 } 30 } 31} 32 33 34void insert(Node *freq, char *str){ 35 static int WID = 0; 36 if(freq == NULL) return; 37 while(*str){ 38 char ch = *str-'a'; 39 if(freq->next_char[ch] == NULL) freq->next_char[ch] = create(); 40 freq = freq->next_char[ch]; 41 str++; 42 } 43 if(freq->id == -1) freq->id = WID++; 44} 45
ソースファイル↓(word_freq.c)
C
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include "trie.h" 5 6int N; 7char **words; 8 9void read_text(char *fn){ 10 FILE *fp = fopen(fn, "r"); 11 char str[32]; 12 fscanf(fp, "%d\n", &N); 13 words = (char **) malloc(N*sizeof(char *)); 14 for(int i = 0; i < N; i++){ 15 fgets(str, 32, fp); 16 char *p = strchr(str, '\n'); 17 *p = '\0'; 18 int len = strlen(str); 19 words[i] = (char *) calloc(len+1, sizeof(char)); 20 strcpy(words[i], str); 21 } 22fclose(fp); 23} 24 25int main(int argc, char *argv[]){ 26 read_text(argv[1]); 27 Node* root = create(); 28 for(int i = 0; i < N; i++) insert(root, words[i]); 29 char str[32]; 30 show(root, str, 0); 31} 32
実現したい実行結果↓
./cpgm/word_freq data/news.txt | sort -n -k 3 -r | head
13 the 12
23 to 9
11 huawei 6
6 of 4
1 google 4
22 states 3
21 united 3
9 with 2
87 is 2
77 components 2
エラーメッセージ↓
cpgm/trie.h: In function ‘create’:
cpgm/trie.h:12:24: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
Node* node = (Node*) malloc(sizeof(Node));
^
cpgm/trie.h:12:24: warning: incompatible implicit declaration of built-in function ‘malloc’
cpgm/trie.h:12:24: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
cpgm/trie.h:14:62: error: ‘NULL’ undeclared (first use in this function)
UM_OF_CHARS; i++) node->next_char[i] = NULL;
^
cpgm/trie.h:14:62: note: each undeclared identifier is reported only once for each function it appears in
cpgm/trie.h: In function ‘show’:
cpgm/trie.h:19:14: error: ‘NULL’ undeclared (first use in this function)
if(curr == NULL) return;
^
cpgm/trie.h:22:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("%d\t%s\n", curr->id+1, str);
^
cpgm/trie.h:22:5: warning: incompatible implicit declaration of built-in function ‘printf’
cpgm/trie.h:22:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
cpgm/trie.h:23:12: error: expected expression before ‘{’ token
printf({id+1}\t{str}\t{freq}\n);
^
cpgm/trie.h:23:12: error: stray ‘\’ in program
cpgm/trie.h:23:12: error: stray ‘\’ in program
cpgm/trie.h:23:12: error: stray ‘\’ in program
cpgm/trie.h: In function ‘insert’:
cpgm/trie.h:36:6: error: ‘curr’ undeclared (first use in this function)
if(curr == NULL) return;
^
cpgm/trie.h:36:14: error: ‘NULL’ undeclared (first use in this function)
if(curr == NULL) return;
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/05 04:53