# Infix to Postfix using different Precedence Values for In-Stack and Out-Stack

Conversion of infix to postfix expression can be done elegantly using two precedence function. Each operator is assigned a value (larger value means higher precedence) which depends upon whether the operator is inside or outside the stack. Also the right and left associativity for different operators can be handled by varying it’s values in the two precedence functions.
Infix expression example: a+b*c
Its corresponding postfix expression: abc*+

Precedence Values Table for Operators

Examples:

Input: str = “a+b*c-(d/e+f^g^h)”
Output: abc*+de/fgh^^+-

Input: a+b*c
Output: abc*+

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. If the input character is an operand, print it.
2. If the input character is an operator-
• If stack is empty push it to the stack.
• If it’s precedence value is greater than the precedence value of the character on top, push.
• If it’s precedence value is lower then pop from stack and print while precedence of top char is more than the precedence value of the input character.
3. If the input character is ‘)’, then pop and print until top is ‘(‘. (Pop ‘(‘ but don’t print it.)
4. If stack becomes empty before encountering ‘(‘, then it’s a invalid expression.
5. Repeat steps 1-4 until input expression is completely read.
6. Pop the remaining elements from stack and print them.

The above method handles right associativity of exponentiation operator (here, ^) by assigning it higher precedence value outside stack and lower precedence value inside stack whereas it’s opposite for left associative operators.

Below is the implementation of the above approach:

## C++

 `// C++ program to convert an infix expression to a ` `// postfix expression using two precedence function ` `#include ` `using` `namespace` `std; ` ` `  ` `  `// to check if the input character ` `// is an operator or a '(' ` `int` `isOperator(``char` `input) ` `{ ` `    ``switch` `(input) { ` `    ``case` `'+'``: ` `        ``return` `1; ` `    ``case` `'-'``: ` `        ``return` `1; ` `    ``case` `'*'``: ` `        ``return` `1; ` `    ``case` `'^'``: ` `        ``return` `1; ` `    ``case` `'%'``: ` `        ``return` `1; ` `    ``case` `'/'``: ` `        ``return` `1; ` `    ``case` `'('``: ` `        ``return` `1; ` `    ``} ` `    ``return` `0; ` `} ` ` `  `// to check if the input character is an operand ` `int` `isOperand(``char` `input) ` `{ ` `    ``if` `(input >= 65 && input <= 90 ` `        ``|| input >= 97 && input <= 122) ` `        ``return` `1; ` `    ``return` `0; ` `} ` ` `  `// function to return precedence value ` `// if operator is present in stack ` `int` `inPrec(``char` `input) ` `{ ` `    ``switch` `(input) { ` `    ``case` `'+'``: ` `    ``case` `'-'``: ` `        ``return` `2; ` `    ``case` `'*'``: ` `    ``case` `'%'``: ` `    ``case` `'/'``: ` `        ``return` `4; ` `    ``case` `'^'``: ` `        ``return` `5; ` `    ``case` `'('``: ` `        ``return` `0; ` `    ``} ` `} ` ` `  `// function to return precedence value ` `// if operator is present outside stack. ` `int` `outPrec(``char` `input) ` `{ ` `    ``switch` `(input) { ` `    ``case` `'+'``: ` `    ``case` `'-'``: ` `        ``return` `1; ` `    ``case` `'*'``: ` `    ``case` `'%'``: ` `    ``case` `'/'``: ` `        ``return` `3; ` `    ``case` `'^'``: ` `        ``return` `6; ` `    ``case` `'('``: ` `        ``return` `100; ` `    ``} ` `} ` ` `  `// function to convert infix to postfix ` `void` `inToPost(``char``* input) ` `{ ` `    ``stack<``char``> s; ` `  `  `    ``// while input is not NULL iterate ` `    ``int` `i = 0; ` `    ``while` `(input[i] != ``'\0'``) { ` ` `  `        ``// if character an operand ` `        ``if` `(isOperand(input[i])) { ` `            ``printf``(``"%c"``, input[i]); ` `        ``} ` ` `  `        ``// if input is an operator, push ` `        ``else` `if` `(isOperator(input[i])) { ` `            ``if` `(s.empty() ` `                ``|| outPrec(input[i]) > inPrec(s.top())) ` `                ``s.push(input[i]); ` `            ``else` `{ ` `                ``while` `(!s.empty() ` `                       ``&& outPrec(input[i]) < inPrec(s.top())) { ` `                    ``printf``(``"%c"``, s.top()); ` `                    ``s.pop(); ` `                ``} ` `                ``s.push(input[i]); ` `            ``} ` `        ``} ` ` `  `        ``// condition for opening bracket ` `        ``else` `if` `(input[i] == ``')'``) { ` `            ``while` `(s.top() != ``'('``) { ` `                ``printf``(``"%c"``, s.top()); ` `                ``s.pop(); ` ` `  `                ``// if opening bracket not present ` `                ``if` `(s.empty()) { ` `                    ``printf``(``"Wrong input\n"``); ` `                    ``exit``(1); ` `                ``} ` `            ``} ` ` `  `            ``// pop the opening bracket. ` `            ``s.pop(); ` `        ``} ` `        ``i++; ` `    ``} ` ` `  `    ``// pop the remaining operators ` `    ``while` `(!s.empty()) { ` `        ``if` `(s.top() == ``'('``) { ` `            ``printf``(``"\n Wrong input\n"``); ` `            ``exit``(1); ` `        ``} ` `        ``printf``(``"%c"``, s.top()); ` `        ``s.pop(); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``char` `input[] = ``"a+b*c-(d/e+f^g^h)"``; ` `    ``inToPost(input); ` `    ``return` `0; ` `} `

## Java

 `import` `static` `java.lang.System.exit; ` `import` `java.util.Stack; ` ` `  `// Java program to convert an infix expression to a ` `// postfix expression using two precedence function ` `class` `GFG { ` ` `  `// to check if the input character ` `// is an operator or a '(' ` `    ``static` `int` `isOperator(``char` `input) { ` `        ``switch` `(input) { ` `            ``case` `'+'``: ` `                ``return` `1``; ` `            ``case` `'-'``: ` `                ``return` `1``; ` `            ``case` `'*'``: ` `                ``return` `1``; ` `            ``case` `'^'``: ` `                ``return` `1``; ` `            ``case` `'%'``: ` `                ``return` `1``; ` `            ``case` `'/'``: ` `                ``return` `1``; ` `            ``case` `'('``: ` `                ``return` `1``; ` `        ``} ` `        ``return` `0``; ` `    ``} ` ` `  `// to check if the input character is an operand ` `    ``static` `int` `isOperand(``char` `input) { ` `        ``if` `(input >= ``65` `&& input <= ``90` `                ``|| input >= ``97` `&& input <= ``122``) { ` `            ``return` `1``; ` `        ``} ` `        ``return` `0``; ` `    ``} ` ` `  `// function to return precedence value ` `// if operator is present in stack ` `    ``static` `int` `inPrec(``char` `input) { ` `        ``switch` `(input) { ` `            ``case` `'+'``: ` `            ``case` `'-'``: ` `                ``return` `2``; ` `            ``case` `'*'``: ` `            ``case` `'%'``: ` `            ``case` `'/'``: ` `                ``return` `4``; ` `            ``case` `'^'``: ` `                ``return` `5``; ` `            ``case` `'('``: ` `                ``return` `0``; ` `        ``} ` `        ``return` `0``; ` `    ``} ` ` `  `// function to return precedence value ` `// if operator is present outside stack. ` `    ``static` `int` `outPrec(``char` `input) { ` `        ``switch` `(input) { ` `            ``case` `'+'``: ` `            ``case` `'-'``: ` `                ``return` `1``; ` `            ``case` `'*'``: ` `            ``case` `'%'``: ` `            ``case` `'/'``: ` `                ``return` `3``; ` `            ``case` `'^'``: ` `                ``return` `6``; ` `            ``case` `'('``: ` `                ``return` `100``; ` `        ``} ` `        ``return` `0``; ` `    ``} ` ` `  `// function to convert infix to postfix ` `    ``static` `void` `inToPost(``char``[] input) { ` `        ``Stack s = ``new` `Stack(); ` ` `  `        ``// while input is not NULL iterate ` `        ``int` `i = ``0``; ` `        ``while` `(input.length!=i) { ` ` `  `            ``// if character an operand ` `            ``if` `(isOperand(input[i]) == ``1``) { ` `                ``System.out.printf(``"%c"``, input[i]); ` `            ``} ``// if input is an operator, push ` `            ``else` `if` `(isOperator(input[i]) == ``1``) { ` `                ``if` `(s.empty() ` `                        ``|| outPrec(input[i]) > inPrec(s.peek())) { ` `                    ``s.push(input[i]); ` `                ``} ``else` `{ ` `                    ``while` `(!s.empty() ` `                            ``&& outPrec(input[i]) < inPrec(s.peek())) { ` `                        ``System.out.printf(``"%c"``, s.peek()); ` `                        ``s.pop(); ` `                    ``} ` `                    ``s.push(input[i]); ` `                ``} ` `            ``} ``// condition for opening bracket ` `            ``else` `if` `(input[i] == ``')'``) { ` `                ``while` `(s.peek() != ``'('``) { ` `                    ``System.out.printf(``"%c"``, s.peek()); ` `                    ``s.pop(); ` ` `  `                    ``// if opening bracket not present ` `                    ``if` `(s.empty()) { ` `                        ``System.out.printf(``"Wrong input\n"``); ` `                        ``exit(``1``); ` `                    ``} ` `                ``} ` ` `  `                ``// pop the opening bracket. ` `                ``s.pop(); ` `            ``} ` `            ``i++; ` `        ``} ` ` `  `        ``// pop the remaining operators ` `        ``while` `(!s.empty()) { ` `            ``if` `(s.peek() == ``'('``) { ` `                ``System.out.printf(``"\n Wrong input\n"``); ` `                ``exit(``1``); ` `            ``} ` `            ``System.out.printf(``"%c"``, s.peek()); ` `            ``s.pop(); ` `        ``} ` `    ``} ` ` `  `// Driver code ` `    ``static` `public` `void` `main(String[] args) { ` `        ``char` `input[] = ``"a+b*c-(d/e+f^g^h)"``.toCharArray(); ` `        ``inToPost(input); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## C#

 `// C# program to convert an infix expression to a ` `// postfix expression using two precedence function ` `using` `System.Collections.Generic; ` `using` `System; ` `public` `class` `GFG { ` `  `  `// to check if the input character ` `// is an operator or a '(' ` `    ``static` `int` `isOperator(``char` `input) { ` `        ``switch` `(input) { ` `            ``case` `'+'``: ` `                ``return` `1; ` `            ``case` `'-'``: ` `                ``return` `1; ` `            ``case` `'*'``: ` `                ``return` `1; ` `            ``case` `'^'``: ` `                ``return` `1; ` `            ``case` `'%'``: ` `                ``return` `1; ` `            ``case` `'/'``: ` `                ``return` `1; ` `            ``case` `'('``: ` `                ``return` `1; ` `        ``} ` `        ``return` `0; ` `    ``} ` `  `  `// to check if the input character is an operand ` `    ``static` `int` `isOperand(``char` `input) { ` `        ``if` `(input >= 65 && input <= 90 ` `                ``|| input >= 97 && input <= 122) { ` `            ``return` `1; ` `        ``} ` `        ``return` `0; ` `    ``} ` `  `  `// function to return precedence value ` `// if operator is present in stack ` `    ``static` `int` `inPrec(``char` `input) { ` `        ``switch` `(input) { ` `            ``case` `'+'``: ` `            ``case` `'-'``: ` `                ``return` `2; ` `            ``case` `'*'``: ` `            ``case` `'%'``: ` `            ``case` `'/'``: ` `                ``return` `4; ` `            ``case` `'^'``: ` `                ``return` `5; ` `            ``case` `'('``: ` `                ``return` `0; ` `        ``} ` `        ``return` `0; ` `    ``} ` `  `  `// function to return precedence value ` `// if operator is present outside stack. ` `    ``static` `int` `outPrec(``char` `input) { ` `        ``switch` `(input) { ` `            ``case` `'+'``: ` `            ``case` `'-'``: ` `                ``return` `1; ` `            ``case` `'*'``: ` `            ``case` `'%'``: ` `            ``case` `'/'``: ` `                ``return` `3; ` `            ``case` `'^'``: ` `                ``return` `6; ` `            ``case` `'('``: ` `                ``return` `100; ` `        ``} ` `        ``return` `0; ` `    ``} ` `  `  `// function to convert infix to postfix ` `    ``static` `void` `inToPost(``char``[] input) { ` `        ``Stack<``char``> s = ``new` `Stack<``char``>(); ` `  `  `        ``// while input is not NULL iterate ` `        ``int` `i = 0; ` `        ``while` `(input.Length!=i) { ` `  `  `            ``// if character an operand ` `            ``if` `(isOperand(input[i]) == 1) { ` `                ``Console.Write(input[i]); ` `            ``} ``// if input is an operator, push ` `            ``else` `if` `(isOperator(input[i]) == 1) { ` `                ``if` `(s.Count==0 ` `                        ``|| outPrec(input[i]) > inPrec(s.Peek())) { ` `                    ``s.Push(input[i]); ` `                ``} ``else` `{ ` `                    ``while` `(s.Count!=0 ` `                            ``&& outPrec(input[i]) < inPrec(s.Peek())) { ` `                        ``Console.Write(s.Peek()); ` `                        ``s.Pop(); ` `                    ``} ` `                    ``s.Push(input[i]); ` `                ``} ` `            ``} ``// condition for opening bracket ` `            ``else` `if` `(input[i] == ``')'``) { ` `                ``while` `(s.Peek() != ``'('``) { ` `                    ``Console.Write(s.Peek()); ` `                    ``s.Pop(); ` `  `  `                    ``// if opening bracket not present ` `                    ``if` `(s.Count==0) { ` `                        ``Console.Write(``"Wrong input\n"``); ` `                        ``return``; ` `                    ``} ` `                ``} ` `  `  `                ``// pop the opening bracket. ` `                ``s.Pop(); ` `            ``} ` `            ``i++; ` `        ``} ` `  `  `        ``// pop the remaining operators ` `        ``while` `(s.Count!=0) { ` `            ``if` `(s.Peek() == ``'('``) { ` `                ``Console.Write(``"\n Wrong input\n"``); ` `                ``return``; ` `            ``} ` `            ``Console.Write(s.Peek()); ` `            ``s.Pop(); ` `        ``} ` `    ``} ` `  `  `// Driver code ` `    ``static` `public` `void` `Main() { ` `        ``char` `[]input = ``"a+b*c-(d/e+f^g^h)"``.ToCharArray(); ` `        ``inToPost(input); ` `    ``} ` `} ` `  `  `// This code is contributed by Rajput-Ji `

Output:

```abc*+de/fgh^^+-
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.