-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefix_Postfix_evaluation.c
More file actions
198 lines (185 loc) · 4.44 KB
/
Copy pathPrefix_Postfix_evaluation.c
File metadata and controls
198 lines (185 loc) · 4.44 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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_SIZE 5
void push(char);
char pop();
int operate(int, int, char);
void postfix(char*);
void prefix(char*, int);
void infix_to_postfix(char *);
int checkifprecedence(char);
struct stack
{
char stck[MAX_SIZE];
int top;
};
struct stack S;
void push(char operand)
{
if(S.top == MAX_SIZE - 1)
{
printf("\nSTACK OVERFLOW\n");
exit(0);
}
else
{
S.top++;
S.stck[S.top] = operand;
}
}
char pop()
{
char return_val;
if(S.top == -1)
{
printf("\nStack is empty\n");
exit(0);
}
else
{
return_val = S.stck[S.top];
S.top--;
}
return return_val;
}
void postfix(char *expression)
{
printf("\nExpression is PostFix\n");
int i = 0, operand = 0, OP1 = 0, OP2 = 0;
while(expression[i] != '\0')
{
if(isdigit(expression[i]))
{
operand = expression[i++] - '0';
push(operand);
}
else
{
OP2 = pop();
OP1 = pop();
push(operate(OP1, OP2, expression[i++]));
}
}
printf("%s\t%d\n","The Result is :",pop());
}
void prefix(char *expression, int length)
{
printf("\nExpression is Prefix\n");
int i = length - 1, operand = 0, OP1 = 0, OP2 = 0;
while(i != -1)
{
if(isdigit(expression[i]))
{
operand = expression[i--] - '0';
push(operand);
}
else
{
OP1 = pop();
OP2 = pop();
push(operate(OP1, OP2, expression[i--]));
}
}
printf("%s\t%d\n","The Result is :",pop());
}
int checkifprecedence(char oper)
{
char precedence_chart[4] = {'*', '/', '+', '-'};
char stck_top = S.stck[S.top];
if(stck_top == '(' || stck_top == ')') return 1;
int i = 0, j = 0;
for(;i < 4; i++) if(S.stck[S.top] == precedence_chart[i]) break;
for(;j < 4; j++) if(oper == precedence_chart[j]) break;
if(j < i) return 1;
else return 0;
}
void infix_to_postfix(char * expression)
{
char * postfix = (char*)malloc(sizeof(char));
int postfix_size = 0, i = 0;
while(expression[i] != '\0')
{
if(isdigit(expression[i]))
{
postfix = (char*)realloc(postfix, sizeof(char) * ++postfix_size);
postfix[postfix_size - 1] = expression[i];
}
else if(expression[i] == '(') push(expression[i]);
else if(expression[i] == ')')
{
postfix = (char*)realloc(postfix, sizeof(char) * ++postfix_size);
postfix[postfix_size - 1] = pop();
pop();
}
else if(checkifprecedence(expression[i])) push(expression[i]);
else
{
postfix = (char*)realloc(postfix, sizeof(char) * ++postfix_size);
postfix[postfix_size - 1] = pop();
push(expression[i]);
}
i++;
}
postfix = (char*)realloc(postfix, sizeof(char) * ++postfix_size);
postfix[postfix_size - 1] = pop();
i = 0;
printf("Infix converted to postfix is :\n");
while(i < postfix_size)
printf("%c",postfix[i++]);
free(postfix);
}
int operate(int OP1, int OP2, char operand)
{
switch(operand)
{
case '+':
return OP1 + OP2;
break;
case '-':
return OP1 - OP2;
break;
case '*':
return OP1 * OP2;
break;
case '/':
return OP1/OP2;
break;
default:
printf("\nUndefined operator\n");
break;
}
return 0;
}
void main()
{
int option = 0;
while(option != 4)
{
S.top = -1;
char expression[50];
printf("\nChoose an option :\n1. Prefix evaluation\n2. Postfix evaluation\n3. Infix to Postfix\n4. Exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\nEnter the expression : ");
scanf("%s",&expression);
postfix(expression);
break;
case 2:
printf("\nEnter the expression : ");
scanf("%s",&expression);
prefix(expression, strlen(expression));
break;
case 3:
printf("\nEnter the expression : ");
scanf("%s",&expression);
infix_to_postfix(expression);
break;
case 4:
printf("\nExiting!!\n");
exit(0);
}
}
}