CONVERT INFIX TO POST FIX using c++

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//PROGRAM TO CONVERT INFIX TO POST FIX EXPRESSION

//PROGRAM TO EVALUATE THE POSTFIX EXPRESSION
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
class postfix
{
char exp[50];
float stack[50],stackp[50];
int top1,top2,i;
public:
postfix();
void getdata();
void display();
void convert();
int priority(char c);
};

postfix::postfix()
{
top1=-1;
top2=-1;
i=0;
}

void postfix::getdata()
{
cout<<“Enter the infix expression”;
cin>>exp;
}

void postfix::convert()
{
char c;
while(exp[i]!=’\0′)
{
c=exp[i];
if(isdigit(c)||isalpha(c))
{
top2++;
stack[top2]=c;
}
if(c=='(‘)
{
top++;
stack[top1]=c;
}
if(c==’)’)
{ While(stack[top1]!='(‘)
{
top2++;
stackp[top2]=stack[top1];
top1–;
}
if(stack[top1]='(‘)
top1–;
}

if(c==’/’||c==’+’||c==’-‘||c==’*’||c==’^’)
{
int p1=priority(c);
char tt=stack[top1];
int p2=priority(tt);
if(p2==p1)
{
top2++;
stackp[top2]=stack[top1];
stack[top1]=c;
}
else
{
i to po
if(p2>p1)
{
top2++;
stackp[top2]=stack[top1];
stack[top1]=c;
}
else
{
top1++;
stack[top1]=c;
}
}
}
i++;
}
if(exp[i]==’\0′)
{
while(top1!=-1)]
{
top2++;
stackp[top2]=stack[top1];
top1–;
}
top2++;
stackp[top2]=’\0′;

}
}
void postfix::display()
{
cout<<“\postfix expression is”<<stackp;
}
int postfix::priority(char c)
{
if(c==’^’)
return 3;
else
{
if(c==’/’||c==’*’)
return 2;
else
{
if(c==’+’||c==’-‘)
return1;
else
return 0;
}
}
}

void main()
{
clrscr();
postfix p;
p.getdata();
p.convert();
p.display();
getch();
}

====================OUTPUT====================

Enter the infix expression (A+(B*(C/D)))^E
postfix expression is: A B C D / *+ E ^

2 thoughts on “CONVERT INFIX TO POST FIX using c++

  1. If an activity or experience is critical to student success, for example, revising an assignment with a writing tutor, or participating in an internship, consider requiring it. ,

Leave a Reply

Your email address will not be published. Required fields are marked *