-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressionEval.c
228 lines (187 loc) · 4.53 KB
/
expressionEval.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Stack Integer-----------------------------------//
struct StackInt
{
int top;
int size;
int* array;
};
struct StackInt* initStack1(int size)
{
struct StackInt* stack = (struct StackInt*) malloc(sizeof(struct StackInt));
stack->size = size;
stack->top = -1;
stack->array = (int*) malloc(size * sizeof(char));
return stack;
}
int isFull1(struct StackInt* stack)
{
if(stack->top == stack->size - 1) return 1;
else return 0;
}
int isEmpty1(struct StackInt* stack)
{
if(stack->top == -1) return 1;
else return 0;
}
void push1(struct StackInt *stack, int item)
{
if (isFull1(stack))
{
printf("Stack is full!! Exit!!");
exit(-1);
}
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
int pop1(struct StackInt* stack)
{
if (isEmpty1(stack))
{
printf("Stack is Empty!! Exit!!");
exit(-1);
}
return stack->array[stack->top--];
}
int peek1(struct StackInt* stack)
{
int k = pop1(stack);
push1(stack, k);
return k;
}
//End of Stack Integer-----------------------------------//
//Stack Char-----------------------------------//
struct StackChar
{
int top;
int size;
char* array;
};
struct StackChar* initStack2(int size)
{
struct StackChar* stack = (struct StackChar*) malloc(sizeof(struct StackChar));
stack->size = size;
stack->top = -1;
stack->array = (char*) malloc(size * sizeof(char));
return stack;
}
int isFull2(struct StackChar* stack)
{
if(stack->top == stack->size - 1) return 1;
else return 0;
}
int isEmpty2(struct StackChar* stack)
{
if(stack->top == -1) return 1;
else return 0;
}
void push2(struct StackChar *stack, char item)
{
if (isFull2(stack))
{
printf("Stack is full!! Exit!!");
exit(-1);
}
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
int pop2(struct StackChar* stack)
{
if (isEmpty2(stack))
{
printf("Stack is Empty!! Exit!!");
exit(-1);
}
return stack->array[stack->top--];
}
char peek2(struct StackChar* stack)
{
char k = pop2(stack);
push2(stack, k);
return k;
}
//End of Stack Char-----------------------------------//
int precedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return 0;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return 0;
else
return 1;
}
int execOperator(int a, int b, char op)
{
switch(op)
{
case '+': return a+b; break;
case '-': return a-b; break;
case '*': return a*b; break;
case '/': if(b==0) {
printf("Divide by zero!!");
exit(-1);
}
return a/b; break;
}
return 0;
}
int evaluate(char expression[])
{
struct StackChar* stackChar = initStack2(1000);
struct StackInt* stackInt = initStack1(1000);
for(int i=0; i<strlen(expression); i++)
{
printf("i=%d\n", i);fflush(stdout);
if(expression[i] == ' ');
else if (expression[i] >= '0' && expression[i] <= '9')
{
int k = 0;
while (i < strlen(expression) && expression[i] >= '0' && expression[i] <= '9')
{
k = k*10 + (expression[i]-'0');
i++;
}
if(expression[i] != ' ')i--;
push1(stackInt, k);
}
else if (expression[i] == '(') push2(stackChar, expression[i]);
else if (expression[i] == ')')
{
while(peek2(stackChar) != '(')
push1(stackInt, execOperator(pop1(stackInt), pop1(stackInt), pop2(stackChar)));
pop2(stackChar);
}
else if (expression[i] == '+' || expression[i] == '-' ||
expression[i] == '*' || expression[i] == '/')
{
// printf("running here\n");fflush(stdout);
while(!isEmpty2(stackChar) && precedence(expression[i], peek2(stackChar)))
push1(stackInt, execOperator(pop1(stackInt), pop1(stackInt), pop2(stackChar)));
push2(stackChar, expression[i]);
}
}
while(!isEmpty2(stackChar)) push1(stackInt, execOperator(pop1(stackInt), pop1(stackInt), pop2(stackChar)));
return pop1(stackInt);
}
int main()
{
char buf[100];
printf("Nhap bieu thuc: ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
int result = 0;
printf("Ket qua: %d\n", evaluate(buf));
// struct StackChar* stack = initStack2(100);
// push2(stack, 'a');
// push2(stack, 'b');
// push2(stack, 'c');
// push2(stack, 'd');
// printf("%c popped from stack\n", pop2(stack));
// push2(stack, 'e');
// printf("%c popped from stack\n", pop2(stack));
// printf("%c popped from stack\n", pop2(stack));
// printf("%c popped from stack\n", pop2(stack));
return 0;
}