質問編集履歴
1
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
解決しました、、、、、
|
test
CHANGED
@@ -1,443 +1,21 @@
|
|
1
|
-
|
1
|
+
**
|
2
2
|
|
3
|
-
|
3
|
+
__
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
###
|
7
|
+
###
|
8
8
|
|
9
|
-
|
9
|
+
```
|
10
10
|
|
11
11
|
```
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
コード
|
16
|
-
|
17
|
-
|
18
16
|
|
19
17
|
```
|
20
18
|
|
19
|
+
コード
|
21
20
|
|
22
|
-
|
23
|
-
### 該当のソースコード
|
24
|
-
|
25
|
-
c言語
|
26
|
-
|
27
|
-
```
|
21
|
+
```__**
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
#include<stdio.h>
|
32
|
-
|
33
|
-
#include<stdlib.h>
|
34
|
-
|
35
|
-
#include<string.h>
|
36
|
-
|
37
|
-
#include <time.h>
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
#define ADVANCED 0 //発展課題(絞り込み検索)に対応する場合は1に変更
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
#define DATAFILE "data_sjis.csv" //data_utf.csvかdata_sjis.csvに変更
|
46
|
-
|
47
|
-
#define CLEN 9 //郵便番号の最大バイト長
|
48
|
-
|
49
|
-
#define ALEN 200 //住所欄の最大バイト長
|
50
|
-
|
51
|
-
#define MAX_SIZE 200000//住所録中の住所数の最大数
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
int mode; //検索モード 0:なし,1:郵便番号検索,2:文字列検索
|
56
|
-
|
57
|
-
int refine_flag; //絞り込み検索の有無 0:なし,1:あり
|
58
|
-
|
59
|
-
char query[ALEN]; //検索クエリ(郵便番号or文字列)
|
60
|
-
|
61
|
-
long line=0;
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
typedef struct{
|
66
|
-
|
67
|
-
int CODE;
|
68
|
-
|
69
|
-
char PREF[ALEN+1];
|
70
|
-
|
71
|
-
char CITY[ALEN+1];
|
72
|
-
|
73
|
-
char TOWN[ALEN+1];
|
74
|
-
|
75
|
-
}add;
|
76
|
-
|
77
|
-
add ad[MAX_SIZE];
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
//”をとばす
|
82
|
-
|
83
|
-
void quotation(char a[],int n){
|
84
|
-
|
85
|
-
for (int i = 0; i < n; i++){
|
86
|
-
|
87
|
-
if(a[i] == '"'){
|
88
|
-
|
89
|
-
for (int j = i; j < n; j++){
|
90
|
-
|
91
|
-
a[j] = a[j+1];
|
92
|
-
|
93
|
-
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
//住所データファイルを読み取り,配列に保存
|
106
|
-
|
107
|
-
void scan(){
|
108
|
-
|
109
|
-
FILE *fp;
|
110
|
-
|
111
|
-
char code[CLEN+1];
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
//datasizeの計算
|
116
|
-
|
117
|
-
if ((fp = fopen(DATAFILE, "r")) == NULL) {
|
118
|
-
|
119
|
-
fprintf(stderr,"error:cannot read %s\n",DATAFILE);
|
120
|
-
|
121
|
-
exit(-1);
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
while(fscanf(fp, "%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%[^,],%[^,],%[^,],%*s",code,ad[line].PREF,ad[line].CITY,ad[line].TOWN) != EOF ){
|
126
|
-
|
127
|
-
quotation(code,CLEN+1);
|
128
|
-
|
129
|
-
quotation(ad[line].PREF,ALEN+1);
|
130
|
-
|
131
|
-
quotation(ad[line].CITY,ALEN+1);
|
132
|
-
|
133
|
-
quotation(ad[line].TOWN,ALEN+1);
|
134
|
-
|
135
|
-
ad[line].CODE=atoi(code);
|
136
|
-
|
137
|
-
line++;
|
138
|
-
|
139
|
-
}
|
140
|
-
|
141
|
-
printf("%ld行の住所があります\n",line);
|
142
|
-
|
143
|
-
fclose(fp);
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
int cmpAsccode(const void * n1, const void * n2)
|
150
|
-
|
151
|
-
{
|
152
|
-
|
153
|
-
if (((add *)n1)->CODE > ((add *)n2)->CODE)
|
154
|
-
|
155
|
-
{
|
156
|
-
|
157
|
-
return 1;
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
else if (((add *)n1)->CODE < ((add *)n2)->CODE)
|
162
|
-
|
163
|
-
{
|
164
|
-
|
165
|
-
return -1;
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
else
|
170
|
-
|
171
|
-
{
|
172
|
-
|
173
|
-
return 0;
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
//昇順に並べる
|
182
|
-
|
183
|
-
void preprocess(){
|
184
|
-
|
185
|
-
qsort(ad, sizeof(ad) / sizeof(ad[0]), sizeof(add), cmpAsccode);
|
186
|
-
|
187
|
-
return;
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
double diff_time(clock_t t1, clock_t t2){
|
194
|
-
|
195
|
-
return (double) (t2-t1)/CLOCKS_PER_SEC;
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
//初期化処理
|
202
|
-
|
203
|
-
void init(){
|
204
|
-
|
205
|
-
clock_t t1,t2;
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
t1 = clock();
|
210
|
-
|
211
|
-
scan();
|
212
|
-
|
213
|
-
preprocess();
|
214
|
-
|
215
|
-
printf("Done initilization\n");
|
216
|
-
|
217
|
-
t2 = clock();
|
218
|
-
|
219
|
-
printf("\n### %f sec for initialization. ###\n",diff_time(t1,t2));
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
//郵便番号による住所検索.検索結果を出力.
|
226
|
-
|
227
|
-
void code_search(){
|
228
|
-
|
229
|
-
int QUERY = atoi(query);
|
230
|
-
|
231
|
-
for(int i=0;i<=line;i++){
|
232
|
-
|
233
|
-
if(QUERY == ad[i].CODE){
|
234
|
-
|
235
|
-
printf("%d:%s%s%s\n",ad[i].CODE,ad[i].PREF,ad[i].CITY,ad[i].TOWN);
|
236
|
-
|
237
|
-
}else if(QUERY < ad[i].CODE){
|
238
|
-
|
239
|
-
break;
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
return;
|
246
|
-
|
247
|
-
}
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
//文字列による住所検索.検索結果を出力.
|
252
|
-
|
253
|
-
void address_search(){
|
254
|
-
|
255
|
-
char *adr1,*adr2,*adr3;
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
for(int j=0;j<=line;j++){
|
260
|
-
|
261
|
-
adr1 = strstr(ad[j].PREF,query);
|
262
|
-
|
263
|
-
adr2 = strstr(ad[j].CITY,query);
|
264
|
-
|
265
|
-
adr3 = strstr(ad[j].TOWN,query);
|
266
|
-
|
267
|
-
if(adr1 != NULL||adr2 != NULL||adr3 != NULL)printf("%d:%s%s%s\n",ad[j].CODE,ad[j].PREF,ad[j].CITY,ad[j].TOWN);
|
268
|
-
|
269
|
-
}
|
270
|
-
|
271
|
-
return;
|
272
|
-
|
273
|
-
}
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
//絞り込み検索の実施
|
278
|
-
|
279
|
-
void refinement(){
|
280
|
-
|
281
|
-
return;
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
void input(){
|
288
|
-
|
289
|
-
printf("\n"
|
290
|
-
|
291
|
-
"#########Top Menu#########\n"
|
292
|
-
|
293
|
-
"# Search by postal code: 1\n"
|
294
|
-
|
295
|
-
"# Search by address : 2\n"
|
296
|
-
|
297
|
-
"# Exit : 0\n"
|
298
|
-
|
299
|
-
"> ");
|
300
|
-
|
301
|
-
scanf("%d", &mode);
|
302
|
-
|
303
|
-
if(mode == 1){
|
304
|
-
|
305
|
-
printf("Postal code > ");
|
306
|
-
|
307
|
-
scanf("%s", query);
|
308
|
-
|
309
|
-
}else if(mode == 2){
|
310
|
-
|
311
|
-
printf("Search String > ");
|
312
|
-
|
313
|
-
scanf("%s", query);
|
314
|
-
|
315
|
-
}
|
316
|
-
|
317
|
-
}
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
//絞り込み検索の有無を確認
|
322
|
-
|
323
|
-
void re_input(){
|
324
|
-
|
325
|
-
printf("\n"
|
326
|
-
|
327
|
-
"# Continue Searching: 1\n"
|
328
|
-
|
329
|
-
"# Return to Top Menu: 0\n"
|
330
|
-
|
331
|
-
"> ");
|
332
|
-
|
333
|
-
scanf("%d", &refine_flag);
|
334
|
-
|
335
|
-
if(refine_flag == 1){
|
336
|
-
|
337
|
-
printf("String for Refinement> ");
|
338
|
-
|
339
|
-
scanf("%s", query);
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
return;
|
344
|
-
|
345
|
-
}
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
//クエリへの応答
|
350
|
-
|
351
|
-
void respond(){
|
352
|
-
|
353
|
-
clock_t t1,t2;
|
354
|
-
|
355
|
-
mode = 1;
|
356
|
-
|
357
|
-
while(1){
|
358
|
-
|
359
|
-
input();
|
360
|
-
|
361
|
-
if(mode == 1){
|
362
|
-
|
363
|
-
t1 = clock();
|
364
|
-
|
365
|
-
code_search();
|
366
|
-
|
367
|
-
t2 = clock();
|
368
|
-
|
369
|
-
printf("\n### %f sec for search. ###\n", diff_time(t1,t2));
|
370
|
-
|
371
|
-
}
|
372
|
-
|
373
|
-
else if(mode == 2){
|
374
|
-
|
375
|
-
t1 = clock();
|
376
|
-
|
377
|
-
address_search();
|
378
|
-
|
379
|
-
t2 = clock();
|
380
|
-
|
381
|
-
printf("\n### %f sec for search. ###\n", diff_time(t1,t2));
|
382
|
-
|
383
|
-
if(!ADVANCED) continue;
|
384
|
-
|
385
|
-
while(1){
|
386
|
-
|
387
|
-
re_input();
|
388
|
-
|
389
|
-
if(refine_flag == 0) break;
|
390
|
-
|
391
|
-
t1 = clock();
|
392
|
-
|
393
|
-
refinement();
|
394
|
-
|
395
|
-
t2 = clock();
|
396
|
-
|
397
|
-
printf("\n### %f sec for search. ###\n", diff_time(t1,t2));
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
}
|
402
|
-
|
403
|
-
else break;
|
404
|
-
|
405
|
-
}
|
406
|
-
|
407
|
-
}
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
int main()
|
414
|
-
|
415
|
-
{
|
416
|
-
|
417
|
-
init();
|
418
|
-
|
419
|
-
respond();
|
420
|
-
|
421
|
-
return 0;
|
422
|
-
|
423
|
-
}
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
```
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
### 試したこと
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
オーバーフローしていないか確認した。
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
### 補足情報(FW/ツールのバージョンなど)
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
ここにより詳細な情報を記載してください。
|