- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Postfix to Infix in C++
- Convert Infix to Postfix Expression
- How to Create a Currency Converter in JavaScript?
- How to create a weight converter with HTML and JavaScript?
- How to create a temperature converter with HTML and JavaScript?
- How to create a length converter with HTML and JavaScript?
- How to create a speed converter with HTML and JavaScript?
- ASCII to hex and hex to ASCII converter class in JavaScript
- Temperature converter using JavaScript
- Prefix to Infix Conversion in C++
- Convert Infix to Prefix Expression
- How to install Any Video Converter on Windows?
- Prefix to Postfix Conversion in C++
- How to create arrays in JavaScript?
- How to create cookies in JavaScript?
