#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<ctype.h>
int top=-1;
char stack[100];
void push(char);
char pop();
int prior(char);
void main()
{
clrscr();
char exp[100];
char *e,x;
cout<<"\nEnter Infix Expression\n";
cin>>exp;
strcat(exp,")");
e=exp;
push('(');
while(*e!='\0')
{
if(isalnum(*e))
cout<<*e;
else if(*e=='(')
push(*e);
else if(*e==')')
{
while( (x=pop()) !='(')
{
cout<<x;
}
}
else if((prior(stack[top])) >=
(prior(*e)))
{
while ( (prior(stack[top])) >=
(prior(*e)) )
{
cout<<pop();
}
push(*e);
}
else if((prior(stack[top])) <
(prior(*e)))
push(*e);
e++;
}
//while(top!=-1)
//
cout<<pop();
getch();
}
void push(char x)
{
top++;
stack[top]=x;
}
char pop()
{
char x;
// if(top<1)
//
cout<<"\nStack Underflow";
if(top==-1)
return -1;
else{
x=stack[top];
top--;
}
return x;
}
int prior(char y)
{
if(y=='^')
return 3;
else if(y=='*' || y=='/')
return 2;
else if(y=='+' || y=='-')
return 1;
else
return 0;
}
Comments
Post a Comment