質問編集履歴

3

修正

2022/07/07 17:06

投稿

ambitious
ambitious

スコア0

test CHANGED
@@ -1 +1 @@
1
- C 文章中の単語出現頻度の計算
1
+ C  単語出現頻度を調べる
test CHANGED
@@ -21,155 +21,7 @@
21
21
  ### 該当のソースコード
22
22
 
23
23
  ```C
24
- #include <stdio.h>
25
- #include <stdlib.h>
26
- #include <string.h>
27
24
 
28
- struct node
29
- {
30
- char key[20]; // 単語を格納
31
- int count; // 単語の登録回数(出現頻度)を格納
32
- struct node *left, *right;
33
- };
34
- typedef struct node CELL;
35
-
36
- CELL *tinsert(CELL *, char *); /* 2分探索木に単語を登録する関数 */
37
- int inorder(CELL *); /* 間順走査を行う関数 */
38
- CELL *tsearch(CELL *, char *); /* 2分探索木を探索する関数 */
39
-
40
- int main(void)
41
- {
42
- CELL *root, *p, *q;
43
- char name[20];
44
- char sfn[] = "*.txt";
45
- FILE *sfp;
46
- char c;
47
- char str[256];
48
- int a, i, j = 0;
49
-
50
- if ((sfp = fopen(sfn, "r")) == NULL)
51
- {
52
- printf("File open error \n");
53
- exit(1);
54
- }
55
-
56
- printf("\n**************************\n");
57
- printf("入力ファイル名: %s\n\n", sfn);
58
- root = NULL;
59
- i = 0;
60
- while ((c = fgetc(sfp)) != EOF)
61
- {
62
- if (c != 32 && c != 44 && c != 46 && c != 58 && c != 59 && c != 28 && c != 29 && 48 > c && c > 57)
63
- {
64
- if (65 <= c && c <= 90)
65
- c += 32;
66
- str[i] = c;
67
- i++;
68
- }
69
- else if (j == 0)
70
- {
71
- printf("%s", str);
72
- root = tinsert(root, str);
73
- j++;
74
- i = 0;
75
- for (i = 0; i < 20; i++)
76
- {
77
- str[i] = '\0'; //初期化
78
- }
79
- }
80
- else if (j == 1)
81
- {
82
- printf("%s", str);
83
- q = tinsert(root, str);
84
- i = 0;
85
- for (i = 0; i < 20; i++)
86
- {
87
- str[i] = '\0'; //初期化
88
- }
89
- }
90
- }
91
-
92
- printf("*** 単語の出現頻度 ***\n");
93
-
94
- a = inorder(root);
95
- printf("%d種類の単語を登録\n", a);
96
-
97
- while (1)
98
- {
99
- printf("\n単語, または00を入力して下さい(00なら終了します): "); /*** 00 が入力されるまで繰り返す***/
100
- scanf("%s", name);
101
- if (strcmp(name, "00") == 0)
102
- break;
103
- else
104
- {
105
- q = tsearch(root, name);
106
- if (q != NULL)
107
- printf("%sの出現頻度: %d\n", name, q->count);
108
- }
109
- }
110
- printf("終了します.\n");
111
-
112
- fclose(sfp);
113
-
114
- return 0;
115
- }
116
-
117
- CELL *tinsert(CELL *p, char *name)
118
- {
119
- int ind;
120
-
121
- if (p == NULL)
122
- {
123
- p = (CELL *)malloc(sizeof(CELL));
124
- strcpy(p->key, name);
125
- p->count = 1;
126
- p->left = p->right = NULL;
127
- return p;
128
- }
129
-
130
- else if (strcmp(name, p->key) > 0)
131
- tinsert(p->right, name);
132
- else if (strcmp(name, p->key) < 0)
133
- tinsert(p->left, name);
134
- else if (strcmp(name, p->key) == 0)
135
- {
136
- p->count += 1;
137
- return p;
138
- }
139
- }
140
- int inorder(CELL *p)
141
- {
142
- int count = 0;
143
- if (p != NULL)
144
- {
145
- count += inorder(p->left);
146
- printf("%d %s", p->count, p->key);
147
- count++;
148
- count += inorder(p->right);
149
- }
150
- else
151
- return count;
152
- }
153
- CELL *tsearch(CELL *p, char *name)
154
- {
155
- int ind;
156
-
157
- if (p != NULL)
158
- {
159
- if (strcmp(name, p->key) > 0)
160
- tsearch(p->right, name);
161
- else if (strcmp(name, p->key) < 0)
162
- tsearch(p->left, name);
163
- else if (strcmp(name, p->key) == 0)
164
- return p;
165
- }
166
-
167
- else
168
- {
169
- printf("%sの単語は登録されていません!\n", name);
170
- return NULL;
171
- }
172
- }
173
25
  ```
174
26
 
175
27
  ### 試したこと

2

質問文の編集

2022/07/07 05:10

投稿

ambitious
ambitious

スコア0

test CHANGED
File without changes
test CHANGED
@@ -9,8 +9,14 @@
9
9
  出現頻度の計算、表示、検索
10
10
 
11
11
  ### 発生している問題・エラーメッセージ
12
- 実行と同時に、segmentation faultになってしまう
13
- 実装に自信のない箇所がある。
12
+ 出力結果が想定通りでない
13
+
14
+ 89 apple
15
+ 23 grape
16
+ 2 lemon
17
+ 1200種類の単語を登録
18
+ としたいです。
19
+
14
20
 
15
21
  ### 該当のソースコード
16
22
 
@@ -167,7 +173,7 @@
167
173
  ```
168
174
 
169
175
  ### 試したこと
170
- faultになる理由が分かりません。
176
+ 出力結果が想定通りになりません。
171
177
 
172
178
  ### 補足情報(FW/ツールのバージョンなど)
173
179
 

1

修正版ソースコードNo.1

2022/07/07 05:06

投稿

ambitious
ambitious

スコア0

test CHANGED
File without changes
test CHANGED
@@ -50,28 +50,36 @@
50
50
  printf("\n**************************\n");
51
51
  printf("入力ファイル名: %s\n\n", sfn);
52
52
  root = NULL;
53
-
53
+ i = 0;
54
54
  while ((c = fgetc(sfp)) != EOF)
55
55
  {
56
- i = 0;
57
- while (c != 32 && c != 44 && c != 46 && c != 58 && c != 59 && c != 28 && c != 29)
56
+ if (c != 32 && c != 44 && c != 46 && c != 58 && c != 59 && c != 28 && c != 29 && 48 > c && c > 57)
58
57
  {
59
58
  if (65 <= c && c <= 90)
60
59
  c += 32;
61
60
  str[i] = c;
62
61
  i++;
63
62
  }
64
- if (j == 0)
63
+ else if (j == 0)
65
64
  {
65
+ printf("%s", str);
66
66
  root = tinsert(root, str);
67
67
  j++;
68
+ i = 0;
69
+ for (i = 0; i < 20; i++)
70
+ {
71
+ str[i] = '\0'; //初期化
72
+ }
68
73
  }
69
- else
74
+ else if (j == 1)
75
+ {
76
+ printf("%s", str);
70
77
  q = tinsert(root, str);
71
-
78
+ i = 0;
72
- for (i = 0; i < 20; i++)
79
+ for (i = 0; i < 20; i++)
73
- {
80
+ {
74
- str[i] = '\0'; //初期化
81
+ str[i] = '\0'; //初期化
82
+ }
75
83
  }
76
84
  }
77
85
 
@@ -156,7 +164,6 @@
156
164
  return NULL;
157
165
  }
158
166
  }
159
-
160
167
  ```
161
168
 
162
169
  ### 試したこと