是否有任何MathParser可以解析包含平均值函数(例如avg(value1 + value2 + value3)+ ...)的数学字符串表达式? [英] Is there any MathParser that parse a mathematical string expression containing average function like avg(value1 + value2 + value3) + ...?

查看:39
本文介绍了是否有任何MathParser可以解析包含平均值函数(例如avg(value1 + value2 + value3)+ ...)的数学字符串表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我只需键入一个数学表达式,如:

I have a text box and I simply type a mathematical expression like:

sin(1) + 2 + cos(5) + sqrt(5)

并将其传递给我的MathParser类对象,它会向我返回结果.

and pass it to my MathParser class object and it returns me the result.

我遍历了课堂,发现对于+,-,*,/等数学运算符,有一个函数返回2,因为这些操作只能在两个参数(如"1 + 2")和数学函数(如sin,cos,tan,abs,sqrt)之间执行;具有单个参数,如sin(param)等,因此它返回1.

I went through the class and found that for mathematical operators like +, -, *, /; there is a function which returns 2 because these operations can be performed only between two parameters like '1 + 2' and mathematical functions like sin, cos, tan, abs, sqrt; have a single parameter like sin(param), etc. so it returns 1.

现在,我也想添加平均值函数,但是问题是,当我输入表达式avg(1 + 2 + 3 + 4)时,它会优先使用方括号并将所有数字相加,这很好,但可以求平均值我需要在该函数内传递的参数数量.我以某种方式设法算出了第一平均值,但是如果它与其他函数一起出现或者出现了多次以上该怎么办?

Now, I want to add average function also, but problem is that when I type the expression avg(1+2+3+4), it gives priority to bracket first and add all the numbers, that's fine but for average I need the number of arguments passes within that function. I somehow managed to count for first average but what if it occurs with other functions or it occurs more than one time?

是否有任何可用的mathParser可以具有avg内置函数?我也没有在Visual Studio Math类中看到它.

Is there any mathParser available which can have an avg built in function? I don't see it in Visual Studio Math class also.

推荐答案

是的,最近有一个开源数学解析器支持可变参数函数(包括平均值),我最近在我的项目中使用了该函数.解析器名称是mXparser.解析器用C#实现,并分别用JAVA实现.

Yes, there is an open-source math parser supporting variadic functions (including average), which I was recently using in my project. The parser name is mXparser. Parser is implemented in C# and separately in JAVA.

https://mxparser.codeplex.com/

http://mathparser.org/

您的问题可以轻松解决,请按照以下示例

Your problem can be simply solved, please follow the below example

简单的平均值,包括可变参数

Simple average including variadic parameters

Expression e = new Expression("avg(2, 5, 6, 34, 2, 10) + avg(5, 4, 32)");
double v = e.calculate()

简单的平均值,包括可变参数中的变量

Simple average including variables in variadic parameters

Argument x = new Argument("x = 5");
Expression e = new Expression("avg(2, 5, 2*x, 34, 2, 10) + avg(5, 4*x, 32)", x);
double v = e.calculate()

平均作为迭代运算符

Expression e = new Expression("avg(i, 1, 100, i^2);
double v = e.calculate()

最诚挚的问候

这篇关于是否有任何MathParser可以解析包含平均值函数(例如avg(value1 + value2 + value3)+ ...)的数学字符串表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆