How can you convert an infix notation to postfix notation?

1 answer

Answer

1220505

2026-04-26 01:45

+ Follow

#include <iOStream>

#include <stack>

using namespace std;

int prec (char ch){

// Gives precedence to different operators

switch (ch) {

case '^':

return 5;

case '/':

return 4;

case '*':

return 4;

case '+':

return 2;

case '-':

return 1;

default :

return 0;

}

}

bool isOperand(char ch){

// Finds out is a character is an operand or not

if ((ch>='0' && ch<='9') (ch>='a' && ch<='z'))

return true;

else

return false;

}

string postFix (string infix){

string pfix = "";

stack<char> opstack;

for (int i=0; i<infix.length(); i++){

// Scan character by character

if (isOperand(infix[i]))

{

pfix += infix[i];

}

else if (infix[i] ')')

{

// Retrace to last ( closure

while (opstack.top() != '(')

{

pfix += opstack.top();

opstack.pop();

}

// Remove the '(' found by while loop

opstack.pop();

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.