-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfixpostfix.c
111 lines (105 loc) · 1.62 KB
/
infixpostfix.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
#include<stdio.h>
#include<math.h>
#include<ctype.h>
char s[100];
int i,top=-1;
void push(char x){
s[++top]=x;
}
char pop(){
if(top==-1)
return -1;
else{
return s[top--];
}
}
void print(){
for(i=top;i>=0;i--)
printf("%c",s[i]);
}
int priority(char x){
switch(x) {
case '^': return 3;
case '*':
case '/': return 2;
case '+':
case '-': return 1;
}
return -1;
}
int post_eval( char post[],int n){
int A[20],j=0,m,x,y,op,res;
i=0;
while(post[i]!='#'){
if(isalpha(post[i])){
printf("Enter value for %c:",post[i]);
scanf("%d",&A[j]);j++;
}
i++;
}
i=0;m=j;j=0;
while(post[i]!='#'){
if(isalpha(post[i])){
push(A[j]);j++;
}
else{
op=post[i];
y=pop();x=pop();
switch(op){
case '+':res=x+y;break;
case '-':res=x-y;break;
case '*':res=x*y;break;
case '/':res=x/y;break;
case '^':res=pow(x,y);break;
}
push(res);
}
i++;
}
return pop();
}
int main(){
char exp[100],post[100];
i=0;
char *p,x,x1;
printf("Enter expression\n");
scanf("%s",exp);
p=exp;
printf("The postfix expression is:\n");
while(*p!='\0'){
if(isalnum(*p)){
printf("%c",*p);
post[i]=*p;
i++;
}
else if(*p=='(')
push(*p);
else if(*p==')'){
x=pop();
while(x !='('){
printf("%c",x);
post[i]=x;i++;
x=pop();
}
}
else{
while(priority(s[top]) >=priority(*p)){
x1=pop();
printf("%c",x1);
post[i]=x1;i++;
}
push(*p);
}
p++;
}
while(top!=-1){
x1=pop();
printf("%c",x1);
post[i]=x1;i++;
}
printf("\n");
int n=i;
post[n]='#';
int result = post_eval(post,n);
printf("The result of the expression is = %d",result);
}