0% found this document useful (0 votes)
18 views

Infix To Postfix Lab 3 Program

Uploaded by

SriniVasan Vasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Infix To Postfix Lab 3 Program

Uploaded by

SriniVasan Vasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

using namespace std;


const int size=20;
class Stack
{
char opstk[20];
int top;
public:
Stack()
{
top==-1;
}
void push(char);
char pop();
int isEmpty();
int isFull();
int stackTop();
};
void Stack::push(char x)
{
if(isFull())
cout<<"\n Stack is full";
else
opstk[++top]=x;
}

int Stack::isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}

int Stack::isFull()
{
if(top==size-1)
return 1;
else
return 0;
}

int Stack::stackTop()
{
if(!isEmpty())
return(opstk[top]);
else
cout<<"\n NO ELEMENT IN THE STACK";
}

char Stack::pop()
{
if(isEmpty())
return 0;
else
return opstk[top--];
}

class Conversion
{
Stack s;//COPY CONSTRUCTOR.
char postfix[20];
public:
void infixtopostfix(char []);
int prec(char,char);
int isp(char);
int icp(char);
};

int Conversion::prec(char op1,char op2)


{
if(isp(op1)>icp(op2))
return 0;
else
return 1;
}
int Conversion::isp(char op1)
{
switch(op1)
{
case '+':
return 2;
case '-':
return 2;
case '*':
return 3;
case '/':
return 3;
case '$':
return 4;
case '(':
return 0;
}
}

int Conversion::icp(char op2)


{
switch(op2)
{
case '+':
return 2;
case '-':
return 2;
case '*':
return 3;
case '/':
return 3;
case '$':
return 5;
case '(':
return 5;
}
}

void Conversion::infixtopostfix(char e[])


{
int count=0,p=0;
while(e[count]!='\0')
count++;
for(int i=0;i<count;i++)
{
if((e[i]>='A'&&e[i]<='Z')||(e[i]>='a'&&e[i]<='z'))
postfix[p++]=e[i];
else
{
while((!s.isEmpty())&&prec(s.stackTop(),e[i]))
postfix[p++]=s.pop();
if(e[i]!=')')
s.push(e[i]);
else
char d=s.pop();
}
}
while(!s.isEmpty())
{
postfix[p++]=s.pop();
}
for(int i=0;i<p;i++)
{
if(postfix[i]!='(')
cout<<postfix[i];
}
}

int main()
{
char exp[20];
Conversion c;
cout<<"\nEnter the expression:";
cin>>exp;
cout<<"\n THE POSTFIX EXPRESSION:";
c.infixtopostfix(exp);
return 0;
}

You might also like