-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproject_S.c
More file actions
250 lines (209 loc) · 4.29 KB
/
project_S.c
File metadata and controls
250 lines (209 loc) · 4.29 KB
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
#include <omp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
#define INPUTFOLDER "inputs"
#define MAXTEXTS 20
#define MAXPATTERNS 20
#define BUFSIZE 100000
char *textData[MAXTEXTS];
int textLength[MAXTEXTS];
char *patternData[MAXPATTERNS];
int patternLength[MAXPATTERNS];
int controlData[800][3];
// File input/output
void outOfMemory()
{
fprintf (stderr, "Out of memory\n");
exit (0);
}
void readFromFile(FILE *f, char **data, int *length)
{
int ch;
int allocatedLength;
char *result;
int resultLength = 0;
allocatedLength = 0;
result = NULL;
ch = fgetc(f);
while (ch >= 0)
{
resultLength++;
if (resultLength > allocatedLength)
{
allocatedLength += 10000;
result = (char *)realloc(result, sizeof(char) * allocatedLength);
if (result == NULL)
outOfMemory();
}
result[resultLength-1] = ch;
ch = fgetc(f);
}
*data = result;
*length = resultLength;
}
int readTexts()
{
int i;
for (i = 0; i < MAXTEXTS; i++)
{
FILE *f;
char fileName[1000];
#ifdef DOS
sprintf (fileName, "%s\\text%d.txt", INPUTFOLDER, i);
#else
sprintf (fileName, "%s/text%d.txt", INPUTFOLDER, i);
#endif
f = fopen(fileName, "r");
if (f == NULL)
return i;
readFromFile (f, &textData[i], &textLength[i]);
fclose (f);
}
return MAXTEXTS;
}
int readPatterns()
{
int i;
for (i = 0; i < MAXPATTERNS; i++)
{
FILE *f;
char fileName[1000];
#ifdef DOS
sprintf (fileName, "%s\\pattern%d.txt", INPUTFOLDER, i);
#else
sprintf (fileName, "%s/pattern%d.txt", INPUTFOLDER, i);
#endif
f = fopen (fileName, "r");
if (f == NULL)
return i;
readFromFile (f, &patternData[i], &patternLength[i]);
fclose (f);
}
return MAXPATTERNS;
}
int readControl()
{
FILE *f;
char fileName[1000];
#ifdef DOS
sprintf (fileName, "%s\\control.txt", INPUTFOLDER);
#else
sprintf (fileName, "%s/control.txt", INPUTFOLDER);
#endif
f = fopen (fileName, "r");
if (f == NULL)
return 0;
int r;
int i = 0;
while ((r = fscanf(f, "%d %d %d", &controlData[i][0], &controlData[i][1], &controlData[i][2])) != EOF)
{
if (r == 3)
i++;
}
fclose (f);
return i;
}
void writeOutputFile(char buf[])
{
FILE *f;
char fileName[1000];
sprintf (fileName, "result_S.txt");
f = fopen(fileName, "a+");
if (f == NULL)
return;
fprintf(f, buf);
fclose(f);
buf[0] = '\0';
}
void writeToBuffer(char buf[], int textIndex, int patternIndex, int matchPoint)
{
if (strlen(buf) > BUFSIZE - 20)
writeOutputFile(buf);
sprintf(buf+strlen(buf), "%d %d %d\n", textIndex, patternIndex, matchPoint);
}
// Mode 0 - find the first occurrence of the pattern
int findFirstInstance(int textIndex, int patternIndex)
{
int i,j,k, lastI;
i=0;
j=0;
k=0;
lastI = textLength[textIndex] - patternLength[patternIndex];
while (i<=lastI && j<patternLength[patternIndex])
{
if (textData[textIndex][k] == patternData[patternIndex][j])
{
k++;
j++;
}
else
{
i++;
k=i;
j=0;
}
}
if (j == patternLength[patternIndex])
return i;
else
return -1;
}
// Mode 1 - find all occurrences of the pattern
void findAllInstances(int textIndex, int patternIndex, char buf[])
{
int i,j,k, lastI;
int found = 0;
i=0;
j=0;
k=0;
lastI = textLength[textIndex] - patternLength[patternIndex];
while (i<=lastI)
{
if (textData[textIndex][k] == patternData[patternIndex][j])
{
k++;
j++;
if (j == patternLength[patternIndex])
{
found = 1;
writeToBuffer(buf, textIndex, patternIndex, i);
i++;
k=i;
j=0;
}
}
else
{
i++;
k=i;
j=0;
}
}
if (!found)
writeToBuffer(buf, textIndex, patternIndex, -1);
}
void processData(int mode, int textIndex, int patternIndex, char buf[])
{
if (mode == 0)
writeToBuffer(buf, textIndex, patternIndex, findFirstInstance(textIndex, patternIndex));
else
findAllInstances(textIndex, patternIndex, buf);
}
int main(int argc, char **argv)
{
int numberOfTests = readControl();
readTexts();
readPatterns();
char buf[BUFSIZE];
sprintf(buf, "");
int i;
for (i = 0; i < numberOfTests; i++)
processData(controlData[i][0], controlData[i][1], controlData[i][2], buf);
writeOutputFile(buf);
}