How to create infix to postfix converter in JavaScript?


An infix to Postfix converter is a tool that converts an infix expression to a postfix expression. In this tutorial, we will build the infix to postfix converter using JavaScript.

What is an infix expression?

Infix expression is an expression in which the operators are between the operands. For example, the expression "3 + 4" is an infix expression.

What is a postfix expression?

A postfix expression is an expression in which the operators are after the operands. For example, the expression "3 4 +" is a postfix expression.

How does the Infix to Postfix converter work?

The converter first converts the infix expression to Reverse Polish Notation (RPN using Shunting-yard algorithm. In RPN, the operators are after the operands. The Reverse Polish Notation is simply the postfix expression.

Let us consider the infix expression "3 + 4".

The expression is converted to RPN. This can be done using the Shunting-yard algorithm.

The steps are as follows −

  • 3 is pushed to the output queue.

  • The + is an operator, so it is added to the operator stack.

  • 4 is pushed to the output queue

  • The + operator is popped off the operator stack and added to output queue.

Therefore, the postfix expression for "3 + 4" is "3 4 +".

Example

Creating infix to postfix converter

The full working HTML code for the Infix to Postfix converter is given below.

<!DOCTYPE html> <html> <head> <title>Infix to Postfix Converter</title> <script> function convertToPostfix(infix) { var output = ""; var stack = []; for (var i = 0; i < infix.length; i++) { var ch = infix.charAt(i); if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { while (stack.length != 0 && stack[stack.length - 1] != '(' && getPrecedence(ch) <= getPrecedence(stack[stack.length - 1])) { output += stack.pop(); output += ' '; } stack.push(ch); } else if (ch == '(') { stack.push(ch); } else if (ch == ')') { while (stack.length != 0 && stack[stack.length - 1] != '(') { output += stackHTML.pop(); output += ' '; } stack.pop(); } else { output += ch; } } while (stack.length != 0) { output += stack.pop(); output += ' '; } return output; } function getPrecedence(ch) { if (ch == '+' || ch == '-') { return 1; } else if (ch == '*' || ch == '/') { return 2; } else { return 0; } } </script> </head> <body> <h1>Infix to Postfix Converter</h1> <p>This converter converts an infix expression to a postfix expression.</p> <form> Infix Expression: <input type="text" id="infix"> <input type="button" value="Convert" onclick="var result = convertToPostfix(document.getElementById('infix').value); document.getElementById('postfix').value = result;"> <br><br> Postfix Expression: <input type="text" id="postfix" readonly> </form> </body> </html>

Application of Infix to Postfix converter

This converter can be used to convert complex infix expressions to postfix expressions. This can be helpful in solving mathematical problems or in programming languages that use postfix expressions. Infix to Postfix Converter is a useful tool that can be used to convert complex infix expressions to postfix expressions.

Updated on: 03-Aug-2022

874 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements