Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views4 pages

0025 Prac 10

Uploaded by

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

0025 Prac 10

Uploaded by

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

#include<iostream>

#include<string.h>
#include<conio.h>
using namespace std;
# define Max 20

class Exp
{
private:
int top;
char stack[Max];
char postfix[Max];

public:
Exp()
{
top=-1;
memset(stack,'\0',sizeof(stack));
memset(postfix,'\0',sizeof(postfix));
}

int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(top==Max-1)
return 1;
else
return 0;
}

void push(char c)
{
if(isfull())
{
cout<<"Overflow"<<endl;
}
else
top++;
stack[top]=c;
}

char pop()
{
if(isempty())
{
cout<<"Underflow"<<endl;
}
else
{ char c=stack[top];
top--;
return c;
}
}

int precedence(char c)
{
if(c=='^')
return 3;
if(c=='/' || c=='*')
return 2;
if(c=='+' || c=='-')
return 1;
else
return 0;
}

int isOperator(char c)
{
if(c=='+'|| c=='-'|| c=='*'||c=='/'||c=='^')
return 1;
else
return 0;
}

void pretopost()
{
int i,j=0;
char infix[Max];
cout<<"Enter the infix expression"<<endl;
cin>>infix;

int n=strlen(infix);

for(i=0;i<n;i++)
{
if( ((infix[i] >= 'a') && (infix[i] <= 'z'))|| ((infix[i] >=
'A') && (infix[i] <= 'Z')))
{
postfix[j]=infix[i];
j++ ;
}
else if(infix[i]=='(')
{
push(infix[i]);
}
else if(infix[i]==')')
{
while(stack[top]!='(' && top!=-1)
{
char temp=pop();
postfix[j]=temp;
j++;
}
if(stack[top]==')')
{
pop();
}

}
else if(isOperator(infix[i]))
{
if(isempty())
{
push(infix[i]);
}
else if(precedence(infix[i])>stack[top])
{
push(infix[i]);
}
else if(precedence(infix[i])==precedence(stack[top]) &&
infix[i]=='^')
{
push(infix[i]);
}
else
{
while(precedence(stack[top])>=precedence(infix[i]) &&
(!isempty()))
{
char temp=stack[top];
postfix[j]=temp;
j++;
pop();
}
push(infix[i]);
}

while(!isempty())
{
postfix[j]=stack[top];
j++;
pop();
}

cout<<"The conversion from prefix to postfix is "<<postfix<<endl;


}
};

int main()
{
Exp e;
e.pretopost();
return 0;
}

PS C:\Atharv\Practise Practicals\FDS> cd "c:\Atharv\Practise


Practicals\FDS\" ; if ($?) { g++ infixtopostfix.cpp -o infixtopostfix } ;
if ($?) { .\infixtopostfix }
Enter the infix expression
A+B*C
The conversion from prefix to postfix is ABC*+
PS C:\Atharv\Practise Practicals\FDS>

You might also like