यह गणित समीकरण स्ट्रिंग का एक उदाहरण है "((VAR1+VAR2)/VAR3)*VAR4)"

यह मेरा जेसन संरचना दृष्टिकोण है, क्योंकि प्राथमिकताएं हैं, प्रत्येक पंक्ति के परिणामों को जोड़ना अच्छा होगा (मुझे लगता है)।

{
"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"}]
} 

मैं कुछ कुशल कोड function parse() प्राप्त करने के लिए संघर्ष कर रहा हूं जो इस जेसन को पढ़ सकता है और पार्स किए गए स्ट्रिंग "((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);
    }
  }
0
user1148875 19 जिंदा 2021, 12:12

2 जवाब

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);
0
Harsh Gundecha 19 जिंदा 2021, 09:37
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);
2
Stratubas 19 जिंदा 2021, 09:30