Conversion of Infix To Postfix Expression Program in C Plus Plus
Conversion of Infix To Postfix Expression Program in C Plus Plus
Conversion of Infix To Postfix Expression Program in C Plus Plus
SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other
interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming
languages. SoftOSet is the hub of the informative and research based articles.
Facebook Twitter
In this article we will explore the program of converting Infix expression to Postfix Expression in the C++ programming
language.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
The concept of conversion of Infix to Postfix expression is related to the subject of data structure. Before we dive to
explore the program to convert infix to postfix expression a kind advice is to first understand the concept of infix and
postfix expression first.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Without the concept of infix and postfix expression, It will be very difficult for you to understand the working of this
program.
POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
[End of If structure.]
[End of Step 2 loop]
7. Exit.
//softoset.blogspot.com
#include<iostream>
#include<string.h>
using namespace std;
#define MAX 100
class Conversion{
private:
char Exp[MAX];
char Stack[MAX];
char Post[MAX];
int Top,stop;
int i,j;
char item,sent,ch,x;
public:
void in()
{
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
cout<<"Enter the Infix Expression "<<endl;
gets(Exp);
}
void sentinel()
{
cout<<"\nEnter Sentinel : ) Bracket at the End of Expression "<<endl;
cin>>sent;
Top=strlen(Exp);
if(Top>=MAX)
{
cout<<"Overflow "<<endl;
exit(1);
}
else
{
Exp[Top]=sent;
Top=Top+1;
}
cout<<"Now ";
}
void scan()
{
stack_push('(');
i=0;
j=0;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
while(i!=strlen(Exp))
{
ch=Exp[i];
if(ch=='(')
{
stack_push(ch);
}
else if(isdigit(ch)||isalpha(ch))
{
Post[j]=ch;
j++;
}
else if(isoperator(ch)==1)
{
x=stack_pop();
while(isoperator(x)==1&&precedence(x)>=precedence(ch))
{
cout<<"x ="<<x<<endl;
Post[j]=x;
j++;
x=stack_pop();
}
stack_push(x);
stack_push(ch);
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
}
else if(ch==')')
{
x=stack_pop();
while(x!='(')
{
Post[j]=x;
j++;
x=stack_pop();
}
}
i++;
}
}
int isoperator(char op)
{
if((op=='+')||(op=='-')||(op=='*')||(op=='/')||(op=='^'))
return 1;
else
return 0;
}
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
int precedence(char op)
{
if(op=='^')
return (3);
else if(op=='*'||op=='/')
return (2);
else if(op=='+'||op=='-')
return (1);
else
return 0;
}
char stack_pop()
{
if(stop<=0)
{
cout<<"Underflow "<<endl;
exit(1);
}
else
{
item=Stack[stop];
stop=stop-1;
return item;
}
}
void stack_push(int item)
{
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if(stop>=MAX)
{
cout<<"Overflow "<<endl;
exit(1);
}
else
{
stop=stop+1;
Stack[stop]=item;
}
}
void out()
{
cout<<"Expression is "<<endl;
Top=strlen(Exp);
for(int i=0;i<Top;i++)
cout<<Exp[i]<<" ";
}
void Pout()
{
cout<<"Postfix Expression is "<<endl;
stop=strlen(Exp);
for(int i=0;i<stop;i++)
cout<<Post[i]<<" ";
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
}
};
int main()
{
Conversion obj;
obj.in();
obj.out();
obj.sentinel();
obj.out();
obj.scan();
obj.Pout();
return 0;
}
And here is the result of this program on execution we have capture it for you.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
You can clearly see that when user entered an infix expression, this program convert it to the postfix expression.
Thanks you.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Tags C plus plus programs Data Structure
Facebook Twitter
Bubble Sort Program in C Plus Plus Binary Search Program in C Plus Plus Conversion of Infix to Postfix Expression
June 06, 2020 June 06, 2020 Program in C Plus Plus
June 06, 2020
Post a Comment
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Enter your comment...
Publish Preview
Follow Us
Most Popular
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
May 04, 2020
Follow by Email
Get all latest content delivered straight to your inbox.
Email Address
Subscribe
Comments
razzaq
I have a similar interest this is my page read ...
sobia
I encourage you to read this text it is fun de...
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Categories
Articles (5)
Books (2)
C Programs (6)
C Tutorial (2)
Java (2)
Python (1)
Site Views
6,574
Main Tags
Articles Books C plus plus programs C Programs C Tutorial Data Structure Java Python
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Recents in Bitcoin
SOFTOSET FOLLOW US
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD