Javascript 无法使用Mathos math Prser计算数学表达式
我用它来计算数学表达式。我试图解析以下表达式,但它引发FormatException-输入字符串的格式不正确Javascript 无法使用Mathos math Prser计算数学表达式,javascript,c#,parsing,math,evaluation,Javascript,C#,Parsing,Math,Evaluation,我用它来计算数学表达式。我试图解析以下表达式,但它引发FormatException-输入字符串的格式不正确 Mathos.Parser.MathParser parser = new Mathos.Parser.MathParser(); string expression = "Math.pow((4),(5))"; //Or "Math.sqrt(1)"; string result = parser.Parse(expression).ToString(); 在我的应用程序中,我使用m
Mathos.Parser.MathParser parser = new Mathos.Parser.MathParser();
string expression = "Math.pow((4),(5))"; //Or "Math.sqrt(1)";
string result = parser.Parse(expression).ToString();
在我的应用程序中,我使用mathml编辑器,它为我提供mathml。使用这个mathml,我使用javascript将其解析为普通的数学表达式,然后将这个表达式发送到我的c#代码进行计算。
我的表情有什么不对
注意:由于某些条件,我不在javascript中计算数学表达式。它不起作用的原因是pow函数被定义为localFunction。下面的代码应该有效:
Mathos.Parser.MathParser parser = new Mathos.Parser.MathParser();
string expression = "pow(4,5)";
string result = parser.Parse(expression).ToString();
// or
string expression2 = "4^5";
string result2 = parser.Parse(expression2).ToString();
Assert.AreEqual(result, result2);
基本上,为了使用Math.Pow函数,您可以使用内置幂运算符或内置幂函数
您可以随时更改幂函数的定义。这是当前的定义:
LocalFunctions.Add(“pow”,x=>(decimal)Math.pow((double)x[0],(double)x[1])代码>
OperatorList.Add(“^”)代码>和运算符操作.Add(“^”,(numberA,numberB)=>(十进制)Math.Pow((双)numberA,(双)numberB))代码>
我喜欢的一个解决方案是在c#中使用javascript eval,使用microsoftjscript而不是Mathos。但在某些情况下也会失败请检查我的解决方案是否有效!