#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXSIZE 20
void push(char x);
char pop();
int priority(char ch);
int stack[MAXSIZE];
int top=-1;
void main()
{
char exp[15],ch;
int i=0;
printf("enter the expresion: ");
gets(exp);
while(exp[i]!='\0')
{
if(isalnum(exp[i]))
{
putchar(exp[i]);
}
else if(exp[i]==')')
{
while(stack[top]!='(')
{
putchar(pop());
}
ch=pop();
}
else if(exp[i]=='(')
{
push('(');
}
else
{
while(priority(stack[top])>=priority(exp[i]))
{
putchar(pop());
}
push(exp[i]);
}
i++ ;
}
while(top!=-1)
{
putchar(pop());
}
}
void push(char x)
{
if(top==MAXSIZE-1)
{
printf("STACK IS FULL");
exit(1);
}
else
{
top++;
stack[top]=x;
}
}
char pop()
{
char x;
if(top==-1)
exit(1);
else
{ x=stack[top];
top--;
return x;
}
}
int priority(char ch)
{
if(ch=='+'||ch=='-')
return 1;
if(ch=='*'||ch=='/')
return 2;
if(ch=='^')
return 3;
if(ch=='(')
return 0;
}
Note: Need to be arranged in compiler after copied
OutPut:
enter the expresion: (a+b)*(c+d)
ab+cd+*