7Bpencil

Dijkstra

Dec 22nd, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict"
  2. function operation(letter, priority, associativity, f) {
  3.     this.letter = letter,
  4.     this.priority = priority,
  5.     this.associativity = associativity
  6.     this.f = f
  7. }
  8.  
  9. function TranslateInfixToPostfix(infix, operators){
  10.     infix = infix.replace(/\s+/g, "");
  11.     let stack = [];
  12.     let result = "";
  13.     result += FillStack(infix, result, stack, operators);
  14.    
  15.     while (stack.length > 0){
  16.         result += stack.pop() + ' ';
  17.     }
  18.  
  19.     return result;
  20. }
  21.  
  22.  
  23. function EvaluatePostfix(postfix, operators){
  24.     let stack = [];
  25.     postfix = postfix.split(' ');
  26.    
  27.     for (let i = 0; i < postfix.length - 1; i++){
  28.         let token = postfix[i];
  29.         if (IsNumber(token)) {
  30.             stack.push(parseFloat(token));
  31.         } else {
  32.             let y = stack.pop();
  33.             let x = stack.pop();
  34.             stack.push(operators[token].f(x,y));
  35.         }
  36.     }
  37.  
  38.     return stack.pop();
  39. }
  40.  
  41.  
  42. function FillStack(infix, postfix, stack, operators) {
  43.     for (let i = 0; i < infix.length; i++) {
  44.         let token = infix.charAt(i);
  45.         if (IsNumber(token)) {
  46.             while (IsNumber(infix.charAt(i + 1))) {
  47.                 postfix += token;
  48.                 token = infix.charAt(i + 1);
  49.                 i++;
  50.             }
  51.             postfix += token + ' ';
  52.         }
  53.        
  54.         else if (operators[token]) {
  55.             postfix = HandleOperations(operators[token], stack, operators, postfix);
  56.         } else if (token == '(') {
  57.             stack.push(token);
  58.         } else if (token == ')') {
  59.             postfix = HandleBrace(stack, postfix);
  60.         }
  61.     }
  62.     return postfix;
  63. }
  64.  
  65.  
  66. function IsNumber(symbol) {
  67.     return symbol >= "0" && symbol <= "9";
  68. }
  69.  
  70.  
  71. function HandleBrace(stack, postfix) {
  72.     while(stack[stack.length - 1] != '(') {
  73.         postfix += stack.pop() + ' ';
  74.     }
  75.     stack.pop();
  76.  
  77.     return postfix;
  78. }
  79.  
  80.  
  81. function HandleOperations(op1, stack, ops, postfix) {
  82.     let op2 = ops[stack[stack.length - 1]];  
  83.     while (op2 &&
  84.         ((op1.associativity == "Left" && (op1.priority <= op2.priority)) ||
  85.         (op1.associativity == "Right" && (op1.priority < op2.priority)))) {
  86.             postfix += op2.letter + ' ';
  87.             stack.pop();
  88.             op2 = ops[stack[stack.length - 1]];
  89.     }
  90.     stack.push(op1.letter);
  91.  
  92.     return postfix;
  93. }
  94.  
  95.  
  96. test();
  97. function test() {
  98.     let operators = {
  99.         "+": new operation("+", "1", "Left", (x, y) => { return x + y }),
  100.         "-": new operation("-", "1", "Left", (x, y) => { return x - y }),
  101.         "*": new operation("*", "2", "Left", (x, y) => { return x * y }),
  102.         "/": new operation("/", "2", "Left", (x, y) => { return x / y }),
  103.         "^": new operation("^", "3", "Right", (x, y) => { return Math.pow(x,y) })
  104.     };
  105.  
  106.     let infix = "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3";
  107.     let postfix = TranslateInfixToPostfix(infix, operators);
  108.     let value = EvaluatePostfix(postfix, operators);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment