回答編集履歴
1
追記
answer
CHANGED
@@ -172,4 +172,204 @@
|
|
172
172
|
Terminate(&s);
|
173
173
|
return 0;
|
174
174
|
}
|
175
|
+
```
|
176
|
+
|
177
|
+
[追記] char* name 版
|
178
|
+
```C
|
179
|
+
#define _CRT_SECURE_NO_WARNINGS
|
180
|
+
#include <stdio.h>
|
181
|
+
#include <string.h>
|
182
|
+
#include <stdlib.h>
|
183
|
+
#include <stdbool.h>
|
184
|
+
|
185
|
+
typedef struct {
|
186
|
+
double vision; /* 視力 */
|
187
|
+
int height; /* 身長 */
|
188
|
+
} Body;
|
189
|
+
|
190
|
+
/*--- 身体検査データ型 ---*/
|
191
|
+
typedef struct {
|
192
|
+
Body body; /* 身体データ型 ---*/
|
193
|
+
char* name; /* 氏名 */
|
194
|
+
} PhysCheck;
|
195
|
+
|
196
|
+
PhysCheck* PhysCheckCreate(const char* name, double vision, int height) {
|
197
|
+
PhysCheck* p = (PhysCheck*)malloc(sizeof(PhysCheck));
|
198
|
+
if (p != NULL) {
|
199
|
+
p->name = (char*)malloc(strlen(name) + 1);
|
200
|
+
if (p->name == NULL) {
|
201
|
+
free(p);
|
202
|
+
p = NULL;
|
203
|
+
}
|
204
|
+
else {
|
205
|
+
strcpy(p->name, name);
|
206
|
+
p->body.height = height;
|
207
|
+
p->body.vision = vision;
|
208
|
+
}
|
209
|
+
}
|
210
|
+
return p;
|
211
|
+
}
|
212
|
+
|
213
|
+
void PhysCheckDestroy(PhysCheck* x) {
|
214
|
+
free(x->name);
|
215
|
+
free(x);
|
216
|
+
}
|
217
|
+
|
218
|
+
bool PhysCheckEqual(const PhysCheck* a, const PhysCheck* b) {
|
219
|
+
return strcmp(a->name, b->name) == 0;
|
220
|
+
}
|
221
|
+
|
222
|
+
void PhysCheckPrint(const PhysCheck* x) {
|
223
|
+
printf("%s %f %d\n", x->name, x->body.vision, x->body.height);
|
224
|
+
}
|
225
|
+
|
226
|
+
typedef struct {
|
227
|
+
int max; /* スタックの容量 */
|
228
|
+
int ptr; /* スタックポインタ */
|
229
|
+
PhysCheck** stk; /* スタック本体*/
|
230
|
+
} PhysCheckStack;
|
231
|
+
|
232
|
+
int Search(const PhysCheckStack* s, const PhysCheck* x) {
|
233
|
+
int count = 0;
|
234
|
+
for (int i = 0; i < s->ptr; i++) {
|
235
|
+
if (PhysCheckEqual(s->stk[i], x)) {
|
236
|
+
PhysCheckPrint(s->stk[i]);
|
237
|
+
count++;
|
238
|
+
}
|
239
|
+
}
|
240
|
+
return count;
|
241
|
+
}
|
242
|
+
|
243
|
+
int SearchByName(const PhysCheckStack* s, const char* name) {
|
244
|
+
int count = 0;
|
245
|
+
for (int i = 0; i < s->ptr; i++) {
|
246
|
+
if (strcmp(s->stk[i]->name, name)) {
|
247
|
+
PhysCheckPrint(s->stk[i]);
|
248
|
+
count++;
|
249
|
+
}
|
250
|
+
}
|
251
|
+
return count;
|
252
|
+
}
|
253
|
+
|
254
|
+
/*--- スタックの初期化 ---*/
|
255
|
+
bool Initialize(PhysCheckStack* s, int max) {
|
256
|
+
s->stk = (PhysCheck**)calloc(max, sizeof(PhysCheck*));
|
257
|
+
if (s->stk == NULL) {
|
258
|
+
return false;
|
259
|
+
}
|
260
|
+
s->ptr = 0;
|
261
|
+
s->max = max;
|
262
|
+
return true;
|
263
|
+
}
|
264
|
+
|
265
|
+
/*--- スタックにデータをプッシュ ---*/
|
266
|
+
bool Push(PhysCheckStack* s, PhysCheck* x) {
|
267
|
+
if (s->ptr >= s->max) return false; /* スタック満杯 */
|
268
|
+
s->stk[s->ptr] = x;
|
269
|
+
s->ptr++;
|
270
|
+
return true;
|
271
|
+
}
|
272
|
+
|
273
|
+
/*--- スタックからデータをポップ ---*/
|
274
|
+
PhysCheck* Pop(PhysCheckStack* s) {
|
275
|
+
if (s->ptr <= 0) return NULL; /* スタックは空 */
|
276
|
+
s->ptr--;
|
277
|
+
return s->stk[s->ptr];
|
278
|
+
}
|
279
|
+
|
280
|
+
/*--- スタックからデータをピーク ---*/
|
281
|
+
PhysCheck* Peek(PhysCheckStack* s) {
|
282
|
+
if (s->ptr <= 0) return NULL;
|
283
|
+
return s->stk[s->ptr - 1];
|
284
|
+
}
|
285
|
+
|
286
|
+
/*--- スタックの容量 ---*/
|
287
|
+
int Capacity(const PhysCheckStack* s) {
|
288
|
+
return s->max;
|
289
|
+
}
|
290
|
+
|
291
|
+
/*--- スタックに積まれているデータ数 ---*/
|
292
|
+
int Size(const PhysCheckStack* s) {
|
293
|
+
return s->ptr;
|
294
|
+
}
|
295
|
+
|
296
|
+
/*--- スタックの全データの表示 ---*/
|
297
|
+
void Print(const PhysCheckStack* s) {
|
298
|
+
int i;
|
299
|
+
|
300
|
+
for (i = 0; i < s->ptr; i++)
|
301
|
+
PhysCheckPrint(s->stk[i]);
|
302
|
+
putchar('\n');
|
303
|
+
}
|
304
|
+
|
305
|
+
/*--- スタックの廃棄 ---*/
|
306
|
+
void Terminate(PhysCheckStack* s) {
|
307
|
+
while (Size(s) > 0) {
|
308
|
+
PhysCheckDestroy(Pop(s));
|
309
|
+
}
|
310
|
+
free(s->stk);
|
311
|
+
}
|
312
|
+
|
313
|
+
int main(void) {
|
314
|
+
PhysCheckStack s;
|
315
|
+
Initialize(&s, 10);
|
316
|
+
while (1) {
|
317
|
+
int menu;
|
318
|
+
double vision;
|
319
|
+
int height;
|
320
|
+
char name[50];
|
321
|
+
PhysCheck* x;
|
322
|
+
printf("現在のデータ数:%d/%d\n", Size(&s), Capacity(&s));
|
323
|
+
printf("(1) プッシュ (2) ポップ (3) ピーク (4) 表示 (5) 探索 (0) 終了:");
|
324
|
+
scanf("%d", &menu);
|
325
|
+
if (menu == 0) break;
|
326
|
+
switch (menu) {
|
327
|
+
case 1: /* プッシュ */
|
328
|
+
printf("データ(name vision height):");
|
329
|
+
scanf("%s", name);
|
330
|
+
scanf("%lf", &vision);
|
331
|
+
scanf("%d", &height);
|
332
|
+
x = PhysCheckCreate(name, vision, height);
|
333
|
+
if (x == NULL || !Push(&s, x))
|
334
|
+
puts("\a エラー:プッシュに失敗しました。");
|
335
|
+
break;
|
336
|
+
case 2: /* ポップ */
|
337
|
+
x = Pop(&s);
|
338
|
+
if (x == NULL) {
|
339
|
+
puts("\a エラー:ポップに失敗しました。");
|
340
|
+
}
|
341
|
+
else {
|
342
|
+
printf("ポップしたデータは");
|
343
|
+
PhysCheckPrint(x);
|
344
|
+
PhysCheckDestroy(x);
|
345
|
+
}
|
346
|
+
break;
|
347
|
+
case 3: /* ピーク */
|
348
|
+
x = Peek(&s);
|
349
|
+
if (x == NULL) {
|
350
|
+
puts("\a エラー:ピークに失敗しました。");
|
351
|
+
}
|
352
|
+
else {
|
353
|
+
printf("ピークしたデータは");
|
354
|
+
PhysCheckPrint(x);
|
355
|
+
}
|
356
|
+
break;
|
357
|
+
case 4: /* 表示 */
|
358
|
+
Print(&s);
|
359
|
+
break;
|
360
|
+
case 5://探索
|
361
|
+
printf("検索する名前:");
|
362
|
+
scanf("%s", name);
|
363
|
+
int search = SearchByName(&s, name);
|
364
|
+
if (search == 0) {
|
365
|
+
puts("パターンは存在しません");
|
366
|
+
}
|
367
|
+
else {
|
368
|
+
printf("%dパターンあります\n", search);
|
369
|
+
}
|
370
|
+
}
|
371
|
+
}
|
372
|
+
Terminate(&s);
|
373
|
+
return 0;
|
374
|
+
}
|
175
375
|
```
|