-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patharithmetic_expression_solver.c
180 lines (167 loc) · 2.85 KB
/
arithmetic_expression_solver.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
/*
Write C program which will calculate an arithmetic expression which is
given as a string. Consider the expression has no paranthesis and contains
the operators such as, +, -, * and /.
Compiler Design Lab
Program No.: 04
Code contributed by, Abhishek Sharma
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
int stack2[30], temp, length=0, indx=0, pos=0, top=-1;
char symbol, infix[20], postfix[20], stack[30];
void push(char);
void push2(int);
char pop();
int pop2();
int precedence(char);
void infix_to_postfix (char[]);
void eval_postfix(char[]);
void push (char symbol){
top = top + 1;
stack[top]=symbol;
}
char pop(){
temp = stack[top];
top = top - 1;
return temp;
}
// providing the priority to the operators while the operation is to be
// going on.
int precedence (char symbol){
int priority;
switch(symbol){
case '#' : {
priority = 0;
break;
}
case '(' :
case ')' :{
priority = 1;
break;
}
case '+' :
case '-' :{
priority = 2;
break;
}
case '*' :
case '/' :{
priority = 3;
break;
}
case '^' :{
priority = 4;
break;
}
}
return priority;
}
// converting the infix expression to postfix.
// It will help us to calculate the result easily.
void infix_to_postfix (char infix[]){
length = strlen(infix);
push ('#');
while (indx<length){
symbol = infix[indx++];
switch (symbol){
case '(' :{
push (symbol);
break;
}
case ')' :{
temp = pop();
while (temp!='('){
postfix[pos++] = temp;
temp = pop();
}
break;
}
case '-' :
case '+' :
case '*' :
case '/' :
case '^' :{
while (precedence(stack[top])>=precedence(symbol)){
temp = pop();
postfix[pos++] = temp;
}
push (symbol);
break;
}
default :{
postfix[pos++]=symbol;
break;
}
}
}
while (top>0){
temp = pop();
postfix[pos++] = temp;
postfix[pos]='\0';
}
}
void push2 (int x){
stack2[++top] = x;
}
int pop2 (){
return stack2[top--];
}
// Evaluating the postfix expression
void eval_postfix (char postfix[]){
char *temp;
int n1, n2, n3, num;
temp = postfix;
while (*temp!= '\0'){
if (isdigit(*temp)){
num = *temp - 48;
push2 (num);
}
else{
n1 = pop2();
n2 = pop2();
switch (*temp){
case '+' :
{
n3 = n1 + n2;
break;
}
case '-' :
{
n3 = n2 - n1;
break;
}
case '*' :
{
n3 = n1 * n2;
break;
}
case '/' :
{
n3 = n2 / n1;
break;
}
case '^' :
{
n3 = pow(n2,n1);
break;
}
}
push2 (n3);
}
temp++;
}
printf ("\nThe Result of the given expression is %d",pop2());
}
// Main Fuction
int main (){
printf ("Enter an Infix Expression : ");
gets (infix);
infix_to_postfix(infix);
printf ("\nThe Postfix Expression will be : ");
puts (postfix);
eval_postfix(postfix);
return 0;
}