#include<stdio.h>
#include<conio.h>
#include<string.h>
#define divide 1
#define multiply 2
#define modulo 3
#define add 4
#define sub 5
#define rightparen 6
#define leftparen 7
#define blank 8
#define tab 9
#define arnold 10
#define operand 11
#define power 12
int get(char);
int put(char);
void main()
{
int i,top=-1,k=0,l,l1;
char infix[20],postfix[20],stack[20];
clrscr();
printf("\nenter the infix notation ");
gets(infix);
l=strlen(infix);
top++;
stack[top]='(';
for(i=0;i<l;i++)
{
if((put(infix[i]))!=blank && (put(infix[i]))!=tab && (put(infix[i]))!=arnold)
{
switch(get(infix[i]))
{
case leftparen:
top++;
stack[top]=infix[i];
break;
case rightparen:
while(stack[top]!='(')
{
postfix[k]=stack[top];
top--;
k++;
}
top--;
break;
case power:
top++;
stack[top]=infix[i];
break;
case divide:
while(stack[top]=='*' || stack[top]=='%')
{
postfix[k]=stack[top];
k++;
top--;
}
top++;
stack[top]=infix[i];
break;
case multiply:
while(stack[top]=='/' || stack[top]=='%')
{
postfix[k]=stack[top];
k++;
top--;
}
top++;
stack[top]=infix[i];
break;
case modulo:
while(stack[top]=='/' || stack[top]=='*')
{
postfix[k]=stack[top];
k++;
top--;
}
top++;
stack[top]=infix[i];
break;
case add:
while(stack[top]=='-' || stack[top]=='/' || stack[top]=='*' || stack[top]=='%')
{
postfix[k]=stack[top];
top--;
k++;
}
top++;
stack[top]=infix[i];
break;
case sub:
while(stack[top]=='+' || stack[top]=='/' || stack[top]=='*' || stack[top]=='%')
{
postfix[k]=stack[top];
top--;
k++;
}
top++;
stack[top]=infix[i];
break;
default:
postfix[k]=infix[i];
k++;
}
}
}
while(stack[top]!='(')
{
postfix[k]=stack[top];
top--;
k++;
}
printf("\nthe infix expression is= %s",infix);
l1=strlen(postfix);
printf("\n");
printf("\nthe postfix expression is= ");
for(i=0;i<l1;i++)
printf("%c",postfix[i]);
getch();
}
int get(char symbol)
{
if(symbol=='(')
return(leftparen);
if(symbol==')')
return(rightparen);
if(symbol=='^')
return(power);
if(symbol=='/')
return(divide);
if(symbol=='*')
return(multiply);
if(symbol=='+')
return(add);
else if(symbol=='-')
return(sub);
else
return(operand);
}
int put(char symbol1)
{
if(symbol1=='\0')
return(blank);
if(symbol1=='\t')
return(tab);
else if(symbol1=='\o')
return(arnold);
}