
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Prefix to Infix Conversion in C++
In this problem, we are given a prefix expression. Our task is to print the infix conversion of the given expression.
Prefix expression is those expressions which have operators before the operands.
Example: +AB.
Infix expressions are those expressions which have operators between the operands.
Example: A+B
Infix expression are information for human understanding, but the computer does computations on prefix or postfix expressions (generally postfix).
Let’s take an example to understand the problem
Input: prefix : /+LM/NX Output: infix : (L+M) / (N/X)
To solve this problem, we will be using the stack data structure. We will traverse the prefix expression in reverse order of the expression. And for each element of the expression check for these cases.
If element is operand -> push(element) in stack.
If element is operator -> 2Xpop(topofstack) and push in order as a string = operand - operator - operand.
Finally, after traversal, the top of the stack will contain a string which is the infix conversion, print it.
Program to show the implementation of our solution
Example
#include <iostream> #include <stack> using namespace std; bool isOperator(char element) { switch (element) { case '+': case '-': case '/': case '*': return true; } return false; } string convertToInfix(string prefix) { stack<string> expression; int length = prefix.size(); for (int i = length - 1; i >= 0; i--) { if (isOperator(prefix[i])) { string op1 = expression.top(); expression.pop(); string op2 = expression.top(); expression.pop(); string temp = "{"+op1+prefix[i]+op2+"}"; expression.push(temp); } else { expression.push(string(1, prefix[i])); } } return expression.top(); } int main() { string prefix = "*-AB/+CD*XY"; cout<<"Prefix expression : "<<prefix<<endl; cout<<"Infix expression : " <<convertToInfix(prefix); return 0; }
Output
Prefix expression : *-AB/+CD*XY Infix expression : {{A-B}*{{C+D}/{X*Y}}}
- Related Articles
- Convert Infix to Prefix Expression
- Prefix to Postfix Conversion in C++
- Postfix to Infix in C++
- Convert Infix to Postfix Expression
- How to create infix to postfix converter in JavaScript?
- K Prefix in Python
- Binary to BCD conversion in 8051
- BCD to binary conversion in 8051
- Hex to ASCII conversion in 8051
- Bool to int conversion in C++
- Object to Map conversion in JavaScript
- Number to word conversion
- MySQL add “prefix” to every column?
- Longest Common Prefix in Python
- Longest Happy Prefix in C++
