forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaininfixtopostfixandevalution.c
200 lines (177 loc) · 3.57 KB
/
maininfixtopostfixandevalution.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
#include<stdio.h>
#include<stdlib.h>
#define N 20
#define M 80
char val;
int isoprator(char ch)
{
if(ch=='^'||ch=='*'||ch=='/'||ch=='+'||ch=='-')
return(1);
else
return(0);
}
int isoprand(char ch)
{
if((ch>='0'&&ch<='9'
)||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return(1);
else
return(0);
}
int prac(char ch)
{
if(ch=='^')
return(3);
else if(ch=='*'||ch=='/')
return(2);
else if(ch=='+'||ch=='-')
return(1);
else
return(0);
}
void push(char s[],int *top,char val)
{
if(*top<(N-1))
{
(*top)++;
s[*top]=val;
}
else
printf("stack is full\n");
}
char pop(char s[],int *top)
{
if((*top)>-1)
{
val=s[*top];
(*top)--;
return val;
}
else
printf("stack is empty\n");
return(-1);
}
int read(char s[],int top,int i)
{
if((top-i+1)>=0)
return s[top-i+1];
else
printf("not found\n");
}
int isempty(char s[],int top)
{
if(top==-1)
return(1);
else
return(0);
}
void infixtopostfix(char *i,char *p)
{
char s[N];int top=-1;
while(*i!='\0')
{
if(isoprand(*i))
{
*p=*i;
*i++;
*p++;
}
else if (*i=='(')
{
push(s,&top,'(');
*i++;
}
else if(*i==')')
{
while(read(s,top,1)!='(')
{
*p=pop(s,&top);
*p++;
}
*i++;
pop(s,&top);
}
else if(isempty(s,top)||read(s,top,1)=='('||prac(*i)>prac(read(s,top,1)))
{
push(s,&top,*i);
*i++;
}
else
{
*p=pop(s,&top);
*p++;
}
}
while(!isempty(s,top))
{
*p=pop(s,&top);
*p++;
}
}
int evaluate(char *p)
{
char s[N];
int top=-1;
int a=0,b=0,val=0,num,ans=0;
push(s,&top,'(');
while(*p!='\0')
{
if(isoprator(*p))
{
b=pop(s,&top);
a=pop(s,&top);
switch(*p)
{
case '+':
val=a+b;
push(s,&top,val);
*p++;
break;
case '-':
val=a-b;
push(s,&top,val);
*p++;
break;
case '*':
val=a*b;
push(s,&top,val);
*p++;
break;
case '/':
val=a/b;
push(s,&top,val);
*p++;
break;
case '^':
val=a^b;
push(s,&top,val);
*p++;
break;
}
}
else
{
num=*p-'0';
push(s,&top,num);
*p++;
}
}
ans=pop(s,&top);
return ans;
}
void main()
{ int ans;
char infix[M]={};
char postfix[M]={};
printf("Infix Expression = ");
scanf("%s",infix);
infixtopostfix(infix,postfix);
printf("Postfix Expression = %s",postfix);
ans=evaluate(postfix);
printf("\nAnswer of Postfix Evalution = %d",ans);
}
/* ""OUTPUT""
Infix Expression = 8+9*7
Postfix Expression = 897*+
Answer of Postfix Evalution = 71
*/