-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path4.InfixToPostFix.c
52 lines (52 loc) · 1.16 KB
/
4.InfixToPostFix.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
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 100
char in[100],pf[100],stk[MAX];
int top=-1;
int pre(char op){
if(op=='%'||op=='^')
return 3;
if(op=='*'||op=='/')
return 2;
return 1;
}
int isoperator(char x){
if(x=='+'||x=='-'||x=='*'||x=='/'||x=='^'||x=='%')
return 1;
return 0;
}
void push(char a){
stk[++top]=a;
}
char pop(){
return stk[top--];
}
int main(){
int i,c=0;
printf("Enter an infix expression\n");
scanf("%s",in);
for(i=0;in[i]!='\0';i++){
if(isalnum(in[i]))
pf[c++]=in[i];
else if(in[i]=='(')
push(in[i]);
else if(isoperator(in[i])){
while(pre(in[i])<pre(stk[top]))
pf[c++]=pop();
push(in[i]);
}
else if(in[i]==')'){
while(stk[top]!='(')
pf[c++]=pop();
pop();//To pop the '('
}
else{
printf("\nInvalid expression\n");
return 0;
}
}
while(top!=-1)
pf[c++]=pop();
printf("\nThe equivalent postfix expression is %s \n",pf);
}