如何解决Javascript - 在字符串上查找左括号和右括号位置?
我正在为我的一个站点项目制作一个计算器,您可以在其中输入整个表达式,然后再解析,例如:2+3*4 将返回 14 ,22-4 将返回 18,20+5! 将返回 140,依此类推。 这适用于我展示的简单表达式,但是当我添加括号时,代码会中断。 所以像 (2+3)! 这样应该返回 120 的简单表达式实际上返回 10 或 2+3! >.
即使是基本的2+3!工作,我最初的想法是将数学符号中的字符串与其余部分分开。所以在这种情况下它会分开,它会将它分成2、+和3!;它会在哪里找到符号并解析那部分。这就是为什么它解决了 10 而不是不工作的原因。 但是在尝试解决之后,我无法使代码正常工作,除非在非常特殊的情况下,所以我决定重做代码并将其发布在这里,以防有人可以帮助我。
这是我目前用来准备用于评估的字符串的函数:
function sepOperFat(){
//2+3! it's working
//1+(2-(2+2)+3)! want that to work in the end
var value = document.calculator.ans.value;
var operandoPos = ['0'];
var operandoInPos = [''];
var paraResolver = [];
for(i = 0; i <= value.length; i++){
//check if value[i] is equal to +,-,×,÷,* & /
if(value[i] == '+' || value[i] == '-' || value[i] == '×' || value[i] == '÷' || value[i] == '*' || value[i] == '/'){
operandoPos.push(i);
operandoInPos.push(value[i]);
}
}
paraResolver.push(value.slice(operandoPos[0],operandoPos[1]));
for(var total = 1; total <= operandoPos.length; totaL++){
paraResolver.push(value.slice(operandoPos[total] + 1,operandoPos[total + 1]));
}
document.calculator.ans.value = '';
for(var total = 0; total <= paraResolver.length - 2; totaL++){
if(paraResolver[total].includes('!')){
document.calculator.ans.value += "factorial(" + paraResolver[total] + ")";
}else{
document.calculator.ans.value += paraResolver[total];
}
document.calculator.ans.value += operandoInPos[total + 1];
}
}
document.calculator.ans.value
是我有表达式的字符串的名称。operandoPos
是符号在字符串上的位置。operandoInPos
是 符号(我也许也可以使用 value.charat(operandoPos) 来表示)。paraResolver
是我要解决的数字(例如 3)。factorial(
是我负责使数字阶乘的函数的名称。
该函数没有返回值,因为我仍然想在 document.calculator.ans.value
内求解。
来解决我使用的 document.calculator.ans.value = Function('"use strict"; return '+ document.calculator.ans.value)();
等式,它在我按下按钮时激活。
是的,就是这样。我只想要一个能够知道 (2+3)! 和 2+(3)! 之间区别的函数,这样它就可以返回 factorial(2+3 ) 而不是 (2+factorial(3))。
感谢您的帮助。
解决方法
您最大的问题将是操作顺序表明需要首先评估括号。这可能意味着您的代码必须进行大量更改以支持括号解析中的任何内容。
我不认为您希望为您处理所有这些,但是您可以采取的一种方法来整理括号部分,如下所示:
router.post('/add',async ctx => {
const a = await new Articles(dbName)
try{
await new a.add(ctx.request.body)
return ctx.redirect('/?msg=new article added')
} catch(err) {
console.log(err)
await ctx.render('error',ctx.hbs)
} finally {
new a.close()
}
})
这会将您的输入包装在一个数组中,并将括号中的任何内容分组到嵌套数组中。
我可以进一步解释这里发生的事情,但您不太可能想要这个特定的代码,而是想要了解如何处理解包括号的一般想法。
值得注意的是,我提供的代码几乎没有功能,也没有错误处理,如果提供了 function parseParentheses(input) {
let openParenCount = 0;
let myOpenParenIndex = 0;
let myEndParenIndex = 0;
const result = [];
for (let i = 0; i < input.length; i++) {
if (input[i] === '(') {
if (openParenCount === 0) {
myOpenParenIndex=i;
// checking if anything exists before this set of parentheses
if (i !== myEndParenIndex) {
result.push(input.substring(myEndParenIndex,i));
}
}
openParenCount++;
}
if (input[i] === ')') {
openParenCount--;
if (openParenCount === 0) {
myEndParenIndex=i+1;
// recurse the contents of the parentheses to search for nested ones
result.push(parseParentheses(input.substring(myOpenParenIndex+1,i)));
}
}
}
// capture anything after the last parentheses
if (input.length > myEndParenIndex) {
result.push(input.substring(myEndParenIndex,input.length));
}
return result;
}
// tests
console.log(JSON.stringify(parseParentheses('1!+20'))) // ["1!+20"]
console.log(JSON.stringify(parseParentheses('1-(2+2)!'))) // ["1-",["2+2"],"!"]
console.log(JSON.stringify(parseParentheses('(1-3)*(2+5)'))) // [["1-3"],"*",["2+5"]]
console.log(JSON.stringify(parseParentheses('1+(2-(3+4))'))) // ["1+",["2-",["3+4"]]]
或 1 - (2 + 3
之类的东西,它的行为会很糟糕。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。