简体   繁体   English

如何将数学方程字符串转换为 json 结构并使用 JavaScript 进行解析?

[英]How to convert a math equation string to json structure and parse using JavaScript?

This is an example of a math equation string "((VAR1+VAR2)/VAR3)*VAR4)"这是数学方程式字符串"((VAR1+VAR2)/VAR3)*VAR4)"的示例

This is my json structure approach, since there are precedences, it would be good (i guess) to pair the results for each row.这是我的 json 结构方法,因为有优先级,最好(我猜)将每一行的结果配对。

{
"variables":[
  {"code":"var1","value":5},
  {"code":"var2","value":10},
  {"code":"var3","value":2},
  {"code":"var4","value":20}
],
'expression':[
  {"fPart":"fp1",  "p1":"var1", "operation": "+", "p2":"var2"},
  {"fPart":"fp2",  "p1":"fp1", "operation": "/", "p2":"var3"},
  {"fPart":"fp3",  "p1":"fp2", "operation": "*", "p2":"var4"}]
} 

Im struggling to get some efficient code function parse() that could read this json and outputs the parsed string "((VAR1+VAR2)/VAR3)*VAR4)" , in this case 5+10/2*20我正在努力获得一些有效的代码function parse()可以读取此 json 并输出解析的字符串"((VAR1+VAR2)/VAR3)*VAR4)" ,在这种情况下为 5+10/2*20

  //
  fillExpressionWithValues(variableArray, expressionArray) {
    for (let expression of expressionArray) {
      let f1 = variableArray.find(x => x.code == expression.p1);
      let f2 = variableArray.find(x => x.code == expression.p2);
      if (f1) expression.p1 = JSON.parse(f1.value);
      if (f2) expression.p2 = JSON.parse(f2.value);
    }
  }

  //
  calculateExpression(expressionArray) {
    for (let expression of expressionArray) {
      let fPart = this.parse(expression.p1 + expression.operation + expression.p2);
      let f1 = expressionArray.find(x => x.p1 == expression.fPart);
      let f2 = expressionArray.find(x => x.p2 == expression.fPart);
      if (f1) {
        f1.p1 = JSON.parse(fPart);
      }
      if (f2) {
        f2.p2 = JSON.parse(fPart);
      }
      expression.fPart = JSON.parse(fPart);
    }
  }

 const config = { "variables": [ { "code": "var1", "value": 5 }, { "code": "var2", "value": 10 }, { "code": "var3", "value": 2 }, { "code": "var4", "value": 20 }, ], "expression": [ { "fPart": "fp1", "p1": "var1", "operation": "+", "p2": "var2" }, { "fPart": "fp2", "p1": "fp1", "operation": "/", "p2": "var3" }, { "fPart": "fp3", "p1": "fp2", "operation": "*", "p2": "var4" }, ], }; let result = ''; for (const expressionStep of config.expression) { const left = config.variables.find(v => v.code === expressionStep.p1).value; const right = config.variables.find(v => v.code === expressionStep.p2).value; const newValue = '(' + left + expressionStep.operation + right + ')'; config.variables.push({ code: expressionStep.fPart, value: newValue }); result = newValue; } console.log(result);

You can update an object that holds the variables each time an expression is parsed, so the string representation of the expression can be used as if it were a variable name when parsing the next expression.您可以在每次解析表达式时更新包含变量的 object,因此在解析下一个表达式时,表达式的字符串表示可以像变量名一样使用。

It could look like this:它可能看起来像这样:

 const input = getJSON(), parsed = parseMath(input); console.log("parsed:", parsed); function parseMath(json){ // Puts variables into a simple object const vars = {}; for(let variable of json.variables){ vars[variable.code] = variable.value; } // Copies "expression" array to avoid mutation const exprs = json.expression.slice(); // Builds string, and adds it to expr object and vars object for(let expr of exprs){ expr.stringRepresention = `(${vars[expr.p1]}${expr.operation}${vars[expr.p2]})`; vars[expr.fPart] = expr.stringRepresention; } // Gets the string representation from the final expr object return exprs.reverse()[0]["stringRepresention"]; } function getJSON(){ return { "variables": [ { "code": "var1", "value": 5 }, { "code": "var2", "value": 10 }, { "code": "var3", "value": 2 }, { "code": "var4", "value": 20 } ], 'expression': [ { "fPart": "fp1", "p1": "var1", "operation": "+", "p2": "var2" }, { "fPart": "fp2", "p1": "fp1", "operation": "/", "p2": "var3" }, { "fPart": "fp3", "p1": "fp2", "operation": "*", "p2": "var4" } ] }; }

 const mathObj = { "variables":[ {"code":"var1","value":5}, {"code":"var2","value":10}, {"code":"var3","value":2}, {"code":"var4","value":20} ], 'expression':[ {"fPart":"fp1", "p1":"var1", "operation": "+", "p2":"var2"}, {"fPart":"fp2", "p1":"fp1", "operation": "/", "p2":"var3"}, {"fPart":"fp3", "p1":"fp2", "operation": "*", "p2":"var4"}] } let evals = new Map(mathObj.variables.map(x=>[x.code,x.value])); mathObj.expression.forEach(x=>{ if(evals.has(x.p1) && evals.has(x.p2)){ evals.set(x.fPart, `(${evals.get(x.p1)}${x.operation}${evals.get(x.p2)})`) } }); evals.get(mathObj.expression[mathObj.expression.length-1].fPart);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM