我正在开发一个网络应用程序,我们使用数据库中的公式进行代数计算。 因此像EXP这样的代数函数必须由Math.exp等代替。 POW功能有点棘手。
我不确定这个公式是否正确解析。
((KT * FK) * RT ^ 2) / QA
((KT * FK) * Math.pow(RT, 2)) / QA
是正确还是有" *"优先?
这里是我从中获取解析公式的页面。 在第一个输入中输入公式,然后单击解析按钮。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Parsing the Pow-function</title>
<meta charset="utf-8" />
<script type="text/javascript">
function ParseFormula(formula) {
if (formula.indexOf("^") > -1) {
formula = formula.replace(/ /g, "");
var tab = [];
var powfunc = "Math.pow";
var joker = "___joker___";
while (formula.indexOf("(") > -1) {
formula = formula.replace(/(\([^\(\)]*\))/g, function (m, t) {
tab.push(t); //Adds the element t to the array
return (joker + (tab.length - 1));
});
}
tab.push(formula); //Adds the formula to the array
formula = joker + (tab.length - 1);
while (formula.indexOf(joker) > -1) {
formula = formula.replace(new RegExp(joker + "(\\d+)", "g"), function (m, d) {
return tab[d].replace(/(\w*)\^(\w*)/g, powfunc + "($1,$2)");
});
}
}
return formula;
}
</script>
</head>
<body>
<h3>On this site the mathematical sign ^ is converted to Math.pow and the formula changed</h3>
<input id="txtFormula" type="text" style="width:300px;" />
<br /><br />
<input id="txtParsedFormula" type="text" style="width:300px;" />
<br /><br />
<input type="button" value="Parse" onclick="document.getElementById('txtParsedFormula').value = ParseFormula(document.getElementById('txtFormula').value);" />
</body>
</html>
&#13;
答案 0 :(得分:0)
根据页面&#34;数学第一&#34;这似乎是正确的。
对于所有数值或代数表达式,评估顺序为(BEDMAS):
B 球拍和圆括号=第一优先级
E xponents =第二优先级
D ivision =第三优先级
M ultiplication =第三优先级
A ddition =第四优先级
S ubtraction =第四优先级