var Solver = (function () {
function Solver(equations) {
this.params = Object.keys(equations)
this.equations = this.parseEquations(equations)
}
Solver.prototype.parseEquations = function(equations){
var replacements = {
power : {
re: /([\w.]+)\^([\w.]+)/g,
res: 'Math.pow($1,$2)'
},
powerPython : {
re: /([\w.]+)\*\*([\w.]+)/g,
res: 'Math.pow($1,$2)'
},
}
for(var key in equations){
var eq = equations[key]
for(var re in replacements){
var repl = replacements[re]
eq = eq.replace(repl.re, repl.res)
}
equations[key] = eq
}
return equations;
}
Solver.prototype.solve = function solve(obj) {
var out = {},
nullCount = Object.keys(this.equations).length,
lastNull = 0;
for (var key = 0; key < this.params.length; key++) {
eval(this.params[key] + '=undefined')
}
for (var key in obj) {
if (this.params.indexOf(key) != -1 && (obj[key]==0 || obj[key])) {
eval(key + '=' + obj[key]),
out[key] = obj[key]
}
}
var equations = JSON.parse(JSON.stringify(this.equations))
while (lastNull !== nullCount) {
lastNull = nullCount;
for (var eq in equations) {
with(Math)
var result = eval(equations[eq]);
if (result) {
out[eq] = result;
equations[eq] = undefined;
}
}
nullCount = Object.keys(equations).length;
}
return out;
}
return Solver;
}());
if (typeof module !== 'undefined') module.exports = Solver;
&#13;
<html>
<head>
<script src='js-solver.js'></script>//declare the js library file
</head>
<body>
<div id="res"></div>
<script>
var triangleSolver = new Solver({
area: 'base*h/2',
c: 'Math.sqrt(Math.pow(a,2)+Math.pow(b,2)) || base/2', //base/2 is an example of a dual-equation definition
a: 'Math.sqrt(Math.pow(c,2)-Math.pow(b,2))',
b: 'Math.sqrt(Math.pow(c,2)-Math.pow(a,2))',
h: 'area*2/base',
base: 'area*2/h'
})
triangleSolver.solve({
c: 5,
b: 3,
area: 50,
h: 10
})
document.getElementById("res").innerHTML = h;
</script>
</body>
</html>
&#13;
我在js-solver中的代数中有一些代码。我想在输入字段中输入代数方程得到方程的解和步骤但是我尝试在algebra.js库中使用但是有些长方程不支持所以如何解决方程请帮助我纠正我的代码。例如,等式1 / 4x2 + 5 / 4x = 3y - 12/5如何解决。