Simplifying and evaluating mathematical strings [closed]

风流意气都作罢 提交于 2020-01-06 04:46:04

问题


What would be the simplest way to evaluate/simplify a mathematical string in VB?

For example: "k*k+(5+2*5)k+k" would simplify to "k^2+15k+k" and "5^2+3" would evaluate to 28.

To evaluate, I'm using NCalc by simply using the evaluate function, but it doesn't simplify expressions. What would be the simplest way of simplifying the equations?


回答1:


You could try Math.NET Symbolics.

It doesn't "know" about the implied multiplication for (a)b, so you would have to work out how to insert an * if you can't require it.

Imports MathNet.Symbolics
Imports Expr = MathNet.Symbolics.SymbolicExpression

Module Module1

    Sub Main()
        Dim a = Expr.Parse("k*k+(5+2*5)*k+k")
        Console.WriteLine(a.ToString())
        Console.WriteLine(Expr.Parse("5^2+3"))

        Console.ReadLine()

    End Sub

End Module

Outputs:

16*k + k^2
28



来源:https://stackoverflow.com/questions/59444338/simplifying-and-evaluating-mathematical-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!