Infix to Postfix
Thu Aug 31 2023 18:33:18 GMT+0000 (Coordinated Universal Time)
Saved by
@Astik
#include<stdio.h>
#include<string.h>
#include<conio.h>
char st[100], post[100], s[100];
int top = -1, id = 0,i ;
void push(int x){
top ++ ;
st[top] = x ;
}
int pop (){
return st[top--];
}
int operand(char x){
return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
}
int prec(char x){
if(x == '*' || x == '/') return 2 ;
else if(x == '+' || x == '-') return 1 ;
return 0 ;
}
int main(){
int n ;
printf("Enter the infix expression : ");
scanf("%s", s);
n = strlen(s);
push('(');
s[n] = ')';
for(i = 0 ; i <= n; i ++){
if(operand(s[i])){
post[id] = s[i];
id ++ ;
} else if(s[i] == '('){
push(s[i]);
} else if(s[i] != ')') {
while(prec(st[top]) >= prec(s[i])){
post[id ++] = pop();
}
push(s[i]);
} else{
while(st[top] != '('){
post[id ++] = pop();
}
pop();
}
}
printf("Posfix = %s", post);
getch();
return 0;
}
content_copyCOPY
Comments