回答編集履歴
2
追記
test
CHANGED
@@ -37,3 +37,229 @@
|
|
37
37
|
|
38
38
|
|
39
39
|
また、質問のタグにはご注意ください。CとC#は別の言語です。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
おまけ
|
44
|
+
|
45
|
+
---
|
46
|
+
|
47
|
+
個人的には、こんな感じで書きますね。
|
48
|
+
|
49
|
+
```C
|
50
|
+
|
51
|
+
#include <stdbool.h>
|
52
|
+
|
53
|
+
#include <stdio.h>
|
54
|
+
|
55
|
+
#include <stdlib.h>
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
#define NUM_OF_STUDENTS 10
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
typedef struct {
|
64
|
+
|
65
|
+
int num, phys, chem;
|
66
|
+
|
67
|
+
} Student;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
typedef enum {
|
72
|
+
|
73
|
+
PHYSICS = 1, CHEMICAL, ALL = -1,
|
74
|
+
|
75
|
+
} Subjects;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
int fscan_student(FILE *fp, Student *dst) {
|
80
|
+
|
81
|
+
return fscanf(
|
82
|
+
|
83
|
+
fp, "%d %d %d", &dst->num, &dst->phys, &dst->chem
|
84
|
+
|
85
|
+
);
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
int fprint_student(FILE *fp, const Student *src, const Subjects subject) {
|
90
|
+
|
91
|
+
switch(subject) {
|
92
|
+
|
93
|
+
case PHYSICS:
|
94
|
+
|
95
|
+
return fprintf(
|
96
|
+
|
97
|
+
fp, "%d %d\n", src->num, src->phys
|
98
|
+
|
99
|
+
);
|
100
|
+
|
101
|
+
case CHEMICAL:
|
102
|
+
|
103
|
+
return fprintf(
|
104
|
+
|
105
|
+
fp, "%d %d\n", src->num, src->chem
|
106
|
+
|
107
|
+
);
|
108
|
+
|
109
|
+
default:
|
110
|
+
|
111
|
+
return fprintf(
|
112
|
+
|
113
|
+
fp, "%d %d %d\n", src->num, src->phys, src->chem
|
114
|
+
|
115
|
+
);
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
bool in_range_student(
|
122
|
+
|
123
|
+
const Student *src, Subjects subject, int lower, int upper) {
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
switch(subject) {
|
128
|
+
|
129
|
+
case PHYSICS:
|
130
|
+
|
131
|
+
return lower <= src->phys && src->phys <= upper;
|
132
|
+
|
133
|
+
case CHEMICAL:
|
134
|
+
|
135
|
+
return lower <= src->chem && src->chem <= upper;
|
136
|
+
|
137
|
+
default:
|
138
|
+
|
139
|
+
return false;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
//
|
148
|
+
|
149
|
+
//
|
150
|
+
|
151
|
+
//
|
152
|
+
|
153
|
+
int main(int argc, char **argv) {
|
154
|
+
|
155
|
+
//
|
156
|
+
|
157
|
+
// Prepare Files
|
158
|
+
|
159
|
+
if(argc < 3) {
|
160
|
+
|
161
|
+
printf("USAGE: exam.exe src_file_name dst_file_name\n");
|
162
|
+
|
163
|
+
return EXIT_FAILURE;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
char *file_in = argv[1];
|
168
|
+
|
169
|
+
char *file_out = argv[2];
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
FILE *fp_in = fopen(file_in, "r");
|
174
|
+
|
175
|
+
if(fp_in == NULL) {
|
176
|
+
|
177
|
+
printf("ファイルが見つかりません---%s\n", file_in);
|
178
|
+
|
179
|
+
return EXIT_FAILURE;
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
FILE *fp_out = fopen(file_out, "w");
|
186
|
+
|
187
|
+
if(fp_out == NULL) {
|
188
|
+
|
189
|
+
fclose(fp_in);
|
190
|
+
|
191
|
+
printf("ファイルが作成できません---%s\n", file_out);
|
192
|
+
|
193
|
+
exit(EXIT_FAILURE);
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
//
|
200
|
+
|
201
|
+
// Read Data
|
202
|
+
|
203
|
+
Student students[NUM_OF_STUDENTS];
|
204
|
+
|
205
|
+
for(int i = 0; i < NUM_OF_STUDENTS; ++i) {
|
206
|
+
|
207
|
+
fscan_student(fp_in, &students[i]);
|
208
|
+
|
209
|
+
fprint_student(stdout, &students[i], ALL); // for debug
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//
|
216
|
+
|
217
|
+
// Select Students
|
218
|
+
|
219
|
+
int subject;
|
220
|
+
|
221
|
+
printf("subject(phys: 1, chem: 2) >>> ");
|
222
|
+
|
223
|
+
if(scanf("%d", &subject) != 1) return EXIT_FAILURE;
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
int upper, lower;
|
228
|
+
|
229
|
+
printf("upper limit: ");
|
230
|
+
|
231
|
+
if(scanf("%d", &upper) != 1) return EXIT_FAILURE;
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
printf("lower limit: ");
|
236
|
+
|
237
|
+
if(scanf("%d", &lower) != 1) return EXIT_FAILURE;
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
for(int i = 0; i < NUM_OF_STUDENTS; ++i) {
|
242
|
+
|
243
|
+
if(in_range_student(&students[i], subject, lower, upper)) {
|
244
|
+
|
245
|
+
fprint_student(fp_out, &students[i], subject);
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
//
|
254
|
+
|
255
|
+
// Termination
|
256
|
+
|
257
|
+
fclose(fp_in);
|
258
|
+
|
259
|
+
fclose(fp_out);
|
260
|
+
|
261
|
+
return EXIT_SUCCESS;
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
```
|
1
追記
test
CHANGED
@@ -17,3 +17,23 @@
|
|
17
17
|
> }
|
18
18
|
|
19
19
|
> ```
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
外側のループを削除してください。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
質問の仕方について
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
teratailでは、上記のようにコードを見やすく表示する機能があります。
|
32
|
+
|
33
|
+
質問編集画面を開き、コードを選択した状態で<code>ボタンを押してください。
|
34
|
+
|
35
|
+
インデントが残る他、`#`も正しく表示されます。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
また、質問のタグにはご注意ください。CとC#は別の言語です。
|