-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix.c
76 lines (71 loc) · 1.57 KB
/
infix.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
#include"function.h"
list infix(list E) {
token t;
node *p = E;
//operand stack
oprnd s1;
init_oprnd(&s1);
//operator stack
oprator s2;
init_oprator(&s2);
int curr_state;
char op;
while(p) {
t = gettoken(p, &s1, &s2);
curr_state = t.type;
if(curr_state == error) {
printf("\nERROR\n");
exit(0);
}
switch(curr_state) {
case operator:
op = t.value.op;
pop_oprator(&s2);
while(!isEmpty_oprator(s2) && precedence(peek_oprator(s2)) >= precedence(op)) {
list n2 = pop_oprnd(&s1);
list n1 = pop_oprnd(&s1);
char op2 = pop_oprator(&s2);
list R = applyOp(n1, n2, op2);
push_oprnd(&s1, R);
}
push_oprator(&s2, op); //works fine
break;
case left:
push_oprator(&s2, op);
break;
case right:
while(!isEmpty_oprator(s2) && peek_oprator(s2) != '(') {
list n2 = pop_oprnd(&s1);
list n1 = pop_oprnd(&s1);
char op = pop_oprator(&s2);
list R = applyOp(n1, n2, op);
push_oprnd(&s1, R);
}
if(peek_oprator(s2) == '(')
pop_oprator(&s2);
break;
default:
exit;
break;
}
p = p->next;
printf(" ");
}
//if after operator list is empty
if(!p && curr_state == 2) {
printf("\nERROR\n");
exit(0);
}
//stop case i.e. list is empty
while(!isEmpty_oprator(s2)) {
list n2 = pop_oprnd(&s1);
list n1 = pop_oprnd(&s1);
char op = pop_oprator(&s2);
list R = applyOp(n1, n2, op);
push_oprnd(&s1, R);
}
if(!isEmpty_oprnd(s1)) {
list Num = pop_oprnd(&s1);
return Num;
}
}