PROGRAM TO CONVERT INFIX TO POSTFIX EXPRESSION.

#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<process.h>

#define SIZE 50

class stack
{
char arr[SIZE];
int top;
public:
void push(char);
int findtop();
char pop();
bool isempty();
bool isfull();
void display();
char gettop();
stack()
{
top=-1;
}
};

int stack::findtop()
{
return(top);
}

bool stack::isempty()
{
return(top==-1);
}

bool stack::isfull()
{
return(top==(SIZE-1));
}

void stack::push(char ch)
{
if(isfull())
{
cout<<"\n STACK OVERFLOW";
return;
}
arr[++top]=ch;
}

char stack::pop()
{
if(isempty())
return('?');
return(arr[top--]);
}

void stack::display()
{
cout<<endl;
if(isempty())
{
cout<<" STACK EMPTY";
return;
}
for(int i=0;i<=top;i++)
cout<<arr[i];
cout<<"                "<<top;
}

char stack::gettop()
{
if(isempty())
return('?');
return(arr[top]);
}

void isvalid(char *exp)
{
int check=0,i,l;
l=strlen(exp);
if(exp[0]!='('&&!isalnum(exp[0]))
{
check=-1;
goto label;
}
if(exp[l-1]!=')'&&!isalnum(exp[l-1]))
{
check=-1;
goto label;
}
for(i=1;i<l-1;i++)
{
if(exp[i]=='(')
check++;
else if(exp[i]==')')
check--;
else if((exp[i]=='.')&&(!(isalnum(exp[i-1]))||!(isalnum(exp[i+1]))))
check=-1;
if(check<0)
{
cout<<"\n SYNTAX ERROR IN INFIX EXPRESSION"<<endl;
exit(0);
}
}
label:
if(check!=0)
{
cout<<"\n SYNTAX ERROR IN INFIX EXPRESSION"<<endl;
exit(0);
}
}

void main()
{
char infix[SIZE];
stack pfix,oper;
cout<<"\n ENTER THE INFIX EXPRESSION ::\n";
cin.getline(infix,SIZE);
isvalid(infix);
int len=strlen(infix);
for(int i=0;i<len;i++)
{
switch(infix[i])
{
case ' ': pfix.display();
oper.display();
break;
case '(':
case '$': oper.push(infix[i]);
pfix.display();
oper.display();
break;
case '/':
case '*':
case '%': while((oper.gettop()=='$')||(oper.gettop()=='/')||(oper.gettop()=='*')||(oper.gettop()=='%')&&(oper.gettop()!='?'))
{
pfix.push(oper.pop());
pfix.push(' ');
}
oper.push(infix[i]);
pfix.display();
oper.display();
break;
case '+':
case '-':
while(oper.gettop()!='('&&oper.gettop()!='?')
{
pfix.push(oper.pop());
pfix.push(' ');
}
oper.push(infix[i]);
pfix.display();
oper.display();
break;
case ')': while(oper.gettop()!='('&&!oper.isempty())
{
pfix.push(oper.pop());
pfix.push(' ');
}
oper.pop();
pfix.display();
oper.display();
break;
case '.': pfix.push(infix[i]);
break;
default:  if(!isalnum(infix[i]))
{
cout<<"\n UNDEFINED SYMBOL "<<infix[i]<<endl;
exit(0);
}
else
{
for(int j=i;isalnum(infix[j]);j++)
pfix.push(infix[j]);
i=j-1;
if(infix[j]!='.')
pfix.push(' ');
}
pfix.display();
oper.display();
}

}
while(oper.gettop()!='?'&&!oper.isempty())
{
pfix.push(oper.pop());
pfix.push(' ');
}
pfix.display();
oper.display();
cout<<endl;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s