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

Code For Converting Infix Notation To Post Fix

The program takes an infix notation expression as input and converts it to postfix notation. It uses a stack to evaluate the expression based on operator precedence and pushes/pops elements to convert the infix to postfix notation, then prints the result.

Uploaded by

Tasneem Kubra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Code For Converting Infix Notation To Post Fix

The program takes an infix notation expression as input and converts it to postfix notation. It uses a stack to evaluate the expression based on operator precedence and pushes/pops elements to convert the infix to postfix notation, then prints the result.

Uploaded by

Tasneem Kubra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a program to convert infix (i.e a+b) notation into post fix (i.e ab+) notation.

#include<iostream>
#include<string>
#define MAX 20
using namespace std;c
void line();
void star();
char stk[20];
int top=-1;
void push(char oper)
{
if(top==MAX-1)
{
cout<<"stackfull!!!!";
}
else
{
top++;
stk[top]=oper;
}
}
char pop()
{
char ch;
if(top==-1)
{
cout<<"stackempty!!!!";
}
else
{
ch=stk[top];
stk[top]='\0';
top--;
return(ch);
}
return 0;
}
int priority ( char alpha )
{
if(alpha == '+' || alpha =='-')
{
return(1);
}
if(alpha == '*' || alpha =='/')
{
return(2);
}
if(alpha == '$')
{
return(3);
}
return 0;
}
string convert(string infix)
{
int i=0;
string postfix = "";
while(infix[i]!='\0')
{
if(infix[i]>='a' && infix[i]<='z'|| infix[i]>='A'&& infix[i]<='Z')
{
postfix.insert(postfix.end(),infix[i]);
i++;
}
else if(infix[i]=='(' || infix[i]=='{' || infix[i]=='[')
{
push(infix[i]);
i++;
}
else if(infix[i]==')' || infix[i]=='}' || infix[i]==']')
{
if(infix[i]==')')
{
while(stk[top]!='(')
{ postfix.insert(postfix.end(),pop());
}
pop();
i++;
}
if(infix[i]==']')
{
while(stk[top]!='[')
{
postfix.insert(postfix.end(),pop());
}
pop();
i++;
}
if(infix[i]=='}')
{
while(stk[top]!='{')
{
postfix.insert(postfix.end(),pop());
}
pop();
i++;
}
}
else
{
if(top==-1)
{
push(infix[i]);
i++;
}
else if( priority(infix[i]) <= priority(stk[top])) {
postfix.insert(postfix.end(),pop());

while(priority(stk[top]) == priority(infix[i])){
postfix.insert(postfix.end(),pop());
if(top < 0) {
break;
}
}
push(infix[i]);
i++;
}
else if(priority(infix[i]) > priority(stk[top])) {
push(infix[i]);
i++;
}
}
}
while(top!=-1)
{
postfix.insert(postfix.end(),pop());
}
cout<<"The Postfix notation is : "<<postfix; //it will print postfix conversion
return postfix;
}
int main()
{
star();
cout<<"Program objective : This program converts infix notation into post fix.\n";
star();
int cont;
string infix, postfix;
cout<<"\nEnter the infix notation : "; //enter the expression
cin>>infix;
cout<<endl;
postfix = convert(infix);
return 0;
}
void line()
{
for(int i=1; i<45; i++)
{
cout<<"-";
}
cout<<endl;
}
void star()
{
cout<<endl;
for(int i=1; i<40;i++)
{
cout<<"**";
}
cout<<endl;
}

You might also like