Advertisement
Guest User

Untitled

a guest
Feb 12th, 2010
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Matthew McDole
  3.  *
  4.  */
  5.  
  6.  
  7. function Calculate()
  8. {
  9.     var infix = document.getElementById('userInput').value;
  10.    
  11.     try
  12.     {
  13.         var postfix = InfixToPostfix(infix);
  14.         var result = EvaluateExpression(postfix);
  15.        
  16.         document.getElementById('result').innerHTML += 'Infix: ' + infix + '<br/>';
  17.         document.getElementById('result').innerHTML += 'Postfix: ' + postfix + '<br/>';
  18.         document.getElementById('result').innerHTML += 'Result: ' + result + '<br/><br/>';
  19.     }
  20.     catch(e)
  21.     {
  22.         document.getElementById('result').innerHTML += e + '<br/><br/>';
  23.     }
  24. }
  25.  
  26.  
  27.  
  28. function InfixToPostfix(expression)
  29. {
  30.     var tokens = expression.split(/([0-9]+(?:\.[0-9]+)?|[*+-\^\/()])/);
  31.     var outputQueue = [];
  32.     var operatorStack = [];
  33.    
  34.     while (tokens.length != 0)
  35.     {
  36.         var currentToken = tokens.shift();
  37.        
  38.         if (isNumber(currentToken))
  39.         {
  40.             outputQueue.push(currentToken);
  41.         }
  42.         else if (isOperator(currentToken))
  43.         {
  44.             while ((getAssociativity(currentToken) == 'left' &&
  45.                     getPrecedence(currentToken) <= getPrecedence(operatorStack[operatorStack.length-1])) ||
  46.                    (getAssociativity(currentToken) == 'right' &&
  47.                     getPrecedence(currentToken) < getPrecedence(operatorStack[operatorStack.length-1])))
  48.             {
  49.                 outputQueue.push(operatorStack.pop())
  50.             }
  51.            
  52.             operatorStack.push(currentToken);
  53.            
  54.         }
  55.         else if (currentToken == '(')
  56.         {
  57.                 operatorStack.push(currentToken);
  58.         }
  59.         else if (currentToken == ')')
  60.         {
  61.             while (operatorStack[operatorStack.length-1] != '(')
  62.             {
  63.                 if (operatorStack.length == 0)
  64.                 {
  65.                     throw("Parentheses balancing error");
  66.                 }  
  67.                 outputQueue.push(operatorStack.pop());
  68.             }  
  69.             operatorStack.pop();       
  70.         }  
  71.     }  
  72.  
  73.     while (operatorStack.length != 0)
  74.     {
  75.         if (!operatorStack[operatorStack.length-1].match(/([()])/))
  76.         {
  77.             outputQueue.push(operatorStack.pop());
  78.         }
  79.         else
  80.         {
  81.             throw("Parentheses balancing error!");
  82.         }        
  83.     }
  84.    
  85.     return outputQueue.join(" ");
  86. }    
  87.  
  88. function EvaluateExpression(expression)
  89. {
  90.     var tokens = expression.split(/([0-9]+(?:\.[0-9]+)?|[*+-\^\/()])/);
  91.     var evalStack = [];
  92.        
  93.     while (tokens.length != 0)
  94.     {
  95.         var currentToken = tokens.shift();
  96.        
  97.         if (isNumber(currentToken))
  98.         {
  99.             evalStack.push(currentToken);
  100.         }
  101.         else if (isOperator(currentToken))
  102.         {
  103.             var operand2 = evalStack.pop();
  104.             var operand1 = evalStack.pop();
  105.            
  106.             var result = PerformOperation(parseFloat(operand1), parseFloat(operand2), currentToken);
  107.             evalStack.push(result);
  108.         }
  109.     }
  110.     return evalStack.pop();
  111. }
  112.  
  113.  
  114.  
  115. function isOperator(token)
  116. {
  117.     if (!token.match(/([*+-\^\/])/))
  118.         return false;
  119.     else
  120.         return true;
  121. }
  122.  
  123.  
  124. function isNumber(token)
  125. {
  126.     if (!token.match(/([0-9]+)/))
  127.         return false;
  128.     else
  129.         return true;
  130. }
  131.  
  132.  
  133. function getPrecedence(token)
  134. {
  135.     switch (token)
  136.     {
  137.         case '^':
  138.             return 9;
  139.         case '*':          
  140.         case '/':
  141.         case '%':
  142.             return 8;
  143.         case '+':
  144.         case '-':
  145.             return 6;
  146.         default:
  147.             return -1;
  148.     }
  149. }
  150.  
  151. function getAssociativity(token)
  152. {
  153.     switch(token)
  154.     {
  155.         case '+':
  156.         case '-':
  157.         case '*':
  158.         case '/':
  159.             return 'left';
  160.         case '^':
  161.             return 'right';
  162.     }
  163.    
  164. }
  165.  
  166. function PerformOperation(operand1, operand2, operator)
  167. {
  168.     switch(operator)
  169.     {
  170.         case '+':
  171.             return operand1 + operand2;
  172.         case '-':
  173.             return operand1 - operand2;
  174.         case '*':
  175.             return operand1 * operand2;
  176.         case '/':
  177.             return operand1 / operand2;
  178.         case '^':
  179.             return Math.pow(operand1, operand2);
  180.         default:
  181.             return;
  182.     }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement