質問編集履歴
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -145,4 +145,156 @@
|
|
145
145
|
|
146
146
|
<質問>
|
147
147
|
プッシュ、サーチが出来るように実装したいです
|
148
|
-
char *nameは変えずにコード作成したいです。
|
148
|
+
char *nameは変えずにコード作成したいです。
|
149
|
+
|
150
|
+
<追記>
|
151
|
+
```
|
152
|
+
#include <stdio.h>
|
153
|
+
#include <string.h>
|
154
|
+
#include <stdlib.h>
|
155
|
+
/*--- int 型スタックを実現する構造体 ---*/
|
156
|
+
|
157
|
+
typedef struct{
|
158
|
+
double vision; /* 視力 */
|
159
|
+
int height; /* 身長 */
|
160
|
+
} Body ;
|
161
|
+
/*--- 身体検査データ型 ---*/
|
162
|
+
typedef struct{
|
163
|
+
Body body; /* 身体データ型 ---*/
|
164
|
+
char *name; /* 氏名 */
|
165
|
+
} PhysCheck ;
|
166
|
+
typedef struct {
|
167
|
+
int max; /* スタックの容量 */
|
168
|
+
int ptr; /* スタックポインタ */
|
169
|
+
PhysCheck **stk; /* スタック本体*/
|
170
|
+
} PhysCheckStack;
|
171
|
+
|
172
|
+
int Search(PhysCheckStack *s , PhysCheck *x){
|
173
|
+
for(int i = s->ptr-1 ; i >= 0 ; i --){
|
174
|
+
if(strncmp(s->stk[i]->name, x->name ,strlen(x->name)) == 0){//strncmp先頭n文字を比較
|
175
|
+
return i;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
return -1;
|
179
|
+
}
|
180
|
+
|
181
|
+
/*--- スタックの初期化 ---*/
|
182
|
+
int Initialize(PhysCheckStack *s, int max){
|
183
|
+
s->ptr = 0;
|
184
|
+
if ((s->stk = calloc(max, sizeof(PhysCheck *))) == NULL) {
|
185
|
+
s->max = 0; /* char* の配列の確保に失敗 */
|
186
|
+
return -1;
|
187
|
+
}
|
188
|
+
s->max = max;
|
189
|
+
return 0;
|
190
|
+
}
|
191
|
+
/*--- スタックにデータをプッシュ ---*/
|
192
|
+
int Push(PhysCheckStack *s, PhysCheck *x){
|
193
|
+
if (s->ptr >= s->max) return -1; /* スタック満杯 */
|
194
|
+
if ((s->stk[s->ptr] = calloc(strlen(x->name)+1, sizeof(PhysCheck *))) == NULL)
|
195
|
+
/* データをコピーするための動的な文字列保存用配列を確保することに失敗 */
|
196
|
+
return -1;
|
197
|
+
s->stk[s->ptr]->name = calloc(strlen(x->name)+1, sizeof(char));
|
198
|
+
strcpy(s->stk[s->ptr]->name,x->name);
|
199
|
+
s->stk[s->ptr]->body.vision = x->body.vision;
|
200
|
+
s->stk[s->ptr]->body.height = x->body.height;
|
201
|
+
s->ptr++;
|
202
|
+
return 0;
|
203
|
+
}
|
204
|
+
/*--- スタックからデータをポップ ---*/
|
205
|
+
int Pop(PhysCheckStack *s, PhysCheck *x){
|
206
|
+
if (s->ptr <= 0) return -1; /* スタックは空 */
|
207
|
+
s->ptr--;
|
208
|
+
strcpy(x->name,s->stk[s->ptr]->name);
|
209
|
+
x->body.vision = s->stk[s->ptr]->body.vision;
|
210
|
+
x->body.height = s->stk[s->ptr]->body.height;
|
211
|
+
free(s->stk[s->ptr]->name);
|
212
|
+
return (0);
|
213
|
+
}
|
214
|
+
/*--- スタックからデータをピーク ---*/
|
215
|
+
int Peek(PhysCheckStack *s, PhysCheck *x){
|
216
|
+
if (s->ptr <= 0) return -1;
|
217
|
+
strcpy(x->name,s->stk[s->ptr-1]->name);
|
218
|
+
x->body.vision = s->stk[s->ptr-1]->body.vision;
|
219
|
+
x->body.height = s->stk[s->ptr-1]->body.height;
|
220
|
+
return 0;
|
221
|
+
}
|
222
|
+
/*--- スタックの容量 ---*/
|
223
|
+
int Capacity(const PhysCheckStack *s){
|
224
|
+
return s->max;
|
225
|
+
}
|
226
|
+
/*--- スタックに積まれているデータ数 ---*/
|
227
|
+
int Size(const PhysCheckStack *s){
|
228
|
+
return s->ptr;
|
229
|
+
}
|
230
|
+
/*--- スタックの全データの表示 ---*/
|
231
|
+
void Print(const PhysCheckStack *s){
|
232
|
+
int i;
|
233
|
+
|
234
|
+
for(i = 0; i < s->ptr; i++)
|
235
|
+
printf("%s %f %d\n", s->stk[i]->name,s->stk[i]->body.vision,s->stk[i]->body.height);
|
236
|
+
putchar('\n');
|
237
|
+
}
|
238
|
+
void Terminate(PhysCheckStack *s){
|
239
|
+
if(s->stk != NULL)
|
240
|
+
free(s->stk);
|
241
|
+
s->max = s->ptr = 0;
|
242
|
+
}
|
243
|
+
int main(void){
|
244
|
+
PhysCheckStack s;
|
245
|
+
PhysCheck x;
|
246
|
+
int max;
|
247
|
+
char y[100];
|
248
|
+
printf("スタックの大きさを入力してください");
|
249
|
+
scanf("%d", &max);
|
250
|
+
if (Initialize(&s, max)==-1){
|
251
|
+
puts("スタックの生成に失敗しました.\n");
|
252
|
+
}
|
253
|
+
while (1) {
|
254
|
+
int menu;
|
255
|
+
printf("現在のデータ数:%d/%d\n",Size(&s), Capacity(&s));
|
256
|
+
printf("(1) プッシュ (2) ポップ (3) ピーク (4) 表示 (5) 探索 (0) 終了:");
|
257
|
+
scanf("%d", &menu);
|
258
|
+
if (menu == 0) break;
|
259
|
+
switch (menu) {
|
260
|
+
case 1: /* プッシュ */
|
261
|
+
printf("データ:");
|
262
|
+
scanf("%d", &x.body.height);
|
263
|
+
scanf("%s",y);
|
264
|
+
x.name = (char *)malloc(sizeof(char)*(strlen(y) + 1));
|
265
|
+
strcpy(x.name,y);
|
266
|
+
scanf("%lf",&x.body.vision);
|
267
|
+
if (Push(&s, &x) == -1)
|
268
|
+
puts("\a エラー:プッシュに失敗しました。");
|
269
|
+
break;
|
270
|
+
case 2: /* ポップ */
|
271
|
+
if (Pop(&s, &x) == -1)
|
272
|
+
puts("\a エラー:ポップに失敗しました。");
|
273
|
+
else
|
274
|
+
printf("ポップしたデータは%s %.1f %d です。\n", x.name,x.body.vision,x.body.height);
|
275
|
+
break;
|
276
|
+
case 3: /* ピーク */
|
277
|
+
if (Peek(&s, &x) == -1)
|
278
|
+
puts("\a エラー:ピークに失敗しました。");
|
279
|
+
else
|
280
|
+
printf("ピークしたデータは%s %.1f %d です。\n", x.name,x.body.vision,x.body.height);
|
281
|
+
break;
|
282
|
+
case 4: /* 表示 */
|
283
|
+
Print(&s);
|
284
|
+
break;
|
285
|
+
case 5://探索
|
286
|
+
scanf("%s",x.name);
|
287
|
+
int search = Search(&s,&x);
|
288
|
+
if(search == -1){
|
289
|
+
puts("パターンは存在しません");
|
290
|
+
} else{
|
291
|
+
printf("名前に含まれるパターンは%d個です\n",search);
|
292
|
+
}
|
293
|
+
}
|
294
|
+
}
|
295
|
+
return 0;
|
296
|
+
}
|
297
|
+
|
298
|
+
```
|
299
|
+
自分なりにやってみました。おそらくもっと効率の良いプログラムがあると思っていますが、今の自分でできることはしました。
|
300
|
+
ポインタについて勉強しなおします。。。
|
4
詳しく
title
CHANGED
File without changes
|
body
CHANGED
@@ -144,4 +144,5 @@
|
|
144
144
|
二回プッシュするとSegmentation fault (core dumped) と表示されます。
|
145
145
|
|
146
146
|
<質問>
|
147
|
-
プッシュ、サーチが出来るように実装したいです
|
147
|
+
プッシュ、サーチが出来るように実装したいです
|
148
|
+
char *nameは変えずにコード作成したいです。
|
3
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
<修正後>
|
1
2
|
```
|
2
3
|
|
3
4
|
#include <stdio.h>
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
```
|
2
|
+
|
2
3
|
#include <stdio.h>
|
3
4
|
#include <string.h>
|
4
5
|
#include <stdlib.h>
|
@@ -12,7 +13,7 @@
|
|
12
13
|
/*--- 身体検査データ型 ---*/
|
13
14
|
typedef struct{
|
14
15
|
Body body; /* 身体データ型 ---*/
|
15
|
-
char *name
|
16
|
+
char *name; /* 氏名 */
|
16
17
|
} PhysCheck ;
|
17
18
|
typedef struct {
|
18
19
|
int max; /* スタックの容量 */
|
@@ -23,8 +24,8 @@
|
|
23
24
|
int Search(PhysCheckStack *s , PhysCheck *x){
|
24
25
|
int count = 0;
|
25
26
|
for(int i = 0 ; i < s->ptr ; i ++){
|
26
|
-
if(strcmp(x->name
|
27
|
+
if(strcmp(x->name,s->stk[i].name) == 0){//string compare 文字一致したら0返す
|
27
|
-
printf("%s %f %d\n",s->stk[i].name
|
28
|
+
printf("%s %f %d\n",s->stk[i].name,s->stk[i].body.vision,s->stk[i].body.height);
|
28
29
|
count ++;
|
29
30
|
}
|
30
31
|
}
|
@@ -34,7 +35,7 @@
|
|
34
35
|
/*--- スタックの初期化 ---*/
|
35
36
|
int Initialize(PhysCheckStack *s, int max){
|
36
37
|
s->ptr = 0;
|
37
|
-
if ((s->stk[s->ptr].name
|
38
|
+
if ((s->stk[s->ptr].name = calloc(max, sizeof(char*))) == NULL) {
|
38
39
|
s->max = 0; /* char* の配列の確保に失敗 */
|
39
40
|
return -1;
|
40
41
|
}
|
@@ -44,8 +45,8 @@
|
|
44
45
|
/*--- スタックにデータをプッシュ ---*/
|
45
46
|
int Push(PhysCheckStack *s, PhysCheck *x){
|
46
47
|
if (s->ptr >= s->max) return -1; /* スタック満杯 */
|
47
|
-
strcpy(s->stk[s->ptr].name
|
48
|
+
strcpy(s->stk[s->ptr].name,x->name);
|
48
|
-
if ((s->stk[s->ptr].name
|
49
|
+
if ((s->stk[s->ptr].name = calloc(strlen(x->name)+1, sizeof(char*))) == NULL)
|
49
50
|
/* データをコピーするための動的な文字列保存用配列を確保することに失敗 */
|
50
51
|
return -1;
|
51
52
|
s->stk[s->ptr].body.vision = x->body.vision;
|
@@ -57,16 +58,16 @@
|
|
57
58
|
int Pop(PhysCheckStack *s, PhysCheck *x){
|
58
59
|
if (s->ptr <= 0) return -1; /* スタックは空 */
|
59
60
|
s->ptr--;
|
60
|
-
strcpy(x->name
|
61
|
+
strcpy(x->name,s->stk[s->ptr].name);
|
61
62
|
x->body.vision = s->stk[s->ptr].body.vision;
|
62
63
|
x->body.height = s->stk[s->ptr].body.height;
|
63
|
-
free(s->stk[s->ptr].name
|
64
|
+
free(s->stk[s->ptr].name);
|
64
65
|
return (0);
|
65
66
|
}
|
66
67
|
/*--- スタックからデータをピーク ---*/
|
67
68
|
int Peek(PhysCheckStack *s, PhysCheck *x){
|
68
69
|
if (s->ptr <= 0) return -1;
|
69
|
-
strcpy(x->name
|
70
|
+
strcpy(x->name,s->stk[s->ptr-1].name);
|
70
71
|
x->body.vision = s->stk[s->ptr-1].body.vision;
|
71
72
|
x->body.height = s->stk[s->ptr-1].body.height;
|
72
73
|
return 0;
|
@@ -84,7 +85,7 @@
|
|
84
85
|
int i;
|
85
86
|
|
86
87
|
for(i = 0; i < s->ptr; i++)
|
87
|
-
printf("%s %f %d", s->stk[i].name
|
88
|
+
printf("%s %f %d", s->stk[i].name,s->stk[i].body.vision,s->stk[i].body.height);
|
88
89
|
putchar('\n');
|
89
90
|
}
|
90
91
|
int main(void){
|
@@ -101,7 +102,7 @@
|
|
101
102
|
case 1: /* プッシュ */
|
102
103
|
printf("データ:");
|
103
104
|
scanf("%d", &x.body.height);
|
104
|
-
scanf("%s",x.name
|
105
|
+
scanf("%s",x.name);
|
105
106
|
scanf("%lf",&x.body.vision);
|
106
107
|
if (Push(&s, &x) == -1)
|
107
108
|
puts("\a エラー:プッシュに失敗しました。");
|
@@ -110,19 +111,19 @@
|
|
110
111
|
if (Pop(&s, &x) == -1)
|
111
112
|
puts("\a エラー:ポップに失敗しました。");
|
112
113
|
else
|
113
|
-
printf("ポップしたデータは%s %.1f %d です。\n", x.name
|
114
|
+
printf("ポップしたデータは%s %.1f %d です。\n", x.name,x.body.vision,x.body.height);
|
114
115
|
break;
|
115
116
|
case 3: /* ピーク */
|
116
117
|
if (Peek(&s, &x) == -1)
|
117
118
|
puts("\a エラー:ピークに失敗しました。");
|
118
119
|
else
|
119
|
-
printf("ピークしたデータは%s %.1f %d です。\n", x.name
|
120
|
+
printf("ピークしたデータは%s %.1f %d です。\n", x.name,x.body.vision,x.body.height);
|
120
121
|
break;
|
121
122
|
case 4: /* 表示 */
|
122
123
|
Print(&s);https://www.onlinegdb.com/fork/HyZW3BAi8#tab-stdin
|
123
124
|
break;
|
124
125
|
case 5://探索
|
125
|
-
scanf("%s",x.name
|
126
|
+
scanf("%s",x.name);
|
126
127
|
int search = Search(&s,&x);
|
127
128
|
if(search == 0){
|
128
129
|
puts("パターンは存在しません");
|
@@ -139,7 +140,7 @@
|
|
139
140
|
最初のメニューで1を選択し、push関数に111、aaa、111と入力して、スタックにプッシュします。
|
140
141
|
メニューで5を選択して、aと入力するとs->ptrが指してる0を返すプログラムを作りたいです。
|
141
142
|
<現在>
|
142
|
-
|
143
|
+
二回プッシュするとSegmentation fault (core dumped) と表示されます。
|
143
144
|
|
144
145
|
<質問>
|
145
|
-
|
146
|
+
プッシュ、サーチが出来るように実装したいです
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -142,4 +142,4 @@
|
|
142
142
|
今、最初のメニューで1を選択し、push関数に111と入力したあと、aaaと入力するとSegmentation fault と表示されてしまいます。
|
143
143
|
|
144
144
|
<質問>
|
145
|
-
<したいこと>で話したプログラムにするにはどうしたらよいでしょうか
|
145
|
+
<したいこと>で話したプログラムにするにはどうしたらよいでしょうか
|