POSTFIX1
POSTFIX1
Roll no: 80
Assignment no :
Title: Implementation of the Program based on the POSTFIX Expression.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class EXPR
{
char INFIX[50],POSTFIX[50];
public:
EXPR();
void READ_INFIX();
void INFIX_POSTFIX();
void SHOW_INFIX();
void SHOW_POSTFIX();
int IS_OPR(char);
int PRIORITY(char);
};
EXPR::EXPR()
{
}
int EXPR::PRIORITY(char c)
{
if(c=='+' || c=='-')
return 1; //true
else
if(c=='*' || c=='/')
return 2; //true
else
return 0; //false
}
int EXPR::IS_OPR(char c)
{
if(c=='+' || c=='-' || c=='*' || c=='/')
return 1; //true
else
return 0; //false
}
void EXPR::READ_INFIX()
{
cout<<endl<<"Enter an INFIX Expression : ";
cin>>INFIX;
}
void EXPR::INFIX_POSTFIX()
{
char STK[50];
int top=0;
//step 1
strcat( INFIX, ")" );
//step 2
top=top+1;
STK[top]='(';
//step 3
int i=0,j=-1;
while(INFIX[i] !=- '\0')
{
if(isalpha( INFIX[i]) ) //case 1
{
j=j+1;
POSTFIX[j]=INFIX[i];
}
else
{
if( INFIX[i] == '(') //case 2
{
top=top+1;
STK[top]=INFIX[i];
}
else
{
if( INFIX[i] =='+' || INFIX[i] =='-' || INFIX[i] == '*' || INFIX[i] =='/')
if( IS_OPR(INFIX[i]) ) //case 3
{
//operator on stack and having higher or equal priority
while(IS_OPR(STK[top]) && PRIORITY(STK[top]) >= PRIORITY(INFIX[i]))
{
j=j+1;
POSTFIX[j]=STK[top];
top=top-1;
}
top=top+1;
STK[top]=INFIX[i];
}
else
{
if( INFIX[i] == ')') //case 4
{
while(STK[top] !='(')
{
j=j+1;
POSTFIX[j]=STK[top];
top=top-1;
}
top=top-1; //return '(' from stk
}
}
}
}
i=i+1;
}
j=j+1;
POSTFIX[j]='\0';
}
void EXPR::SHOW_INFIX()
{
}
void EXPR::SHOW_POSTFIX()
{
cout<<POSTFIX;
}
void main()
{
clrscr();
EXPR obj;
obj.READ_INFIX();
obj.INFIX_POSTFIX();
obj.SHOW_POSTFIX();
getch();
}