0

I have an equation : 100 + (y * 5) = 200

I have to find the value of y = (200 -100)/5, but the main thing is operators sign may changes ( for eg: 100 * (y -5) = 200 ), So How to write a program such that if the equation is passed, it solve and gives the value of x, irrespective of whatever operators are used.

Any help is appreciated.

I have tried below in node js and have to solve equation :

var fs = require('fs');

fs.readFile('equation.json',
    // callback function that is called when reading file is done
    function(err, data) { 
        // json data
        var jsonData = data;
        // parse json
        var jsonParsed = JSON.parse(jsonData);
        //operators array
        var operators = {
              "add":"+",
              "subtract":"-",
              "multiply":"*",
              "divide":"/",
              "equal":"="
        };
        var eq,op1,op2,equation;
        for(opt in operators){
          if(jsonParsed.op == opt){
            eq = operators.equal;
          }
          //looking for first operator
          if(jsonParsed.lhs.op == opt){
            if(opt=="add"){
              op1 = operators.add;
            }
            else if(opt=="subtract"){
              op1 = operators.subtract;
            }
            else if(opt=="multiply"){
              op1 = operators.multiply;
            }
            else if(opt=="divide"){
              op1 = operators.divide;
            }
          }
          //looking for second operator
          if(jsonParsed.lhs.rhs.op == opt){
            if(opt=="add"){
              op2 = operators.add;
            }
            else if(opt=="subtract"){
              op2 = operators.subtract;
            }
            else if(opt=="multiply"){
              op2 = operators.multiply;
            }
            else if(opt=="divide"){
              op2 = operators.divide;
            }
          }
        }
        //console.log(eq);
        //console.log(op1);
        //console.log(op2)
        //parsing expression
        equation = jsonParsed.lhs.lhs + op1 +"(" + jsonParsed.lhs.rhs.lhs + op2 + jsonParsed.lhs.rhs.rhs + ")" + eq + jsonParsed.rhs;

        console.log(equation);

});

JSON

{
    "op": "equal",
    "lhs": {
        "op": "add",
        "lhs": 1,
        "rhs": {
            "op": "multiply",
            "lhs": "x",
            "rhs": 10
        }
    },
    "rhs": 21
}
  • I think you can hava a look at the answers for this question: https://stackoverflow.com/questions/4514302/javascript-equation-solver-library – ehhc Jan 23 '20 at 18:38
  • Do you want this solved for exactly this equation or for *any* equation? Is this homework or do you try to get a job done? – Scheintod Jan 23 '20 at 18:45
  • Does this answer your question? [JavaScript equation solver library](https://stackoverflow.com/questions/4514302/javascript-equation-solver-library) – Rvy Pandey Jan 23 '20 at 19:02

1 Answers1

0

Systems of linear equations are most easily and generally solved on computers using matrices.

There is a webpage here that goes into how this works:

https://www.aplustopper.com/solving-systems-linear-equations-using-matrices/

There are also matrix packages for JavaScript available on the internet, like this one

https://mathjs.org/docs/datatypes/matrices.html

Disclosure: I have no association with either of these websites. This is how I did it in Java (using NASA's matrix package for Java) back in the day.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • mathjs has a [`usolve`](https://mathjs.org/docs/reference/functions/usolve.html) function to solve systems of linear equations. – Anderson Green Nov 15 '21 at 20:01