#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define MAX 10
/*Working fine no issues*/
char stack[MAX];
int top = -1;
void push(char);
char pop();
void Infixtopostfix(char [],char[]);
int priority(char);
void main()
{
char infix[20],postfix[20];
printf("Enter infix expression");
scanf("%s",infix);
Infixtopostfix(infix,postfix);
printf("postfix exp = %s",postfix);
getch();
}
void Infixtopostfix(char infix[],char postfix[])
{
int i,j=0;
char temp;
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]=='(')
push(infix[i]);
else if(isdigit(infix[i])||isalpha(infix[i]))
{
postfix[j]=infix[i];
j++;
}
else if(infix[i]==')')
{
if(top==-1)
{
printf("Incorrect expression");
exit(1);
}
while((top!=-1)&&(stack[top]!='('))
{
postfix[j]=pop();
j++;
}
temp = pop();
}
else if(infix[i]=='+'||infix[i]=='-'||infix[i]=='/'||infix[i]=='*')
{
while((top!=-1)&&(stack[top]!='(')&&(priority(stack[top]>=priority(infix[i]))))
{
postfix[j]=pop();
j++;
}
push(infix[i]);
}
else
{
printf("Incorrect expression");
exit(1);
}
}
while(top!=-1)
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
}
void push(char ch)
{
top++;
stack[top]=ch;
}
char pop()
{
char val;
val = stack[top];
top = top-1;
return val;
}
int priority(char ch)
{
if(ch=='*'||ch=='/')
return 1;
if(ch=='+'||ch=='-')
return 0;
}