This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.c
304 lines (275 loc) · 7.24 KB
/
sol.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define MAX_BUF 80
#define OP_NODE 0
#define NU_NODE 1
struct __option {
char file[80];
};
typedef struct __option option_t;
void parse_option (option_t * opt, int argc, char** argv) {
if (argc != 2) {
printf ("Usage: main <testfile>\n");
}
strcpy (opt->file, argv[1]);
}
// char stack
struct __char_node {
char element;
struct __char_node * next;
};
typedef struct __char_node char_node_t;
struct __char_stack {
char_node_t * top;
};
typedef struct __char_stack char_stack_t;
char_stack_t * create_char_stack () {
char_stack_t * this = (char_stack_t *) malloc (sizeof (char_stack_t));
this->top = NULL;
return this;
}
void char_push (char_stack_t * this, char c) {
char_node_t * node = (char_node_t *) malloc (sizeof (char_node_t));
node->element = c;
node->next = this->top;
this->top = node;
}
int is_char_empty (char_stack_t * this) {
return this->top == NULL;
}
char char_pop (char_stack_t * this) {
char_node_t * del = this->top;
char c = del->element;
this->top = del->next;
free (del);
return c;
}
void delete_char_stack (char_stack_t * this) {
char_node_t * node = this->top;
while (node != NULL) {
char_node_t * del = node;
node = node->next;
free (del);
}
free (this);
}
// int stack
struct __int_node {
int element;
struct __int_node * next;
};
typedef struct __int_node int_node_t;
struct __int_stack {
int_node_t * top;
};
typedef struct __int_stack int_stack_t;
int_stack_t * create_int_stack () {
int_stack_t * this = (int_stack_t *) malloc (sizeof (int_stack_t));
this->top = NULL;
return this;
}
void int_push (int_stack_t * this, int n) {
int_node_t * node = (int_node_t *) malloc (sizeof (int_node_t));
node->element = n;
node->next = this->top;
this->top = node;
}
int is_int_empty (int_stack_t * this) {
return this->top == NULL;
}
int int_pop (int_stack_t * this) {
int_node_t * del = this->top;
int n = del->element;
this->top = del->next;
free (del);
return n;
}
void delete_int_stack (int_stack_t * this) {
int_node_t * node = this->top;
while (node != NULL) {
int_node_t * del = node;
node = node->next;
free (del);
}
free (this);
}
// util
int find_number (char * str, int * i, int max) {
int result = 0;
int end_of_num = FALSE;
while (*i < max) {
switch (str[*i]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
result *= 10;
result += str[*i] - '0';
break;
default:
end_of_num = TRUE;
}
if (end_of_num) {
(*i)--;
break;
}
(*i)++;
}
return result;
}
// main
int main (int argc, char** argv) {
option_t opt;
parse_option (&opt, argc, argv);
FILE * f = fopen (opt.file, "r");
char buf[MAX_BUF];
char back_marking[MAX_BUF] = { 0, };
// back marking expression creation part
char_stack_t * op_stack = create_char_stack ();
while (fgets (buf, MAX_BUF, f) != NULL) {
int i = 0;
int num;
char op;
int end_of_line = FALSE;
while (!end_of_line) {
switch (buf[i]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num = find_number (buf, &i, MAX_BUF);
back_marking[0] == 0
? sprintf(back_marking, "%d", num)
: sprintf (back_marking, "%s %d", back_marking, num);
break;
case '+':
case '-':
while (!is_char_empty (op_stack)) {
op = char_pop (op_stack);
if (op == '(') {
char_push (op_stack, op);
break;
}
sprintf (back_marking, "%s %c", back_marking, op);
}
char_push (op_stack, buf[i]);
break;
case '*':
case '/':
while (!is_char_empty (op_stack)) {
op = char_pop (op_stack);
if (op == '(' || op == '+' || op == '-') {
char_push (op_stack, op);
break;
}
sprintf (back_marking, "%s %c", back_marking, op);
}
char_push (op_stack, buf[i]);
break;
case '(':
char_push (op_stack, buf[i]);
break;
case ')':
while (TRUE) {
op = char_pop (op_stack);
if (op == '(') {
break;
}
sprintf(back_marking, "%s %c", back_marking, op);
}
break;
case ' ':
case '\n':
case '\t':
break;
default:
end_of_line = TRUE;
}
i++;
}
}
while (!is_char_empty (op_stack)) {
sprintf (back_marking, "%s %c", back_marking, char_pop (op_stack));
}
delete_char_stack (op_stack);
fclose (f);
// calculate back marking expression
int_stack_t * num_stack = create_int_stack ();
int end_of_expression = FALSE;
int i = 0;
int num;
int n1;
int n2;
while (!end_of_expression) {
switch (back_marking[i]) {
// find number
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num = find_number (back_marking, &i, MAX_BUF);
// push number into stack
int_push (num_stack, num);
break;
// operators
case '+':
// pop two numbers from stack;
// notice that popped numbers are in REVERSE order
n2 = int_pop (num_stack);
n1 = int_pop (num_stack);
// push calculated number into stack
int_push (num_stack, n1 + n2);
break;
case '-':
// do as same as +
n2 = int_pop (num_stack);
n1 = int_pop (num_stack);
int_push (num_stack, n1 - n2);
break;
case '*':
n2 = int_pop (num_stack);
n1 = int_pop (num_stack);
int_push (num_stack, n1 * n2);
break;
case '/':
n2 = int_pop (num_stack);
n1 = int_pop (num_stack);
int_push (num_stack, n1 / n2);
break;
// whitespaces
case ' ':
case '\n':
case '\t':
// discard whitespaces
break;
default:
end_of_expression = TRUE;
}
i++;
}
// pop last number from stack
printf ("%d\n", int_pop (num_stack));
delete_int_stack (num_stack);
return 0;
}