-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
146 lines (125 loc) · 3.44 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
typedef struct {
double vision;
int height;
} Body;
typedef struct {
char *name;
Body body;
} PhysCheck;
typedef struct {
int max;
int ptr;
PhysCheck stk[MAX];
} PhysCheckStack;
int Initialize(PhysCheckStack *s, int max) {
s->ptr = 0;
s->max = max;
return 0;
}
int Push(PhysCheckStack *s, PhysCheck x) {
if (s->ptr >= s->max) return -1;
s->stk[s->ptr] = x;
s->ptr++;
return 0;
}
int Pop(PhysCheckStack *s, PhysCheck *x) {
if (s->ptr <= 0) return -1;
s->ptr--;
*x = s->stk[s->ptr];
return 0;
}
int Peek(PhysCheckStack *s, PhysCheck *x) {
if (s->ptr <= 0) return -1;
*x = s->stk[s->ptr - 1];
return 0;
}
int Capacity(const PhysCheckStack *s) {
return s->max;
}
int Size(const PhysCheckStack *s) {
return s->ptr;
}
void PrintOne(const PhysCheck *x) {
printf("%s %d %0.2lf", x->name, x->body.height, x->body.vision);
}
void Print(const PhysCheckStack *s) {
int i;
for (i = 0; i < s->ptr; i++) {
PrintOne(&s->stk[i]);
putchar('\n');
}
}
int Search(PhysCheckStack *s, PhysCheck *x) {
int i, counter = 0;
for (i = 0; i < s->ptr; i++) {
if (strstr(s->stk[i].name, x->name) != NULL) {
PrintOne(&s->stk[i]);
putchar('\n');
counter++;
}
}
return counter;
}
int main(void) {
PhysCheckStack s;
Initialize(&s, MAX);
while (1) {
int menu, search;
PhysCheck x;
printf("現在のデータ数:%d/%d\n", Size(&s), Capacity(&s));
printf("(1)プッシュ (2)ポップ (3)ピーク (4)表示 (5)探索 (0)終了:");
scanf("%d", &menu);
// ----------------①
if (menu == 0) break;
switch (menu) {
case 1:
x.name = calloc(20, sizeof(char));
printf("名前:");
scanf("%s", x.name);
printf("身長:");
scanf("%d", &x.body.height);
printf("視力:");
scanf("%lf", &x.body.vision);
if (Push(&s, x) == -1)
puts("\aエラー;プッシュに失敗しました。");
break;
case 2:
if (Pop(&s, &x) == -1)
puts("\aエラー:ポップに失敗しました。");
else {
printf("ポップしたデータは ");
PrintOne(&x);
printf(" です。\n");
}
free(x.name);
break;
case 3:
if (Peek(&s, &x) == -1)
puts("\aエラー:ピークに失敗しました。");
else {
printf("ピークしたデータは ");
PrintOne(&x);
printf(" です。\n");
}
break;
case 4:
Print(&s);
break;
case 5:
x.name = calloc(20, sizeof(char));
printf("パターン:");
scanf("%s", x.name);
if ((search = Search(&s, &x)) == 0)
puts("パターンは存在しません.");
else
printf("パターンは %d 個見つかりました.\n", search);
free(x.name);
break;
}
}
return 0;
}