回答編集履歴

1

追記

2020/06/10 08:39

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -347,3 +347,403 @@
347
347
  }
348
348
 
349
349
  ```
350
+
351
+
352
+
353
+ [追記] char* name 版
354
+
355
+ ```C
356
+
357
+ #define _CRT_SECURE_NO_WARNINGS
358
+
359
+ #include <stdio.h>
360
+
361
+ #include <string.h>
362
+
363
+ #include <stdlib.h>
364
+
365
+ #include <stdbool.h>
366
+
367
+
368
+
369
+ typedef struct {
370
+
371
+ double vision; /* 視力 */
372
+
373
+ int height; /* 身長 */
374
+
375
+ } Body;
376
+
377
+
378
+
379
+ /*--- 身体検査データ型 ---*/
380
+
381
+ typedef struct {
382
+
383
+ Body body; /* 身体データ型 ---*/
384
+
385
+ char* name; /* 氏名 */
386
+
387
+ } PhysCheck;
388
+
389
+
390
+
391
+ PhysCheck* PhysCheckCreate(const char* name, double vision, int height) {
392
+
393
+ PhysCheck* p = (PhysCheck*)malloc(sizeof(PhysCheck));
394
+
395
+ if (p != NULL) {
396
+
397
+ p->name = (char*)malloc(strlen(name) + 1);
398
+
399
+ if (p->name == NULL) {
400
+
401
+ free(p);
402
+
403
+ p = NULL;
404
+
405
+ }
406
+
407
+ else {
408
+
409
+ strcpy(p->name, name);
410
+
411
+ p->body.height = height;
412
+
413
+ p->body.vision = vision;
414
+
415
+ }
416
+
417
+ }
418
+
419
+ return p;
420
+
421
+ }
422
+
423
+
424
+
425
+ void PhysCheckDestroy(PhysCheck* x) {
426
+
427
+ free(x->name);
428
+
429
+ free(x);
430
+
431
+ }
432
+
433
+
434
+
435
+ bool PhysCheckEqual(const PhysCheck* a, const PhysCheck* b) {
436
+
437
+ return strcmp(a->name, b->name) == 0;
438
+
439
+ }
440
+
441
+
442
+
443
+ void PhysCheckPrint(const PhysCheck* x) {
444
+
445
+ printf("%s %f %d\n", x->name, x->body.vision, x->body.height);
446
+
447
+ }
448
+
449
+
450
+
451
+ typedef struct {
452
+
453
+ int max; /* スタックの容量 */
454
+
455
+ int ptr; /* スタックポインタ */
456
+
457
+ PhysCheck** stk; /* スタック本体*/
458
+
459
+ } PhysCheckStack;
460
+
461
+
462
+
463
+ int Search(const PhysCheckStack* s, const PhysCheck* x) {
464
+
465
+ int count = 0;
466
+
467
+ for (int i = 0; i < s->ptr; i++) {
468
+
469
+ if (PhysCheckEqual(s->stk[i], x)) {
470
+
471
+ PhysCheckPrint(s->stk[i]);
472
+
473
+ count++;
474
+
475
+ }
476
+
477
+ }
478
+
479
+ return count;
480
+
481
+ }
482
+
483
+
484
+
485
+ int SearchByName(const PhysCheckStack* s, const char* name) {
486
+
487
+ int count = 0;
488
+
489
+ for (int i = 0; i < s->ptr; i++) {
490
+
491
+ if (strcmp(s->stk[i]->name, name)) {
492
+
493
+ PhysCheckPrint(s->stk[i]);
494
+
495
+ count++;
496
+
497
+ }
498
+
499
+ }
500
+
501
+ return count;
502
+
503
+ }
504
+
505
+
506
+
507
+ /*--- スタックの初期化 ---*/
508
+
509
+ bool Initialize(PhysCheckStack* s, int max) {
510
+
511
+ s->stk = (PhysCheck**)calloc(max, sizeof(PhysCheck*));
512
+
513
+ if (s->stk == NULL) {
514
+
515
+ return false;
516
+
517
+ }
518
+
519
+ s->ptr = 0;
520
+
521
+ s->max = max;
522
+
523
+ return true;
524
+
525
+ }
526
+
527
+
528
+
529
+ /*--- スタックにデータをプッシュ ---*/
530
+
531
+ bool Push(PhysCheckStack* s, PhysCheck* x) {
532
+
533
+ if (s->ptr >= s->max) return false; /* スタック満杯 */
534
+
535
+ s->stk[s->ptr] = x;
536
+
537
+ s->ptr++;
538
+
539
+ return true;
540
+
541
+ }
542
+
543
+
544
+
545
+ /*--- スタックからデータをポップ ---*/
546
+
547
+ PhysCheck* Pop(PhysCheckStack* s) {
548
+
549
+ if (s->ptr <= 0) return NULL; /* スタックは空 */
550
+
551
+ s->ptr--;
552
+
553
+ return s->stk[s->ptr];
554
+
555
+ }
556
+
557
+
558
+
559
+ /*--- スタックからデータをピーク ---*/
560
+
561
+ PhysCheck* Peek(PhysCheckStack* s) {
562
+
563
+ if (s->ptr <= 0) return NULL;
564
+
565
+ return s->stk[s->ptr - 1];
566
+
567
+ }
568
+
569
+
570
+
571
+ /*--- スタックの容量 ---*/
572
+
573
+ int Capacity(const PhysCheckStack* s) {
574
+
575
+ return s->max;
576
+
577
+ }
578
+
579
+
580
+
581
+ /*--- スタックに積まれているデータ数 ---*/
582
+
583
+ int Size(const PhysCheckStack* s) {
584
+
585
+ return s->ptr;
586
+
587
+ }
588
+
589
+
590
+
591
+ /*--- スタックの全データの表示 ---*/
592
+
593
+ void Print(const PhysCheckStack* s) {
594
+
595
+ int i;
596
+
597
+
598
+
599
+ for (i = 0; i < s->ptr; i++)
600
+
601
+ PhysCheckPrint(s->stk[i]);
602
+
603
+ putchar('\n');
604
+
605
+ }
606
+
607
+
608
+
609
+ /*--- スタックの廃棄 ---*/
610
+
611
+ void Terminate(PhysCheckStack* s) {
612
+
613
+ while (Size(s) > 0) {
614
+
615
+ PhysCheckDestroy(Pop(s));
616
+
617
+ }
618
+
619
+ free(s->stk);
620
+
621
+ }
622
+
623
+
624
+
625
+ int main(void) {
626
+
627
+ PhysCheckStack s;
628
+
629
+ Initialize(&s, 10);
630
+
631
+ while (1) {
632
+
633
+ int menu;
634
+
635
+ double vision;
636
+
637
+ int height;
638
+
639
+ char name[50];
640
+
641
+ PhysCheck* x;
642
+
643
+ printf("現在のデータ数:%d/%d\n", Size(&s), Capacity(&s));
644
+
645
+ printf("(1) プッシュ (2) ポップ (3) ピーク (4) 表示 (5) 探索 (0) 終了:");
646
+
647
+ scanf("%d", &menu);
648
+
649
+ if (menu == 0) break;
650
+
651
+ switch (menu) {
652
+
653
+ case 1: /* プッシュ */
654
+
655
+ printf("データ(name vision height):");
656
+
657
+ scanf("%s", name);
658
+
659
+ scanf("%lf", &vision);
660
+
661
+ scanf("%d", &height);
662
+
663
+ x = PhysCheckCreate(name, vision, height);
664
+
665
+ if (x == NULL || !Push(&s, x))
666
+
667
+ puts("\a エラー:プッシュに失敗しました。");
668
+
669
+ break;
670
+
671
+ case 2: /* ポップ */
672
+
673
+ x = Pop(&s);
674
+
675
+ if (x == NULL) {
676
+
677
+ puts("\a エラー:ポップに失敗しました。");
678
+
679
+ }
680
+
681
+ else {
682
+
683
+ printf("ポップしたデータは");
684
+
685
+ PhysCheckPrint(x);
686
+
687
+ PhysCheckDestroy(x);
688
+
689
+ }
690
+
691
+ break;
692
+
693
+ case 3: /* ピーク */
694
+
695
+ x = Peek(&s);
696
+
697
+ if (x == NULL) {
698
+
699
+ puts("\a エラー:ピークに失敗しました。");
700
+
701
+ }
702
+
703
+ else {
704
+
705
+ printf("ピークしたデータは");
706
+
707
+ PhysCheckPrint(x);
708
+
709
+ }
710
+
711
+ break;
712
+
713
+ case 4: /* 表示 */
714
+
715
+ Print(&s);
716
+
717
+ break;
718
+
719
+ case 5://探索
720
+
721
+ printf("検索する名前:");
722
+
723
+ scanf("%s", name);
724
+
725
+ int search = SearchByName(&s, name);
726
+
727
+ if (search == 0) {
728
+
729
+ puts("パターンは存在しません");
730
+
731
+ }
732
+
733
+ else {
734
+
735
+ printf("%dパターンあります\n", search);
736
+
737
+ }
738
+
739
+ }
740
+
741
+ }
742
+
743
+ Terminate(&s);
744
+
745
+ return 0;
746
+
747
+ }
748
+
749
+ ```