题解 | #四则运算#
四则运算
https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e
let str = readline().replace('{','(').replace('}',')').replace('[','(').replace(']',')').split(' ').join('');
// console.log(str)
// 中缀表达式转后缀表达式
function convert(str){
// 存储输出表达式
let output = [];
//存储符号
let stack = [];
for (let i = 0;i < str.length;i ++){
let ch = str[i];
// 如果式数字就取出对应的数
if(/\d/.test(ch)){
let j = i + 1;
let temStr = ch;
while(/\d/.test(str[j])){
temStr = temStr + '' + str[j];
j ++;
}
output.push(temStr);
let count = temStr.length;
i += (count-1);
}else if(ch == '('){
// 所有左括号都入栈
stack.push(ch)
} else if (ch == ')'){
let a;
// 碰到右括号后则将(及之后的元素出栈
while ((a = stack.pop()) && a != '('){
output.push(a);
}
}else if(ch == '*' || ch == '/'){
stack.push(ch);
}else if(ch == '+' || ch == '-'){
// 如果有小括号
if (stack.indexOf('(') > -1){
let a;
while ( (a = stack.pop()) && a != '('){
output.push(a);
}
stack.push('(')
stack.push(ch);
}else{
let a;
while ( (a = stack.pop()) && a != undefined){
output.push(a);
}
stack.push(ch);
}
}
}
let a;
while ((a = stack.pop()) && a != undefined){
output.push(a)
}
return output;
}
let arr = convert(str)
let isStop = false;
function caculate(arr){
if (isStop)return;
let temArr;
for (let i = 0; i < arr.length; i++){
let ch = arr[i];
if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
let a = arr[i-2];
let b = arr[i-1];
let c;
if(ch == '+'){
c = Number(a) + Number(b);
}else if (ch == '-'){
c = a - b;
}else if (ch == '*'){
c = a * b;
}else if (ch == '/'){
c = a / b;
}
if(arr.length > 3){
arr.splice(i-2,3,c);
break;
}else if (arr.length == 3){
console.log(c);
isStop = true;
return;
}
}
}
caculate(arr);
}
caculate(arr);
// console.log(str)
// 中缀表达式转后缀表达式
function convert(str){
// 存储输出表达式
let output = [];
//存储符号
let stack = [];
for (let i = 0;i < str.length;i ++){
let ch = str[i];
// 如果式数字就取出对应的数
if(/\d/.test(ch)){
let j = i + 1;
let temStr = ch;
while(/\d/.test(str[j])){
temStr = temStr + '' + str[j];
j ++;
}
output.push(temStr);
let count = temStr.length;
i += (count-1);
}else if(ch == '('){
// 所有左括号都入栈
stack.push(ch)
} else if (ch == ')'){
let a;
// 碰到右括号后则将(及之后的元素出栈
while ((a = stack.pop()) && a != '('){
output.push(a);
}
}else if(ch == '*' || ch == '/'){
stack.push(ch);
}else if(ch == '+' || ch == '-'){
// 如果有小括号
if (stack.indexOf('(') > -1){
let a;
while ( (a = stack.pop()) && a != '('){
output.push(a);
}
stack.push('(')
stack.push(ch);
}else{
let a;
while ( (a = stack.pop()) && a != undefined){
output.push(a);
}
stack.push(ch);
}
}
}
let a;
while ((a = stack.pop()) && a != undefined){
output.push(a)
}
return output;
}
let arr = convert(str)
let isStop = false;
function caculate(arr){
if (isStop)return;
let temArr;
for (let i = 0; i < arr.length; i++){
let ch = arr[i];
if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
let a = arr[i-2];
let b = arr[i-1];
let c;
if(ch == '+'){
c = Number(a) + Number(b);
}else if (ch == '-'){
c = a - b;
}else if (ch == '*'){
c = a * b;
}else if (ch == '/'){
c = a / b;
}
if(arr.length > 3){
arr.splice(i-2,3,c);
break;
}else if (arr.length == 3){
console.log(c);
isStop = true;
return;
}
}
}
caculate(arr);
}
caculate(arr);