-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path소스.cpp
340 lines (317 loc) · 5.94 KB
/
소스.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <stdio.h>
#include <io.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
struct Phrase
{
char *word;// 파싱된 단어를 저장
};
typedef struct Phrase phrase;
struct file
{
char name[30];
int existence = 0;
int wordnum = 0;
int countforsorting = 0;
phrase *words; // 단어들의 배열을 저장
};
struct file *fi;
void findfile(int *c);
void searchingstring(int *c, char string[30]);
void divide_string(int *c);
void randomnumber(int *c, char string[30]);
void sorting(int *c);
int main()
{
int count = 1;
char str[30] = { 0 };
clock_t before;
double result;
findfile(&count);
printf("Please input string that you want: ");
scanf("%s", str);
before = clock();
searchingstring(&count, str);
divide_string(&count);
randomnumber(&count, str);
sorting(&count);
free(fi);
result = (double)(clock() - before) / CLOCKS_PER_SEC;
printf("걸린시간: %5.2f\n", result);
return 0;
}
void findfile(int *c)
{
struct _finddata_t fd;
long handle;
int result = 1;
int i = 0;
handle = _findfirst("c:\*.txt", &fd);
if (handle == -1)
{
printf("No files!!!\n");
return;
}
while (result != -1)
{
result = _findnext(handle, &fd);
(*c)++;
}
handle = _findfirst("c:\*.txt", &fd);
result = 1;
fi = (struct file *)calloc((*c)-1, sizeof(struct file));
while (result != -1)
{
strcpy(fi[i].name, fd.name);
result = _findnext(handle, &fd);
i++;
}
_findclose(handle);
return;
}
void searchingstring(int *c, char string[30])
{
int i;
int j;
int n = 0;
int p = 0;
char ch = 1;
int k = 0;
for (j = 0;; j++)
{
if (string[j] == 0)
{
break;
}
p++;
}
for (i = 0; i < (*c) - 1; i++)
{
FILE *fp = fopen(fi[i].name, "rt");
fi[i].existence = 0;
while ((ch = fgetc(fp)) != EOF)
{
if (ch == string[n] || ch == string[n] - 32)
{
n++;
}
else
{
n = 0;
}
if (n == p)
{
fi[i].existence = 1;
break;
}
}
fclose(fp);
}
printf("검색한 문자열이 존재하는 파일\n");
for (i = 0; i < (*c) - 1; i++)
{
if (fi[i].existence == 1)
{
printf("%s\n", fi[i].name);
}
}
printf("\n");
return;
}
void divide_string(int *c)
{
int i;
int j = 0;
char *result = { 0 };
int ch = 1;
int str = 1;
char wordbuf[2] = { 0 };
int k = 0;
for (i = 0; i < (*c) - 1; i++)
{
if (fi[i].existence == 1)
{
FILE *fp = fopen(fi[i].name, "r");
j = 0;
ch = 1;
k = 0;
fseek(fp, 0, SEEK_SET);
while ((ch != EOF))
{
ch = fgetc(fp);
if ((ch < '0')
|| ('9' < ch&&ch < 'A')
|| ('Z' < ch&&ch < 'a')
|| ('z' < ch))
{
j++;
while (1)
{
ch = fgetc(fp);
if (('0' <= ch && ch <= '9')
|| ('A' <= ch && ch <= 'Z')
|| ('a' <= ch && ch <= 'z') ||
(ch == EOF))
{
break;
}
}
}
}
fi[i].wordnum = j;
fi[i].words = (phrase *)calloc(fi[i].wordnum, sizeof(phrase));
ch = 1;
fseek(fp, 0, SEEK_SET);
while (ch != EOF)
{
fi[i].words->word = (char*)calloc(55, sizeof(char));
while (1)
{
ch = fgetc(fp);
if (65 <= ch&&ch <= 90)
{
ch = ch + 32;
}
wordbuf[0] = (char)ch;
result = &wordbuf[0];
if ((48 <= wordbuf[0] && wordbuf[0] <= 57)
|| (65 <= wordbuf[0] && wordbuf[0] <= 90)
|| (97 <= wordbuf[0] && wordbuf[0] <= 122))
{
strcat(fi[i].words->word, result);
}
else
{
while (1)
{
ch = fgetc(fp);
if (('0' <= ch && ch <= '9')
|| ('A' <= ch && ch <= 'Z')
|| ('a' <= ch && ch <= 'z') ||
(ch == EOF))
{
fseek(fp, -1, SEEK_CUR);
break;
}
}
fi[i].words++;
break;
}
}
}
fclose(fp);
}
}
return;
}
void randomnumber(int *c, char string[30])
{
int a;
int i;
int j;
int k=0;
int l;
int computer = 0;
char *result = { 0 };
int randcount = 0;
int q;
for (i = 0; i < strlen(string); i++)
{
if (65 <= string[i] && string[i] <= 90)
{
string[i] = string[i] + 32;
}
}
for (i = 0; i < (*c) - 1; i++)
{
if (fi[i].existence == 1)
{
for(q=0;q<fi[i].wordnum;q++)
{
fi[i].words--;
}
}
}
for (i = 0; i < (*c) - 1; i++)
{
if (fi[i].existence == 1)
{
srand(time(NULL));
randcount = 0;
computer = 0;
while (1)
{
computer = (rand() % fi[i].wordnum) + 1;
for (j = 1; j <= fi[i].wordnum; j++)
{
if (computer == j)
{
result = strstr(fi[i].words->word, string);
randcount++;
break;
}
else
{
fi[i].words++;
k++;
}
}
if (result != '\0')
{
fi[i].countforsorting = randcount;
break;
}
else
{
for (l = 0; l < k; l++)
{
fi[i].words--;
}
k = 0;
}
}
k = 0;
}
}
return;
}
void sorting(int *c)
{
int i;
int j;
int l = 1;
int temp = 0;
char str[50] = { 0 };
int count = 0;
for (i = 0; i < (*c) - 1; i++)
{
if (fi[i].existence == 1)
{
count++;
}
}
for (i = 0; i < (*c) - 2; i++)
{
for (j = 0; j < ((*c-1) - i) - 1; j++)
{
if (fi[j].countforsorting > fi[j + 1].countforsorting)
{
temp = fi[j].countforsorting;
fi[j].countforsorting = fi[j + 1].countforsorting;
fi[j + 1].countforsorting = temp;
strcpy(str, fi[j].name);
strcpy(fi[j].name, fi[j + 1].name);
strcpy(fi[j + 1].name, str);
}
}
}
printf("\n\n--------------------------------------------------------------------------------------------------\n\n\n");
printf("RANKING: 검색한 문자열을 포함하는 단어를 가장 먼저 찾은 텍스트 파일\n");
for (i = (*c-1)-count; i < (*c)-1; i++)
{
printf("%d등. 파일 이름: %s 단어를 뽑은 횟수: %d\n\n", l, fi[i].name, fi[i].countforsorting);
l++;
}
return;
}