2
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”我如何编辑我的代码,先乘以先乘,然后除+和 - 。有人有想法吗?我如何设置运营商的优先级在vb.net
使用括号 - '(2 * 3)+ 1 + 4'。请注意,您必须找出最佳使用偏差的位置,以免影响结果,但'('和')'具有更高的优先权。 [Visual Basic中的运算符优先级](http://msdn.microsoft.com/en-us/library/fw84t893.aspx) – Tim 2014-11-06 09:21:52
我该如何将它们添加到我的代码中并从它们开始? – gew 2014-11-06 09:27:16