🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In computer science, infix notation is the standard arithmetic and logical formulation we use in our day-to-day lives, where operators are written between operands. For example: A + B. On the other hand, postfix notation, also known as Reverse Polish Notation (RPN), places the operator after the operands, like A B +.
Postfix notation has the advantage that it does not require any parentheses as long as the number of operands for every operator is fixed.
In this post, we will create a C++ program to convert an infix expression to a postfix expression using a stack data structure.
2. Program Overview
Our infix to postfix converter program will:
1. Utilize a stack to hold operators and ensure they are placed in the postfix expression in the correct order.
2. Iterate over the infix expression from left to right.
3. Place operands directly to the postfix expression.
4. Use the stack to manage operators and handle the precedence between them.
5. At the end of the iteration, pop any remaining operators from the stack to the postfix expression.
3. Code Program
#include<iostream>
#include<stack>
using namespace std;
int precedence(char op) {
if(op == '^') return 3;
else if(op == '*' || op == '/') return 2;
else if(op == '+' || op == '-') return 1;
return -1;
}
string infixToPostfix(string infix) {
stack<char> s;
string postfix = "";
for(int i = 0; i < infix.length(); i++) {
char c = infix[i];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
postfix += c;
} else if(c == '(') {
s.push(c);
} else if(c == ')') {
while(!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
if(!s.empty()) s.pop();
} else {
while(!s.empty() && precedence(s.top()) >= precedence(c)) {
postfix += s.top();
s.pop();
}
s.push(c);
}
}
while(!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
}
int main() {
string infix;
cout << "Enter infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
Output:
Enter infix expression: A+B*(C^D-E) Postfix expression: ABCD^E-*+
4. Step By Step Explanation
1. The precedence function determines the priority of operators.
2. In the infixToPostfix function:
- Operands (alphabets) are directly added to the postfix expression.- Operators are pushed onto the stack, but before pushing, higher or equal precedence operators at the top of the stack are popped to the postfix expression.- Parentheses are handled by pushing '(' onto the stack and popping operators for ')' until '(' is encountered.
3. At the end of the iteration, the remaining operators on the stack are popped to the postfix expression.
4. The main function is responsible for taking input from the user and displaying the converted postfix expression.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment