C++ Program to Convert Postfix to Infix Expression Using Stacks and Pointers

You are currently viewing C++ Program to Convert Postfix to Infix Expression Using Stacks and Pointers

Using this tutorial, You are going to run the C++ Program to Convert Postfix to Infix Expression Using Stacks and Pointers successfully without facing any troubles.

C++ Program to Convert Postfix to Infix Expression Using Stacks and Pointers

Source Code

#include <bits/stdc++.h>
using namespace std;
bool isOperand(char x) {
   return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
}
string infixConversion(string postfix) {
   stack<string> infix;
   for (int i=0; postfix[i]!=''; i++) {
      if (isOperand(postfix[i])) {
         string op(1, postfix[i]);
         infix.push(op);
      } else {
         string op1 = infix.top();
         infix.pop();
         string op2 = infix.top();
         infix.pop();
         infix.push("{"+op2+postfix[i]+op1 +"}");
      }
   }
   return infix.top();
}
int main() {
   string postfix = "xyae+/%";
   cout<<"The infix conversion of the postfix expression '"<<postfix<<"' is : ";
   cout<<infixConversion(postfix);
   return 0;
}

Final Thoughts

I hope this tutorial about C++ Program to Convert Postfix to Infix Expression Using Stacks and Pointers will be very helpful to you. If you face any difficulties during the program execution ask us via the comment section. Share this tutorial with others via social media.

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply