题解 | #四则运算#
四则运算
https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
handle(line);
});
function handle(line) {
let str = line.replace(/\[|{/g, "(").replace(/\]|}/g, ")");
// 方法一:投机取巧法
// console.log(eval(str))
// 方法二:半投机取巧
let stack = [...str];
let arr = [];
while (stack.length) {
const stackCode = stack.pop();
if (stackCode !== "(") {
arr.unshift(stackCode);
} else {
let newStr = "";
let arrCode = "";
while (arrCode !== ")") {
newStr += arrCode;
arrCode = arr.shift();
}
arr.unshift(eval(newStr));
}
}
console.log(eval(arr.join("")));
}