Advertisement
Lusien_Lashans

dejkstra

May 22nd, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getPolska(input) {
  2.  
  3.  priority = {'(' : 0, ')' : 1, '+' : 2, '-' : 2, '*' : 3, '/' : 3, '^' : 4};
  4.  
  5.  
  6.      stack = new Array();
  7.      out = "";
  8.  
  9.  
  10.     function inputNumber() {
  11.         while ((input.charAt(i) >= '0' && input.charAt(i) <= '9') || (input.charAt(i) == '.')) {
  12.             out += input.charAt(i);
  13.             i++
  14.         }
  15.         i--;
  16.         out += " ";
  17.     }
  18.    
  19.  
  20.     for (i = 0; i < input.length; i++) {
  21.         symbol = input.charAt(i);
  22.         switch (symbol) {
  23.             case ' ' :
  24.                 break;
  25.  
  26.             case '(' :
  27.                 stack.push('(');
  28.                 break;
  29.  
  30.             case ')':
  31.                 while (stack[stack.length - 1] != '(') {
  32.                     out += stack.pop();
  33.                 }
  34.                 stack.pop();
  35.                 break;
  36.  
  37.             case '+' :
  38.             case '-' :
  39.             case '*' :
  40.             case '/' :
  41.             case '^' :
  42.                 if (/\(\-/.test(input.charAt(i-1) + symbol)) {
  43.                     i++;
  44.                     out += " -";
  45.                     inputNumber();
  46.                 }
  47.  
  48.                 else if (priority[symbol] > priority[stack[stack.length - 1]]) {
  49.                     stack.push(symbol);
  50.                 }
  51.                 else {
  52.                     while (priority[symbol] <= priority[stack[stack.length - 1]]) {
  53.                         out += stack.pop();
  54.                     }
  55.                     stack.push(symbol);
  56.                 }
  57.                 break;
  58.            
  59.             default :
  60.                 if (symbol >= '0' && symbol <= '9') {
  61.                     out += " ";
  62.                     inputNumber();
  63.                 }
  64.         }
  65.     }
  66.  
  67.     while (stack.length > 0)
  68.         out += stack.pop();
  69.    
  70.  
  71.     return out;
  72. }
  73.  
  74.  
  75. function calculate(polska) {
  76.     stack = new Array();
  77.     justDoIT = "";
  78.  
  79.     for (i=0; i<polska.length; i++) {
  80.         symbol = polska.charAt(i);
  81.        
  82.         if (symbol == ' ') {
  83.             i++;
  84.             num = "";
  85.  
  86.             while (polska.charAt(i) != " ") {
  87.                 num+=polska.charAt(i);
  88.                 i++;
  89.             }
  90.  
  91.             stack.push(num);
  92.         }
  93.  
  94.         else {
  95.             op2=stack.pop();
  96.             op1=stack.pop();
  97.             if (symbol!='^') {
  98.                 justDoIT = op1 + symbol + op2;    
  99.             }
  100.  
  101.             else {
  102.                 justDoIT = "Math.pow(" + op1 + ',' + op2 + ')';
  103.             }
  104.             stack.push(eval(justDoIT));
  105.         }
  106.     }
  107.     var result = stack.pop();
  108.     return result;
  109. }
  110.  
  111. function main(){
  112.     //WScript.Echo("Vvedi virazhenie")
  113.     //var input = WScript.StdIn.Readline();
  114.     input = '(-1)*6.5+3';
  115.     polska = getPolska(input);
  116.  
  117.     WScript.Echo("eto polska zapis")
  118.     WScript.Echo(polska);
  119.  
  120.     result = calculate(polska);
  121.  
  122.     WScript.Echo("eto resultat")
  123.     WScript.Echo(result);
  124. }
  125.  
  126. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement