Advertisement
zXenoc

Stack

Apr 9th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.61 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>fix</title>
  7. </head>
  8.  
  9. <body>
  10.     <form action="#">
  11.         <div>
  12.             <h1>Infix to Postfix Conversion</h1>
  13.             <input type="text" id="ex" placeholder="Enter an expression" />
  14.             <button id="postToInfixBtn" onclick="postToIn()">Convert to Infix</button>
  15.             <button id="infixToPostfixBtn" onclick="inToPost()">Convert to Postfix</button>
  16.             <div id="result"></div>
  17.         </div>
  18.     </form>
  19.  
  20.     <script src="fix.js"></script>
  21. </body>
  22. </html>
  23.  
  24. --------------------------------------script---------------------------------------
  25. class Stack {
  26.     constructor() {
  27.         this.myList = []
  28.     }
  29.  
  30.     isEmpty() {
  31.         return this.myList.length == 0;
  32.     }
  33.  
  34.     push(value) {
  35.         this.myList.push(value)
  36.     }
  37.  
  38.     pop() {
  39.         if (this.isEmpty()) {
  40.             return " ";
  41.         } else {
  42.             return this.myList.pop();
  43.         }
  44.     }
  45.  
  46.     peek() {
  47.         return this.myList[this.myList.length - 1];
  48.     }
  49.  
  50.     stackDisplay() {
  51.         for (let i = this.myList.length - 1; i >= 0; i--) {
  52.             console.log(this.myList[i], " ");
  53.         }
  54.     }
  55.  
  56. }
  57.  
  58. const precedence = {
  59.     "^": 3,
  60.     "*": 2,
  61.     "/": 2,
  62.     "+": 1,
  63.     "-": 1
  64. };
  65.  
  66. function infixToPostfix(str) {
  67.     let output = " ";
  68.     let myStack = new Stack();
  69.     let ch;
  70.  
  71.     for (let i = 0; i < str.length; i++) {
  72.         ch = str.charAt(i);
  73.  
  74.         if (ch === " ") {
  75.             continue;
  76.         } else if (ch === "(") {
  77.             myStack.push(ch);
  78.         } else if (ch === ")") {
  79.             while (!myStack.isEmpty() && myStack.peek() !== "(") {
  80.                 output += myStack.pop();
  81.             }
  82.             myStack.pop();
  83.         } else if (isOperator(ch)) {
  84.             while (!myStack.isEmpty() && precedence[ch] <= precedence[myStack.peek()]) {
  85.                 output += myStack.pop();
  86.             }
  87.             myStack.push(ch);
  88.         } else {
  89.             output += ch;
  90.         }
  91.     }
  92.  
  93.     while (!myStack.isEmpty()) {
  94.         output += myStack.pop();
  95.     }
  96.  
  97.     return output;
  98. }
  99.  
  100. function isOperator(c) {
  101.     return c === "^" || c === "*" || c === "/" || c === "+" || c === "-";
  102. }
  103.  
  104. function checkPriority(c) {
  105.     if (c == "+" || c == "-") {
  106.         return 1;
  107.     } else if (c == "*" || c == "/") {
  108.         return 2;
  109.     } else if (c == "^") {
  110.         return 3;
  111.     } else {
  112.         return 0;
  113.     }
  114. }
  115.  
  116. function postFixToInFix(str) {
  117.     let output = "";
  118.     let myStack = new Stack();
  119.     let ch;
  120.     let temp1, temp2;
  121.  
  122.     for (let i = 0; i < str.length; i++) {
  123.         ch = str.charAt(i);
  124.  
  125.         if (checkOperator(ch)) {
  126.             temp1 = myStack.pop();
  127.             temp2 = myStack.pop();
  128.  
  129.             myStack.push("(" + temp2 + ch + temp1 + ")");
  130.         } else {
  131.             myStack.push(ch);
  132.         }
  133.     }
  134.  
  135.     output = myStack.pop();
  136.     return output;
  137. }
  138.  
  139. function checkOperator(c) {
  140.     if (c == "+" || c == "-" || c == "*" || c == "/" || c == "^") {
  141.         return true;
  142.     } else {
  143.         return false;
  144.     }
  145. }
  146.  
  147. function postToIn() {
  148.     let text = document.getElementById("ex").value;
  149.     console.log(postFixToInFix(text));
  150.     let re = document.getElementById("result");
  151.  
  152.     re.innerHTML = postFixToInFix(text);
  153. }
  154.  
  155. function inToPost() {
  156.     let text = document.getElementById("ex").value;
  157.     console.log(infixToPostfix(text));
  158.     let re = document.getElementById("result");
  159.  
  160.     re.innerHTML = infixToPostfix(text);
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement