#include<stdio.h>
#include<string.h>
char stack[50];
int top=-1;
void push(char symb)
{
stack[++top]=symb;
}
char pop()
{
return(stack[top--]);
}
int preced(char x)
{
if(x == '#')
return -1;
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
if(x == '$' || x == '^')
return 3;
}
void post(char infix[])
{
int i;
char symbol,temp;
char postfix[40];
push('#');
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
while((temp=pop())!='(')
{
printf("%c",temp);
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
printf("%c",temp);
}
push(symbol);
break;
default: printf("%c",symbol);
break;
}
}
while(top>0)
{
temp=pop();
printf("%c",temp);
}
return;
}
void main()
{
char infix[25];
printf("\nENTER THE INFIX EXPRESSION ");
scanf("%s",infix);
post(infix);
getch();
}
0 Comments