// program for conversion of infix expression to postfix expression
PROGRAM
#include<stdio.h>
#define MAX 100
char stack[MAX];
int top=-1;
void push(char ch)
{
if (top>= MAX -1)
printf("stack Overflow\n");
else
stack [++top]=ch;
}
char pop()
{
if (top<0)
{
printf("Stack underflow\n");
return -1;
}
else
{
return stack[top--];
}
}
int precedence(char operator)
{
switch(operator)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
default : return 0;
}
}
int main()
{
char infix[MAX], postfix[MAX], ch ,temp;
int i=0, j=0;
printf("Enter the infix expression: ");
scanf("%s",&infix);
while ((ch = infix[i++]) !='\0')
{
if(isalnum(ch))
postfix[j++]=ch;
else if (ch=='(')
push(ch);
else if (ch ==')')
{
while((temp =pop()) !='(')
postfix[j++]=temp;
}
else
{
while ( top !=-1 && precedence(stack[top]) >=precedence(ch))
postfix[j++]=pop();
push(ch);
}
}
while (top!= -1)
postfix[j++]=pop();
postfix [j] ='\0';
printf("postfix expression :%s\n",postfix);
return 0;
}
OUTPUT