-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix.c
158 lines (158 loc) · 3.22 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
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
//Write a program to evaluate infix expression using Stacks.
// header files
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
//Global variables
int numbers[50],tn=-1,to=-1;
char op[50];
//function to push digits
void push_num(int n)
{
numbers[++tn]=n;
}
//function to push operators
void push_op(char ch)
{
op[++to]=ch;
}
// function to pop digits
int pop_num()
{
return numbers[tn--];
}
//function to pop operators
char pop_op()
{
return op[to--];
}
//evaluating the expression
int infix_eval(int numbers[50], char op[50])
{
int x, y;
char ope;
//taking first two operands
x=pop_num();
y=pop_num();
//taking the operators between them
ope=pop_op();
//executing the operation
switch(ope)
{
case '+':
return x+y;
case '-':
return y-x;
case '*':
return x*y;
case '/':
if(x==0)
{
printf("\n cannot divide by 0");
exit(0);
}
else
return y/x;
}
return 0;
}
//Function to check whether the character is an operator or not
int is_operator(char ch)
{
return (ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^');
}
//the precedence of the operators
int precedence (char c)
{
switch (c)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
//function to evaluate an infix expression
int eval(char exp[20])
{
int i, num, output,r;
char c;
for (i=0;exp[i]!='\0';i++)
{
//taking each character from the expression
c = exp[i];
//checking if it is number
if(isdigit(c)!=0)
{
num = 0;
while (isdigit(c))
{
num = num*10 + (c-'0');
i++;
if (i<strlen(exp))
c = exp[i];
else
break;
}
i--;
//pushing the number into stack of numbers
push_num(num);
}
else if(c=='(')
{
//pushing the operators into the stack
push_op(c);
}
//If we get close bracket, evaluate the entire brackets
else if(c==')')
{
while (op[to]!='(')
{
r = infix_eval (numbers, op);
//pushing the result back to stack
push_num(r);
}
pop_op();
}
//if the current character is operator
else if (is_operator(c))
{
//evaluating the expression
while (to!=-1 && precedence (c)<= precedence(op[to]))
{
output = infix_eval(numbers,op);
//pushing the result back to stack
push_num(output);
}
//pushing the current operator to stack
push_op(c);
}
}
//if there is any remaining expression, evaluate them
while(to!=-1)
{
output = infix_eval(numbers,op);
//pushing it back to stack
push_num(output);
}
return pop_num();
}
//Main function
int main()
{
char exp[50];
//taking the expression to evaluate
printf("Enter the infix expression to evaluate:");
gets(exp);
//calling the function and printing the result
printf("%d",eval(exp));
return 0;
}