January 2018
Intermediate to advanced
332 pages
7h 36m
English
Putting together all the code discussed above, the final code for converting the infix expression to postfix looks like the following:
function convert(expr) { var postfix = ""; var ops = new Stack(); var operators = { "^": { priority: 4, associativity: "rtl" }, "*": { priority: 3, associativity: "ltr" }, "/": { priority: 3, associativity: "ltr" }, "+": { priority: 2, associativity: "ltr" }, "-": { priority: 2, associativity: "ltr" } }; expr = clean(expr.trim().replace(/\s+/g, "").split(/([\+\-\*\/\^\(\)])/)); if (!isBalanced(expr) { return 'error'; } expr.forEach(function(exp) { if(!isNaN(parseFloat(exp))) { postfix += exp + " "; } else if(exp === "(") { ops.push(exp); } else if(exp === ")") { while ...
Read now
Unlock full access