-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
423 lines (382 loc) · 9.49 KB
/
main.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 10000 //maximum size of an expression
/*defining all structs and enum to be used in program*/
//Type represents different types of cell
typedef enum {
NUMBER, POSITION, EXPRESSION
}Type;
//Cell represents indivisual cell in csv file
typedef struct {
char* val;
Type type;
}Cell ;
//Table represents the structure of csv file
typedef struct {
int rows;
int cols;
Cell** table;
}Table;
//Stack represents stack data structure
typedef struct{
int top;
int arr[MAX_SIZE];
}Stack ;
/*function declarations*/
void parse(char* buffer, int m, Table *t);
void analyse(Table *t);
void solveWrapper(Table *t);
int solve(Table *t, int i, int j, int visited[t->rows][t->cols]);
char* simplify(char* s, Table* t, int visited[t->rows][t->cols]);
char* infixToPostfix(char *infix);
int precedence(char c);
int evaluate(char *postfix);
/*driver function*/
int main(int argc, char* argv[])
{
if (argc < 2)
{
fprintf(stderr, "ERROR: input file is not provided\n");
exit(1);
}
char* file_path = argv[1];
FILE *f = fopen(file_path, "rb");
if (f == NULL)
{
fprintf(stderr, "ERROR: Cannot allocate memory\n");
fclose(f);
exit(1);
}
//creating a temporary pointer to obtain size of file
FILE* temp = f;
fseek(temp, 0, SEEK_END);
long m = ftell(temp);
fclose(temp);
//storing contents of file in buffer
char* buffer = malloc(sizeof(char)*m);
if (buffer == NULL)
{
fprintf(stderr, "ERROR: Cannot allocate memory\n");
fclose(f);
exit(1);
}
//printing contents of buffer
printf("Your input CSV file:\n");
fwrite(buffer, sizeof(char), m, stdout);
printf("\n");
//parse the csv file and store it in Table t
Table t;
parse(buffer, m, &t);
//assign each cell a type
analyse(&t);
// //solve expression cells
solveWrapper(&t);
//print output csv file
printf("The output file is: \n");
for (int i = 0 ; i < t.rows; i++)
{
for (int j = 0 ; j < t.cols; j++)
{
printf("%s ", t.table[i][j].val);
}
printf("\n");
}
}
/*function definitions*/
void parse(char* buffer, int m, Table *t)
{
int row = 0;
int col = 0;
//detecting number of rows and columns in csv file
for (int i = 0; i < m ; i++)
{
if (buffer[i] == '\n')
{
row++;
}
if (row == 0 && buffer[i] == '|')
{
col++;
}
}
//incrementing since a single '|' will represent two columns
t->rows = ++row;
t->cols = ++col;
printf("The dimension of csv file is %dx%d\n", t->rows, t->cols);
Cell *p = NULL;
t->table = (Cell**)malloc(sizeof(p)*t->rows);
for (int i = 0 ; i < t->rows; i++)
{
t->table[i] = (Cell*)malloc(sizeof(Cell)*t->cols);
}
int k = 0;
for (int i = 0 ; i < t->rows; i++)
{
for (int j = 0 ; j < t->cols; j++)
{
char *temp = "";
while(k < m && buffer[k] != '|' && buffer[k] != '\n')
{
if(buffer[k] == ' ')
{
k++;
continue;
}
char c = buffer[k];
char* new_temp = malloc(strlen(temp) + 1 + 1);
strcpy(new_temp, temp);
new_temp[strlen(temp)] = c;
new_temp[strlen(temp) + 1] = '\0';
temp = new_temp;
k++;
}
(t->table[i][j]).val = temp;
k++;
}
}
}
void analyse(Table *t)
{
for (int i = 0 ; i < t->rows; i++)
{
for (int j = 0 ; j < t->cols; j++)
{
if(t->table[i][j].val[0] == '=')
{
t->table[i][j].type = EXPRESSION;
}
else if (t->table[i][j].val[0] >= '0' && t->table[i][j].val[0] <= '9')
{
t->table[i][j].type = NUMBER;
}
else
{
t->table[i][j].type = POSITION;
}
}
}
}
void solveWrapper(Table *t)
{
for (int i = 0 ; i < t->rows; i++)
{
for (int j = 0 ; j < t->cols; j++)
{
if(t->table[i][j].type == EXPRESSION)
{
int visited[t->rows][t->cols];
for (int x = 0; x < t->rows; x++)
{
for (int y = 0 ; y < t->cols; y++)
{
visited[x][y] = 0;
}
}
int x = solve(t, i, j, visited);
sprintf(t->table[i][j].val, "%d", x);
t->table[i][j].type = NUMBER;
}
}
}
}
int solve(Table *t, int i, int j, int visited[t->rows][t->cols])
{
char* temp = t->table[i][j].val;
if (visited[i][j] == 1)
{
fprintf(stderr,"ERROR: Circular dependecy at cell (%d, %d)\n", i, j);
exit(1);
}
else
{
visited[i][j] = 1;
}
char* infix = simplify(temp, t, visited);
char* postfix = infixToPostfix(infix);
int result = evaluate(postfix);
return result;
}
char* simplify(char* s, Table* t, int visited[t->rows][t->cols])
{
char* new = "";
//we start from i = 1 to ignore = symbol
for (int i = 1 ; i < strlen(s); i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
{
int Op, Op_i, Op_j;
if (i+1 >= strlen(s))
{
fprintf(stderr,"ERROR: Cannot resolve expressions in CSV file");
exit(1);
}
Op_i = s[i+1] - '0';
Op_j = s[i] - 'A';
char* temp = (char*)malloc(sizeof(char)*10);
if (t->table[Op_i][Op_j].type == NUMBER)
{
Op = atoi(t->table[Op_i][Op_j].val);
}
else
{
Op = solve(t, Op_i, Op_j, visited);
}
i++;
sprintf(temp, "%d", Op);
char* new_temp = malloc(strlen(new) + strlen(temp) + 1);
strcpy(new_temp, new);
strcat(new_temp, temp);
new = new_temp;
free(temp);
}
else
{
char c = s[i];
char* new_temp = malloc(strlen(new) + 1 + 1);
strcpy(new_temp, new);
new_temp[strlen(new)] = c;
new_temp[strlen(new) + 1] = '\0';
new = new_temp;
}
}
return new;
}
int precedence(char c)
{
if (c == '+' || c == '-')
{
return 1;
}
else if (c == '*' || c == '/')
{
return 2;
}
else
{
return -1;
}
}
char* infixToPostfix(char *s)
{
//using | as a delimeter for seperating multi-digit numbers
int n = strlen(s);
char* new = (char *)malloc(sizeof(char)*((2*n)+1));
int j = 0;
Stack st;
st.top = -1;
int flag = 0;
for (int i = 0 ; i < n; i++)
{
if (s[i]>='0' && s[i] <= '9')
{
if (j > 0 && new[j-1]>='0' && new[j-1] <= '9' && flag == 0)
{
new[j] = '|';
j++;
}
new[j] = s[i];
j++;
flag = 1;
}
else if (st.top == -1)
{
st.top++;
st.arr[st.top] = s[i];
flag = 0;
}
else if (s[i] == '(')
{
st.top++;
st.arr[st.top] = s[i];
flag = 0;
}
else if (s[i] == ')')
{
while(st.top > -1 && st.arr[st.top] != '(')
{
new[j] = st.arr[st.top];
st.top--;
j++;
}
st.top--;
flag = 0;
}
else
{
while (st.top > -1 && precedence(st.arr[st.top]) >= precedence(s[i]))
{
new[j] = st.arr[st.top];
st.top--;
j++;
}
st.top++;
st.arr[st.top]=s[i];
flag = 0;
}
}
while (st.top > -1)
{
new[j] = st.arr[st.top];
st.top--;
j++;
}
new[j] = '\0';
return new;
}
int evaluate(char *s)
{
Stack operand;
operand.top = -1;
int n = strlen(s);
for (int i = 0 ; i < n ; i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
char* temp = (char*)malloc(sizeof(char)*n);
int k = 0;
while (s[i] >= '0' && s[i] <= '9')
{
temp[k] = s[i];
k++;
i++;
}
i--;
temp[k]='\0';
int num = atoi(temp);
operand.top++;
operand.arr[operand.top] = num;
}
else if (s[i] == '|')
{
continue;
}
else
{
int Op2 = operand.arr[operand.top];
operand.top--;
int Op1 = operand.arr[operand.top];
operand.top--;
int result;
if (s[i] == '+')
{
result = Op1+Op2;
}
else if (s[i] == '-')
{
result = Op1-Op2;
}
else if (s[i] == '*')
{
result = Op1*Op2;
}
else
{
result = Op1/Op2;
}
operand.top++;
operand.arr[operand.top] = result;
}
}
return operand.arr[operand.top];
}