如何在vb.net中设置运营商优先级

时间:2014-11-06 09:12:15

标签: vb.net

Dim equation As String
Dim numbers() As String
Dim operators As New List(Of String)
Dim result As Double
Dim rx As New Regex("(\+|\-|\*)+")
Dim matches As MatchCollection

equation = TextBox1.Text
numbers = equation.Split(New String() {"+"c, "-"c, "*"c}, StringSplitOptions.None)
matches = rx.Matches(equation)

Dim m1 As Match

For Each m1 In matches
    operators.Add(m1.Value)
Next

result = CInt(numbers(0))
Dim i As Integer

For i = 1 To numbers.GetUpperBound(0)
    Select Case operators(i - 1)
        Case "*"
            result *= CDec(numbers(i))
        Case "+"
            result += CDec(numbers(i))
        Case "-"
            result -= CDec(numbers(i))
        Case " ^"
            result ^= CDec(numbers(i))
    End Select
Next
MessageBox.Show(result)

这是我的代码,例如" 1 + 4 + 2 * 3"我怎么能编辑我的代码,先从乘法开始,然后除以+和 - 。有人有想法吗?

1 个答案:

答案 0 :(得分:0)

我正在添加mXparser数学解析器库,它是用C#编写的,但是符合CLS - 它可以很容易地与VB一起使用。请关注mXparser Hello World tutorial for VB

Dim e As Expression = New Expression("2+(3-sin(pi))")
mXparser.consolePrintln( e.calculate() )

此致