質問編集履歴
1
ソースコードとヘッダファイルを全て記載しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,7 +22,59 @@
|
|
22
22
|
|
23
23
|
```C
|
24
24
|
|
25
|
+
#include <stdio.h>
|
26
|
+
|
27
|
+
#include "mystack.h"
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
int main(void)
|
32
|
+
|
33
|
+
{
|
34
|
+
|
35
|
+
int menu;
|
36
|
+
|
37
|
+
char *data[100];
|
38
|
+
|
39
|
+
struct student *seito[100];
|
40
|
+
|
41
|
+
char n[16];
|
42
|
+
|
43
|
+
int m,e;
|
44
|
+
|
45
|
+
int i=0;
|
46
|
+
|
47
|
+
CharStack s;
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
if (Initialize(&s, 64) == -1) {
|
52
|
+
|
53
|
+
puts("スタックの生成に失敗しました。");
|
54
|
+
|
55
|
+
return 1;
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
while (1) {
|
62
|
+
|
63
|
+
char x[48],x1[16],x2[16],x3[16];
|
64
|
+
|
65
|
+
printf("現在のデータ数:%d / %d\n", Size(&s), Capacity(&s));
|
66
|
+
|
67
|
+
printf("(1)プッシュ \n(2)ポップしてデータベースに格納 (3)データベースの値を表示 (4)スタック内表示 (0) 終了:");
|
68
|
+
|
69
|
+
scanf("%d", &menu);
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
if (menu == 0) break;
|
74
|
+
|
75
|
+
|
76
|
+
|
25
|
-
switch (menu) {
|
77
|
+
switch (menu) {
|
26
78
|
|
27
79
|
case 1: /*--- プッシュ ---*/
|
28
80
|
|
@@ -62,7 +114,7 @@
|
|
62
114
|
|
63
115
|
break;
|
64
116
|
|
65
|
-
/*---データベースの値を表示---*/
|
117
|
+
/*---データベースの値を表示---*/
|
66
118
|
|
67
119
|
case 3:
|
68
120
|
|
@@ -76,76 +128,228 @@
|
|
76
128
|
|
77
129
|
}
|
78
130
|
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
Terminate(&s);
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
return 0;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
### 使用したヘッダファイル内の関数
|
148
|
+
|
149
|
+
```C
|
150
|
+
|
151
|
+
#ifndef MYSTACK_H
|
152
|
+
|
153
|
+
#define MYSTACK_H
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
#include <stdio.h>
|
158
|
+
|
159
|
+
#include <stdlib.h>
|
160
|
+
|
161
|
+
#include <string.h>
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
typedef struct
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
int max; //スタックのサイズ
|
170
|
+
|
171
|
+
int ptr; //スタックのポインタ
|
172
|
+
|
173
|
+
char **stk; //スタック(の先頭要素へのポインタ)
|
174
|
+
|
175
|
+
} CharStack;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
struct student
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
char name[12];
|
184
|
+
|
185
|
+
int math;
|
186
|
+
|
187
|
+
int eng;
|
188
|
+
|
189
|
+
};
|
190
|
+
|
191
|
+
/*--- スタックの初期化 ---*/
|
192
|
+
|
193
|
+
int Initialize(CharStack *s, int max)
|
194
|
+
|
195
|
+
{
|
196
|
+
|
197
|
+
s->ptr = 0;
|
198
|
+
|
199
|
+
if ((s->stk = calloc(max, sizeof(char *))) == NULL) {
|
200
|
+
|
201
|
+
s->max = 0; /* 配列の生成に失敗 */
|
202
|
+
|
203
|
+
return -1;
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
s->max = max;
|
208
|
+
|
209
|
+
return 0;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
/*--- スタックの後始末 ---*/
|
214
|
+
|
215
|
+
void Terminate(CharStack *s)
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
if (s->stk != NULL)
|
220
|
+
|
221
|
+
free(s->stk); /* 配列を破棄 */
|
222
|
+
|
223
|
+
s->max = s->ptr = 0;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
/*--サイズ確認--*/
|
228
|
+
|
229
|
+
int Size(const CharStack *s)
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
return s->ptr;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
/*--- スタックにデータをプッシュ ---*/
|
238
|
+
|
239
|
+
int Push(CharStack *s, char *x)
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
if (s->ptr >= s->max) /* スタックは満杯 */
|
244
|
+
|
245
|
+
return -1;
|
246
|
+
|
247
|
+
s->stk[s->ptr++] = x;
|
248
|
+
|
249
|
+
return 0;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
/*--- スタックからデータをポップ ---*/
|
256
|
+
|
257
|
+
int Pop(CharStack *s, char **x)
|
258
|
+
|
259
|
+
{
|
260
|
+
|
261
|
+
if (s->ptr <= 0) /* スタックは空 */
|
262
|
+
|
263
|
+
return -1;
|
264
|
+
|
265
|
+
*x = s->stk[--s->ptr];
|
266
|
+
|
267
|
+
return 0;
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
/*--- スタックの容量 ---*/
|
272
|
+
|
273
|
+
int Capacity(const CharStack *s)
|
274
|
+
|
275
|
+
{
|
276
|
+
|
277
|
+
return s->max;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
/*--- 全データの表示 ---*/
|
282
|
+
|
283
|
+
void Print(const CharStack *s)
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
int i;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
for (i = 0; i < s->ptr; i++) /* 底→頂上に走査 */
|
292
|
+
|
293
|
+
printf("%s", s->stk[i]);
|
294
|
+
|
295
|
+
putchar('\n');
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
void print_all_student(struct student *seito[],int ninzu)
|
302
|
+
|
303
|
+
{
|
304
|
+
|
305
|
+
int i;
|
306
|
+
|
307
|
+
for(i=0;i<ninzu;i++)
|
308
|
+
|
309
|
+
{
|
310
|
+
|
311
|
+
printf("Name:%s\nMath:%d\nEng:%d\n",seito[i]->name,seito[i]->math,seito[i]->eng);
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
//struct student構造体オブジェクトのメンバに値を格納する関数
|
318
|
+
|
319
|
+
struct student *set_student(char *n, int m, int e)
|
320
|
+
|
321
|
+
{
|
322
|
+
|
323
|
+
struct student *ps = NULL;
|
324
|
+
|
325
|
+
ps = (struct student*)malloc(1 * sizeof(struct student));
|
326
|
+
|
327
|
+
if(ps == NULL)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
puts("記憶域の確保に失敗しました");
|
332
|
+
|
333
|
+
return NULL;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
strcpy(ps->name, n);
|
338
|
+
|
339
|
+
ps->math = m;
|
340
|
+
|
341
|
+
ps->eng = e;
|
342
|
+
|
343
|
+
return ps;
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
#endif
|
350
|
+
|
79
351
|
```
|
80
352
|
|
81
|
-
### 使用したヘッダファイル内の関数
|
82
|
-
|
83
|
-
```C
|
84
|
-
|
85
|
-
//宣言
|
86
|
-
|
87
|
-
typedef struct
|
88
|
-
|
89
|
-
{
|
90
|
-
|
91
|
-
int max; //スタックのサイズ
|
92
|
-
|
93
|
-
int ptr; //スタックのポインタ
|
94
|
-
|
95
|
-
char **stk; //スタック(の先頭要素へのポインタ)
|
96
|
-
|
97
|
-
} CharStack;
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
struct student
|
102
|
-
|
103
|
-
{
|
104
|
-
|
105
|
-
char name[12];
|
106
|
-
|
107
|
-
int math;
|
108
|
-
|
109
|
-
int eng;
|
110
|
-
|
111
|
-
};
|
112
|
-
|
113
|
-
/*--- スタックにデータをプッシュ ---*/
|
114
|
-
|
115
|
-
int Push(CharStack *s, char *x)
|
116
|
-
|
117
|
-
{
|
118
|
-
|
119
|
-
if (s->ptr >= s->max) /* スタックは満杯 */
|
120
|
-
|
121
|
-
return -1;
|
122
|
-
|
123
|
-
s->stk[s->ptr++] = x;
|
124
|
-
|
125
|
-
return 0;
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
/*--- スタックからデータをポップ ---*/
|
132
|
-
|
133
|
-
int Pop(CharStack *s, char **x)
|
134
|
-
|
135
|
-
{
|
136
|
-
|
137
|
-
if (s->ptr <= 0) /* スタックは空 */
|
138
|
-
|
139
|
-
return -1;
|
140
|
-
|
141
|
-
*x = s->stk[--s->ptr];
|
142
|
-
|
143
|
-
return 0;
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
```
|
148
|
-
|
149
353
|
### 試したこと
|
150
354
|
|
151
355
|
|