質問編集履歴

5

画像削除

2018/01/22 01:59

投稿

ping_100
ping_100

スコア7

test CHANGED
File without changes
test CHANGED
@@ -407,5 +407,3 @@
407
407
  c
408
408
 
409
409
  エクリプス最新
410
-
411
- ![イメージ説明](427464a7f81ad7e98fcb8f9406b34e39.png)

4

誤字

2018/01/22 01:59

投稿

ping_100
ping_100

スコア7

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- 学籍番号と氏名を入力し名簿を作るプログラムですが、どこが間違っているのかわかりません。main文はmeibo20を読んでいるだけです。
3
+ 出席番号と氏名を入力し名簿を作るプログラムですが、どこが間違っているのかわかりません。main文はmeibo20を読んでいるだけです。
4
4
 
5
5
  ###発生している問題・エラーメッセージ
6
6
 
@@ -194,7 +194,7 @@
194
194
 
195
195
  Person *p=(Person *)malloc(sizeof(Person));
196
196
 
197
- printf("学籍番号(4桁)=");
197
+ printf("番号=");
198
198
 
199
199
  fflush(stdout);
200
200
 
@@ -254,7 +254,7 @@
254
254
 
255
255
  char command[2];
256
256
 
257
- printf("コマンドを入力してください(I:挿入、D:削除、P:印刷、X:終了)=");
257
+ printf("コマンドを入力してください=");
258
258
 
259
259
  fflush(stdout);
260
260
 

3

画像追加

2018/01/22 01:59

投稿

ping_100
ping_100

スコア7

test CHANGED
File without changes
test CHANGED
@@ -407,3 +407,5 @@
407
407
  c
408
408
 
409
409
  エクリプス最新
410
+
411
+ ![イメージ説明](427464a7f81ad7e98fcb8f9406b34e39.png)

2

誤字

2018/01/21 17:08

投稿

ping_100
ping_100

スコア7

test CHANGED
File without changes
test CHANGED
@@ -404,6 +404,6 @@
404
404
 
405
405
  ###補足情報(言語/FW/ツール等のバージョンなど)
406
406
 
407
- c++
407
+ c
408
408
 
409
409
  エクリプス最新

1

書式の改善

2018/01/21 16:57

投稿

ping_100
ping_100

スコア7

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
  ###該当のソースコード
12
12
 
13
+ ```
14
+
15
+
16
+
13
17
  #include <stdio.h>
14
18
 
15
19
  #include <string.h>
@@ -30,6 +34,10 @@
30
34
 
31
35
  } Person;
32
36
 
37
+ ```
38
+
39
+ ```
40
+
33
41
  char *my_gets6(){
34
42
 
35
43
  static int n;
@@ -90,7 +98,79 @@
90
98
 
91
99
  }
92
100
 
93
-
101
+ ```
102
+
103
+ ```
104
+
105
+ char *my_fgets6(FILE *fp){
106
+
107
+ static int n;
108
+
109
+ static char *s=NULL;
110
+
111
+ if(s==NULL){
112
+
113
+ n = 10;
114
+
115
+ s = (char *)malloc(n);
116
+
117
+ }
118
+
119
+ int i;
120
+
121
+ for(i=0;i<n;i++){
122
+
123
+ int c = fgetc(stdin);
124
+
125
+ if(c == EOF){
126
+
127
+ return NULL;
128
+
129
+ }
130
+
131
+ if(c == '\n'){
132
+
133
+ s[i] = '\0';
134
+
135
+ break;
136
+
137
+ }
138
+
139
+ if(i == n - 1){
140
+
141
+ s[i] ='\0';
142
+
143
+ n *= 2;
144
+
145
+ char *s2 = (char *)malloc(n);
146
+
147
+ strcpy(s2,s);
148
+
149
+ free(s);
150
+
151
+ s = s2;
152
+
153
+ i = strlen(s);
154
+
155
+ }
156
+
157
+ s[i] = c;
158
+
159
+ }
160
+
161
+ char *s3 = (char *)malloc(strlen(s)+1);
162
+
163
+ strcpy(s3,s);
164
+
165
+ free(s);
166
+
167
+ return s3;
168
+
169
+ }
170
+
171
+ ```
172
+
173
+ ```
94
174
 
95
175
  static char *get_file_name(){
96
176
 
@@ -110,311 +190,211 @@
110
190
 
111
191
  }
112
192
 
113
-
193
+ static Person *get_person(){
194
+
114
-
195
+ Person *p=(Person *)malloc(sizeof(Person));
196
+
115
-
197
+ printf("学籍番号(4桁)=");
198
+
116
-
199
+ fflush(stdout);
200
+
201
+ p->id=my_gets6();
202
+
203
+ if(strlen(p->id)==0){
204
+
205
+ free(p);
206
+
207
+ return NULL;
208
+
209
+ }
210
+
211
+ printf("名前=");
212
+
213
+ fflush(stdout);
214
+
215
+ p->name =my_gets6();
216
+
217
+ return p;
218
+
219
+ }
220
+
221
+
222
+
117
- void my_gets1(char *s, int n){
223
+ static void put_persons(Person **ps, int n){
118
224
 
119
225
  int i;
120
226
 
121
- for(i = 0; i < n; i++){
227
+ for (i = 0; i < n; i++)
228
+
122
-
229
+ printf("%s %s\n", ps[i]->id, ps[i]->name);
230
+
231
+ }
232
+
233
+ ```
234
+
235
+ ```
236
+
237
+ void meibo20(void) {
238
+
239
+ Person **array = NULL;
240
+
241
+ int array_size = 10;
242
+
243
+ int next_idx = 0;
244
+
245
+ char *file_name;
246
+
247
+ FILE *fp;
248
+
249
+
250
+
251
+ array = (struct person **) malloc(sizeof(Person *) * array_size);
252
+
253
+ while (1) {
254
+
255
+ char command[2];
256
+
257
+ printf("コマンドを入力してください(I:挿入、D:削除、P:印刷、X:終了)=");
258
+
259
+ fflush(stdout);
260
+
261
+ my_gets1(command, 2);
262
+
123
- s[i] = getchar();
263
+ switch (command[0]) {
124
-
264
+
125
- if(s[i] == '\n'){
265
+ Person *workp;
126
-
266
+
127
- s[i] = '\0';
267
+ case 'I': ;
268
+
269
+ workp = get_person();
270
+
271
+ if (workp == NULL) {
272
+
273
+ break;
274
+
275
+ }
276
+
277
+ int i;
278
+
279
+ for (i = next_idx - 1; i >= 0; i--) {
280
+
281
+ if (strcmp(array[i]->id, workp->id) < 0) {
282
+
283
+ break;
284
+
285
+ }
286
+
287
+ array[i + 1] = array[i];
288
+
289
+ }
290
+
291
+ array[i + 1] = workp;
292
+
293
+ next_idx++;
128
294
 
129
295
  break;
130
296
 
131
- }
132
-
133
- }
134
-
135
- if(i == n){
136
-
137
- printf("入力文字列が%dバイトを超えたため、以降を読み飛ばします。\n", n-1);
138
-
139
- fflush(stdout);
140
-
141
- s[n-1] = '\0';
142
-
143
- while(getchar() != '\n');
144
-
145
- }
146
-
147
- }
148
-
149
-
150
-
151
- char *my_fgets6(FILE *fp){
152
-
153
- static int n;
154
-
155
- static char *s=NULL;
156
-
157
- if(s==NULL){
158
-
159
- n = 10;
160
-
161
- s = (char *)malloc(n);
162
-
163
- }
164
-
165
- int i;
166
-
167
- for(i=0;i<n;i++){
168
-
169
- int c = fgetc(stdin);
170
-
171
- if(c == EOF){
172
-
173
- return NULL;
174
-
175
- }
176
-
177
- if(c == '\n'){
178
-
179
- s[i] = '\0';
297
+
298
+
299
+ case 'X':
300
+
301
+ printf("処理を終了します。 \n");
302
+
303
+ return;
304
+
305
+
306
+
307
+ case 'W':
308
+
309
+ file_name = get_file_name();
310
+
311
+ fp = fopen(file_name, "w");
312
+
313
+ if(fp == NULL){
314
+
315
+ printf("ファイル %s を作成できませんでした。\n",file_name);
316
+
317
+ continue;
318
+
319
+ }
320
+
321
+ for (i = 0; i < next_idx; i++) {
322
+
323
+ fprintf(fp, "%s\n", array[i]->id);
324
+
325
+ fprintf(fp, "%s\n", array[i]->name);
326
+
327
+ }
328
+
329
+ fclose(fp);
330
+
331
+ break;
332
+
333
+ case 'R':
334
+
335
+ file_name = get_file_name();
336
+
337
+ fp = fopen(file_name, "r");
338
+
339
+ if(fp == NULL){
340
+
341
+ printf("ファイル %s が存在しません。\n", file_name);
342
+
343
+ continue;
344
+
345
+ }
346
+
347
+ while (1) {
348
+
349
+ workp = (Person *) malloc(sizeof(Person));
350
+
351
+ if((workp->id = my_fgets6(fp)) == NULL){
352
+
353
+ free(workp);
354
+
355
+ break;
356
+
357
+ }
358
+
359
+ workp->name = my_fgets6(fp);
360
+
361
+ for (i = next_idx - 1; i >= 0; i--) {
362
+
363
+ if (strcmp(array[i]->id, workp->id) < 0) {
364
+
365
+ break;
366
+
367
+ }
368
+
369
+ array[i + 1] = array[i];
370
+
371
+ }
372
+
373
+ array[i + 1] = workp;
374
+
375
+ next_idx++;
180
376
 
181
377
  break;
182
378
 
183
379
  }
184
380
 
185
- if(i == n - 1){
186
-
187
- s[i] ='\0';
188
-
189
- n *= 2;
190
-
191
- char *s2 = (char *)malloc(n);
192
-
193
- strcpy(s2,s);
194
-
195
- free(s);
196
-
197
- s = s2;
198
-
199
- i = strlen(s);
200
-
201
- }
202
-
203
- s[i] = c;
204
-
205
- }
206
-
207
- char *s3 = (char *)malloc(strlen(s)+1);
208
-
209
- strcpy(s3,s);
210
-
211
- free(s);
212
-
213
- return s3;
214
-
215
- }
216
-
217
-
218
-
219
- static Person *get_person(){
220
-
221
- Person *p=(Person *)malloc(sizeof(Person));
222
-
223
- printf("学籍番号(4桁)=");
224
-
225
- fflush(stdout);
226
-
227
- p->id=my_gets6();
228
-
229
- if(strlen(p->id)==0){
230
-
231
- free(p);
232
-
233
- return NULL;
234
-
235
- }
236
-
237
- printf("名前=");
238
-
239
- fflush(stdout);
240
-
241
- p->name =my_gets6();
242
-
243
- return p;
244
-
245
- }
246
-
247
-
248
-
249
- static void put_persons(Person **ps, int n){
250
-
251
- int i;
252
-
253
- for (i = 0; i < n; i++)
254
-
255
- printf("%s %s\n", ps[i]->id, ps[i]->name);
256
-
257
- }
258
-
259
- void meibo20(void) {
260
-
261
- Person **array = NULL;
262
-
263
- int array_size = 10;
264
-
265
- int next_idx = 0;
266
-
267
- char *file_name;
268
-
269
- FILE *fp;
270
-
271
-
272
-
273
- array = (struct person **) malloc(sizeof(Person *) * array_size);
274
-
275
- while (1) {
276
-
277
- char command[2];
278
-
279
- printf("コマンドを入力してください(I:挿入、D:削除、P:印刷、X:終了)=");
280
-
281
- fflush(stdout);
282
-
283
- my_gets1(command, 2);
284
-
285
- switch (command[0]) {
286
-
287
- Person *workp;
288
-
289
- case 'I': ;
290
-
291
- workp = get_person();
292
-
293
- if (workp == NULL) {
294
-
295
- break;
381
+ break;
296
-
297
- }
382
+
298
-
383
+
384
+
299
- int i;
385
+ default:
300
-
386
+
301
- for (i = next_idx - 1; i >= 0; i--) {
387
+ printf("不正なコマンドです。 \n");
302
-
303
- if (strcmp(array[i]->id, workp->id) < 0) {
304
-
305
- break;
306
-
307
- }
308
-
309
- array[i + 1] = array[i];
310
-
311
- }
312
-
313
- array[i + 1] = workp;
314
-
315
- next_idx++;
316
388
 
317
389
  break;
318
390
 
319
-
320
-
321
- case 'X':
322
-
323
- printf("処理を終了します。 \n");
324
-
325
- return;
326
-
327
-
328
-
329
- case 'W':
330
-
331
- file_name = get_file_name();
332
-
333
- fp = fopen(file_name, "w");
334
-
335
- if(fp == NULL){
336
-
337
- printf("ファイル %s を作成できませんでした。\n",file_name);
338
-
339
- continue;
340
-
341
- }
391
+ }
342
-
343
- for (i = 0; i < next_idx; i++) {
392
+
344
-
345
- fprintf(fp, "%s\n", array[i]->id);
346
-
347
- fprintf(fp, "%s\n", array[i]->name);
348
-
349
- }
393
+ }
350
-
351
- fclose(fp);
394
+
352
-
353
- break;
354
-
355
- case 'R':
356
-
357
- file_name = get_file_name();
358
-
359
- fp = fopen(file_name, "r");
360
-
361
- if(fp == NULL){
362
-
363
- printf("ファイル %s が存在しません。\n", file_name);
364
-
365
- continue;
366
-
367
- }
395
+ }
368
-
369
- while (1) {
396
+
370
-
371
- workp = (Person *) malloc(sizeof(Person));
372
-
373
- if((workp->id = my_fgets6(fp)) == NULL){
374
-
375
- free(workp);
376
-
377
- break;
397
+ ```
378
-
379
- }
380
-
381
- workp->name = my_fgets6(fp);
382
-
383
- for (i = next_idx - 1; i >= 0; i--) {
384
-
385
- if (strcmp(array[i]->id, workp->id) < 0) {
386
-
387
- break;
388
-
389
- }
390
-
391
- array[i + 1] = array[i];
392
-
393
- }
394
-
395
- array[i + 1] = workp;
396
-
397
- next_idx++;
398
-
399
- break;
400
-
401
- }
402
-
403
- break;
404
-
405
-
406
-
407
- default:
408
-
409
- printf("不正なコマンドです。 \n");
410
-
411
- break;
412
-
413
- }
414
-
415
- }
416
-
417
- }
418
398
 
419
399
  ###試したこと
420
400