Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>fix</title>
- </head>
- <body>
- <form action="#">
- <div>
- <h1>Infix to Postfix Conversion</h1>
- <input type="text" id="ex" placeholder="Enter an expression" />
- <button id="postToInfixBtn" onclick="postToIn()">Convert to Infix</button>
- <button id="infixToPostfixBtn" onclick="inToPost()">Convert to Postfix</button>
- <div id="result"></div>
- </div>
- </form>
- <script src="fix.js"></script>
- </body>
- </html>
- --------------------------------------script---------------------------------------
- class Stack {
- constructor() {
- this.myList = []
- }
- isEmpty() {
- return this.myList.length == 0;
- }
- push(value) {
- this.myList.push(value)
- }
- pop() {
- if (this.isEmpty()) {
- return " ";
- } else {
- return this.myList.pop();
- }
- }
- peek() {
- return this.myList[this.myList.length - 1];
- }
- stackDisplay() {
- for (let i = this.myList.length - 1; i >= 0; i--) {
- console.log(this.myList[i], " ");
- }
- }
- }
- const precedence = {
- "^": 3,
- "*": 2,
- "/": 2,
- "+": 1,
- "-": 1
- };
- function infixToPostfix(str) {
- let output = " ";
- let myStack = new Stack();
- let ch;
- for (let i = 0; i < str.length; i++) {
- ch = str.charAt(i);
- if (ch === " ") {
- continue;
- } else if (ch === "(") {
- myStack.push(ch);
- } else if (ch === ")") {
- while (!myStack.isEmpty() && myStack.peek() !== "(") {
- output += myStack.pop();
- }
- myStack.pop();
- } else if (isOperator(ch)) {
- while (!myStack.isEmpty() && precedence[ch] <= precedence[myStack.peek()]) {
- output += myStack.pop();
- }
- myStack.push(ch);
- } else {
- output += ch;
- }
- }
- while (!myStack.isEmpty()) {
- output += myStack.pop();
- }
- return output;
- }
- function isOperator(c) {
- return c === "^" || c === "*" || c === "/" || c === "+" || c === "-";
- }
- function checkPriority(c) {
- if (c == "+" || c == "-") {
- return 1;
- } else if (c == "*" || c == "/") {
- return 2;
- } else if (c == "^") {
- return 3;
- } else {
- return 0;
- }
- }
- function postFixToInFix(str) {
- let output = "";
- let myStack = new Stack();
- let ch;
- let temp1, temp2;
- for (let i = 0; i < str.length; i++) {
- ch = str.charAt(i);
- if (checkOperator(ch)) {
- temp1 = myStack.pop();
- temp2 = myStack.pop();
- myStack.push("(" + temp2 + ch + temp1 + ")");
- } else {
- myStack.push(ch);
- }
- }
- output = myStack.pop();
- return output;
- }
- function checkOperator(c) {
- if (c == "+" || c == "-" || c == "*" || c == "/" || c == "^") {
- return true;
- } else {
- return false;
- }
- }
- function postToIn() {
- let text = document.getElementById("ex").value;
- console.log(postFixToInFix(text));
- let re = document.getElementById("result");
- re.innerHTML = postFixToInFix(text);
- }
- function inToPost() {
- let text = document.getElementById("ex").value;
- console.log(infixToPostfix(text));
- let re = document.getElementById("result");
- re.innerHTML = infixToPostfix(text);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement