Convert Infix to Postfix Expression

Mathematical ProblemsData StructureAlgorithms

Infix expressions are readable and solvable by humans. We can easily distinguish the order of operators, and also can use the parenthesis to solve that part first during solving mathematical expressions. The computer cannot differentiate the operators and parenthesis easily, that’s why postfix conversion is needed.

To convert infix expression to postfix expression, we will use the stack data structure. By scanning the infix expression from left to right, when we will get any operand, simply add them to the postfix form, and for the operator and parenthesis, add them in the stack maintaining the precedence of them.

Note: Here we will consider only {+, −,∗,/, ^} operators, other operators are neglected.

Input and Output

The infix expression. x^y/(5*z)+2
Postfix Form Is: xy^5z*/2+



Input − Infix expression.

Output − Convert infix expression to postfix form.

   initially push some special character say # into the stack
   for each character ch from infix expression, do
      if ch is alphanumeric character, then
         add ch to postfix expression
      else if ch = opening parenthesis (, then
         push ( into stack
      else if ch = ^, then            //exponential operator of higher precedence
         push ^ into the stack
      else if ch = closing parenthesis ), then
         while stack is not empty and stack top ≠ (,
            do pop and add item from stack to postfix expression

         pop ( also from the stack
         while stack is not empty AND precedence of ch <= precedence of stack top element, do
            pop and add into postfix expression

         push the newly coming character.

   while the stack contains some remaining characters, do
      pop and add to the postfix expression
   return postfix


#include<locale>      //for function isalnum()
using namespace std;

int preced(char ch) {
   if(ch == '+' || ch == '-') {
      return 1;              //Precedence of + or - is 1
   }else if(ch == '*' || ch == '/') {
      return 2;            //Precedence of * or / is 2
   }else if(ch == '^') {
      return 3;            //Precedence of ^ is 3
   }else {
      return 0;

string inToPost(string infix ) {
   stack<char> stk;
   stk.push('#');               //add some extra character to avoid underflow
   string postfix = "";         //initially the postfix string is empty
   string::iterator it;

   for(it = infix.begin(); it!=infix.end(); it++) {
         postfix += *it;      //add to postfix when character is letter or number
      else if(*it == '(')
      else if(*it == '^')
      else if(*it == ')') {
         while( != '#' && != '(') {
            postfix +=; //store and pop until ( has found
         stk.pop();          //remove the '(' from stack
      }else {
         if(preced(*it) > preced(
            stk.push(*it); //push if precedence is high
         else {
            while( != '#' && preced(*it) <= preced( {
               postfix +=;        //store and pop until higher precedence is found

   while( != '#') {
      postfix +=;        //store and pop until stack is not empty.

   return postfix;

int main() {
   string infix = "x^y/(5*z)+2";
   cout << "Postfix Form Is: " << inToPost(infix) << endl;


Postfix Form Is: xy^5z*/2+
Published on 11-Jul-2018 13:10:23