Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

/////STACHINPO.

H//////
#include<iostream>
#include<conio.h>
using namespace std;
class Stack
{
int top;
char *stk;
int MaxSize;
public:
Stack(int);
void Push(char);
char Pop();
int IsFull();
int IsEmpty();
char Top();
};

Stack::Stack(int m)
{
MaxSize=m ;
stk=new char[MaxSize];
top=-1;
}

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

int Stack::IsEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
void Stack::Push(char x)
{
if(IsFull())

1
{
cout<<"\n STACK iS FULL";
}
else
{
++top;
stk[top]=x;
}
}

char Stack::Pop()
{
char x;
if(IsEmpty())
{
cout<<"\n STACK IS EMPTY";
x='-1';
return x;
}
else
{
x=stk[top];
top--;
return x;
}
}

char Stack::Top()
{
char x;
if(IsEmpty())
{
cout<<"\n STACK IS EMPTY";
x='-1';
return x;
}
else
{
x=stk[top];
return x;
}
}

2
//////CONVERSION OF INFIX TO POSTFIX NOTATION //////////
#include"Stackinpo.h"
class expression
{
char *infix;
char *postfix;
int inf,pof;
public:
void readinf();
void intopost();
char nexttoken(char[],int);
void writetopost(char);
void displaypost();
int isp(char);
int icp(char);
expression(int size)
{
inf=0;
pof=0;
infix=new char[size];
postfix=new char[size];
}
};

char expression::nexttoken(char a[],int i)


{
return a[i];
}

int expression::icp( char ch)


{
int i;
switch(ch)
{
case ')':i=8;break;
case '^':i=1;break;
case '*':
case '/':i=4;break;
case '+':
case '-':i=6;break;
case '(':i=0;break;
}
return i; }

3
int expression::isp(char ch)
{
int i;
switch(ch)
{
case '^':i=2;break;
case '*':
case '/':i=3;break;
case '+':
case '-':i=5;break;
case'#':
case '(':i=8;break;
}
return i;
}

void expression::readinf()
{
cin>>infix;
}

void expression::writetopost(char x)
{
postfix[pof++]=x;
}

void expression::displaypost()
{
postfix[--pof]='\0';
cout<<postfix;
}

void expression::intopost()
{
Stack s(20);
char x,ch;
ch='#';
s.Push(ch);
for(x=nexttoken(infix,inf);x!='\0';x=nexttoken(infix,++inf))
{
if((x>='A'&&x<='Z')||(x>='a'&&x<='z')||(x>='0'&&x<='9'))writetopost(x);

4
else if(x==')')
{
for(;s.Top()!='(';s.Pop())
{
writetopost(s.Top());
}
s.Pop();
}
else
{
for(;isp(s.Top())<=icp(x);s.Pop())
{
writetopost(s.Top());
}
s.Push(x);
}
}
while(!s.IsEmpty())
{
writetopost(s.Pop());
}
}

int main()
{
expression e(20);
e.readinf();
e.intopost();
e.displaypost();
getch();
return 0;
}

You might also like