Skip to content

Instantly share code, notes, and snippets.

@matthewmorrone
Last active August 29, 2015 14:17
Show Gist options
  • Save matthewmorrone/756875a195f6193b8d29 to your computer and use it in GitHub Desktop.
Save matthewmorrone/756875a195f6193b8d29 to your computer and use it in GitHub Desktop.
function MathSolver() {
this.infixToPostfix = function(infix) {
var outputQueue = "";
var operatorStack = [];
var operators = {
"^": {
precedence: 4,
associativity: "Right"
},
"/": {
precedence: 3,
associativity: "Left"
},
"*": {
precedence: 3,
associativity: "Left"
},
"+": {
precedence: 2,
associativity: "Left"
},
"-": {
precedence: 2,
associativity: "Left"
}
}
infix = infix.replace(/\s+/g, "");
infix = infix.split(/([\+\-\*\/\^\(\)])/).clean();
for(var i = 0; i < infix.length; i++) {
var token = infix[i];
if(token.isNumeric()) {
outputQueue += token + " ";
} else if("^*/+-".indexOf(token) !== -1) {
var o1 = token;
var o2 = operatorStack[operatorStack.length - 1];
while("^*/+-".indexOf(o2) !== -1 && ((operators[o1].associativity === "Left" && operators[o1].precedence <= operators[o2].precedence) || (operators[o1].associativity === "Right" && operators[o1].precedence < operators[o2].precedence))) {
outputQueue += operatorStack.pop() + " ";
o2 = operatorStack[operatorStack.length - 1];
}
operatorStack.push(o1);
} else if(token === "(") {
operatorStack.push(token);
} else if(token === ")") {
while(operatorStack[operatorStack.length - 1] !== "(") {
outputQueue += operatorStack.pop() + " ";
}
operatorStack.pop();
}
}
while(operatorStack.length > 0) {
outputQueue += operatorStack.pop() + " ";
}
return outputQueue;
}
}
var ms = new MathSolver();
console.log(ms.infixToPostfix("5 + 3 * 6 - ( 5 / 3 ) + 7"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment