我有这个问题:
编写一个简单的解释器,该解释器可以理解“ +”,“-”和“ *”操作。按顺序应用操作
使用以value
初始值开头的命令/参数对。如果遇到未知命令,请返回-1。您将完全忽略B.O.D.M.A.S。
Examples of the input and output
interpret(1, ["+"], [1]) → 2
interpret(4, ["-"], [2]) → 2
interpret(1, ["+", "*"], [1, 3]) → 6
interpret(5, ["+", "*", "-"], [4, 1, 3]) → 6
我尝试将参数作为下面的多维数组传递。我正在尝试解决问题,以便当我这样做时
let operator = ["+", "-"];
let integer = 1;
let intArr = [1, 2];
let emptyInt;
let anotherInt;
let newArray = [integer, operator, intArr];
我如何像上面那样进行这项工作?依次添加每个数组
答案 0 :(得分:1)
您可以使用Array.prototype.reduce()
。ac
作为第一个值。然后通过检查运算符来加/减/除/乘。
function interpret(...args){
let operators = args[1]; //get the operators array
let values = args[2] //numbers expect the first one.
return values.reduce((ac,val,i) =>{
//check the operator at the 'i' on which we are on while iterating through 'value'
if(operators[i] === '+') return ac + val;
else if(operators[i] === '-') return ac - val;
else if(operators[i] === '*') return ac * val;
else if(operators[i] === '/') return ac / val;
else return -1;
},args[0]) //'ac' is initially set to first value.
}
console.log(interpret(1, ["+"], [1]))
console.log(interpret(4, ["-"], [2]))
console.log(interpret(1, ["+", "*"], [1, 3]))
console.log(interpret(5, ["+", "*", "-"], [4, 1, 3]))
答案 1 :(得分:1)
您可以使用recursive approach。首先,您可以定义一个对象以将您的运算符映射到可用函数,然后调用递归函数来计算结果:
const oper = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b
};
const interpret = (n, [fc, ...calcs], [fn, ...nums]) =>
fc === undefined ? n :
interpret(oper[fc](n, fn), calcs, nums)
console.log(interpret(1, ["+"], [1])); // 2
console.log(interpret(4, ["-"], [2])); // 2
console.log(interpret(1, ["+", "*"], [1, 3])); // 6
console.log(interpret(5, ["+", "*", "-"], [4, 1, 3])); // 6
如果必须为无效的操作数返回-1
,则可以使用以下命令:
const oper = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b
};
const interpret = (n, [fc, ...calcs], [fn, ...nums]) => {
if(fc === undefined) return n;
if(!(fc in oper)) return -1;
return interpret(oper[fc](n, fn), calcs, nums)
}
console.log(interpret(1, ["+"], [1])); // 2
console.log(interpret(4, ["-"], [2])); // 2
console.log(interpret(1, ["+", "*"], [1, 3])); // 6
console.log(interpret(5, ["+", "*", "-"], [4, 1, 3])); // 6
console.log(interpret(1, ["+", "%"], [1, 2])); // -1
答案 2 :(得分:0)
您可以使用具有运算符所需功能的对象,并通过获取值和操作数来重新分配数组。
const
fns = {
'+': (a, b) => a + b,
'-': (a, b) => a - b,
'*': (a, b) => a * b,
'/': (a, b) => a / b
},
interpret = (start, operators, values) =>
values.reduce(
(a, b, i) => operators[i] in fns
? fns[operators[i]](a, b)
: - 1,
start
);
console.log(interpret(1, ["+"], [1])); // 2
console.log(interpret(4, ["-"], [2])); // 2
console.log(interpret(1, ["+", "*"], [1, 3])); // 6
console.log(interpret(5, ["+", "*", "-"], [4, 1, 3])); // 6
答案 3 :(得分:0)
您可以这样:-
function getOperator(number, operator, integer) {
let result;
switch (operator) {
case '+':
result = number + integer;
break;
case '-':
result = number - integer;
break;
case '*':
result = number * integer;
break;
case '/':
result = number / integer;
break;
}
return result;
}
function doOperation(array) {
number = array[0];
for (let i = 0; i < array[1].length; i++) {
number = getOperator(number, array[1][i], array[2][i]);
}
return number;
}
然后您可以使用它来实现询问:-
doOperation([1, ["+"], [1]])