Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Matthew McDole
- *
- */
- function Calculate()
- {
- var infix = document.getElementById('userInput').value;
- try
- {
- var postfix = InfixToPostfix(infix);
- var result = EvaluateExpression(postfix);
- document.getElementById('result').innerHTML += 'Infix: ' + infix + '<br/>';
- document.getElementById('result').innerHTML += 'Postfix: ' + postfix + '<br/>';
- document.getElementById('result').innerHTML += 'Result: ' + result + '<br/><br/>';
- }
- catch(e)
- {
- document.getElementById('result').innerHTML += e + '<br/><br/>';
- }
- }
- function InfixToPostfix(expression)
- {
- var tokens = expression.split(/([0-9]+(?:\.[0-9]+)?|[*+-\^\/()])/);
- var outputQueue = [];
- var operatorStack = [];
- while (tokens.length != 0)
- {
- var currentToken = tokens.shift();
- if (isNumber(currentToken))
- {
- outputQueue.push(currentToken);
- }
- else if (isOperator(currentToken))
- {
- while ((getAssociativity(currentToken) == 'left' &&
- getPrecedence(currentToken) <= getPrecedence(operatorStack[operatorStack.length-1])) ||
- (getAssociativity(currentToken) == 'right' &&
- getPrecedence(currentToken) < getPrecedence(operatorStack[operatorStack.length-1])))
- {
- outputQueue.push(operatorStack.pop())
- }
- operatorStack.push(currentToken);
- }
- else if (currentToken == '(')
- {
- operatorStack.push(currentToken);
- }
- else if (currentToken == ')')
- {
- while (operatorStack[operatorStack.length-1] != '(')
- {
- if (operatorStack.length == 0)
- {
- throw("Parentheses balancing error");
- }
- outputQueue.push(operatorStack.pop());
- }
- operatorStack.pop();
- }
- }
- while (operatorStack.length != 0)
- {
- if (!operatorStack[operatorStack.length-1].match(/([()])/))
- {
- outputQueue.push(operatorStack.pop());
- }
- else
- {
- throw("Parentheses balancing error!");
- }
- }
- return outputQueue.join(" ");
- }
- function EvaluateExpression(expression)
- {
- var tokens = expression.split(/([0-9]+(?:\.[0-9]+)?|[*+-\^\/()])/);
- var evalStack = [];
- while (tokens.length != 0)
- {
- var currentToken = tokens.shift();
- if (isNumber(currentToken))
- {
- evalStack.push(currentToken);
- }
- else if (isOperator(currentToken))
- {
- var operand2 = evalStack.pop();
- var operand1 = evalStack.pop();
- var result = PerformOperation(parseFloat(operand1), parseFloat(operand2), currentToken);
- evalStack.push(result);
- }
- }
- return evalStack.pop();
- }
- function isOperator(token)
- {
- if (!token.match(/([*+-\^\/])/))
- return false;
- else
- return true;
- }
- function isNumber(token)
- {
- if (!token.match(/([0-9]+)/))
- return false;
- else
- return true;
- }
- function getPrecedence(token)
- {
- switch (token)
- {
- case '^':
- return 9;
- case '*':
- case '/':
- case '%':
- return 8;
- case '+':
- case '-':
- return 6;
- default:
- return -1;
- }
- }
- function getAssociativity(token)
- {
- switch(token)
- {
- case '+':
- case '-':
- case '*':
- case '/':
- return 'left';
- case '^':
- return 'right';
- }
- }
- function PerformOperation(operand1, operand2, operator)
- {
- switch(operator)
- {
- case '+':
- return operand1 + operand2;
- case '-':
- return operand1 - operand2;
- case '*':
- return operand1 * operand2;
- case '/':
- return operand1 / operand2;
- case '^':
- return Math.pow(operand1, operand2);
- default:
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement