Private ExStr() As String '存储表达式
Option Explicit
Private Sub cmdCalc_Click()
Dim tmpStr As String
tmpStr = txtExpression.Text
Form1.Cls
ReDim ExStr(1)
Dim i As Integer, Ch As String, P As Integer, tmpStr2 As String, NumStr As String, j As Integer, Found As Boolean
Found = True
Do While Found
For i = 1 To Len(tmpStr)
If Mid(tmpStr, i, 2) = "(-" Then
tmpStr = Left(tmpStr, i) & "0" & Right(tmpStr, Len(tmpStr) - i)
Found = True
Exit For
End If
Next
If i > Len(tmpStr) Then Found = False
Loop
tmpStr2 = ".0123456789"
NumStr = ""
For i = 1 To Len(tmpStr)
Ch = Mid(tmpStr, i, 1)
P = InStr(1, tmpStr2, Ch)
If P > 0 Then '如果是数字
NumStr = NumStr & Ch
Else '非数字
If NumStr <> "" Then
ExStr(UBound(ExStr)) = NumStr
ReDim Preserve ExStr(UBound(ExStr) + 1)
NumStr = ""
End If
ExStr(UBound(ExStr)) = Ch
ReDim Preserve ExStr(UBound(ExStr) + 1)
End If
Next
If NumStr <> "" Then
ExStr(UBound(ExStr)) = NumStr
Else
ReDim Preserve ExStr(UBound(ExStr) - 1)
End If
Dim Oper1 As Double, Oper2 As Double, Op As String, LArc As String, RArc As String, CalcFlag As Boolean
Dim Result As Double
Dim tmpSSS As String
Do While ExistOperator
For i = 2 To UBound(ExStr) - 1
If ExStr(i - 1) = "(" And ExStr(i + 1) = ")" Then
ExStr(i - 1) = ExStr(i)
For j = i + 2 To UBound(ExStr)
ExStr(j - 2) = ExStr(j)
Next
ReDim Preserve ExStr(UBound(ExStr) - 2)
Exit For
End If
Next
tmpSSS = ""
For i = 1 To UBound(ExStr)
If IsNumeric(ExStr(i)) = True Then
If CDbl(ExStr(i)) < 0 Then
tmpSSS = tmpSSS & "(" & ExStr(i) & ")"
Else
tmpSSS = tmpSSS & ExStr(i)
End If
Else
tmpSSS = tmpSSS & ExStr(i)
End If
Next
Print "=" & tmpSSS
CalcFlag = False
For i = 1 To UBound(ExStr)
Select Case ExStr(i)
Case "*":
If IsNumeric(ExStr(i - 1)) And IsNumeric(ExStr(i + 1)) Then
Oper1 = CDbl(ExStr(i - 1))
Oper2 = CDbl(ExStr(i + 1))
Result = Oper1 * Oper2
ExStr(i - 1) = CStr(Result)
For j = i To UBound(ExStr) - 2
ExStr(j) = ExStr(j + 2)
Next
ReDim Preserve ExStr(UBound(ExStr) - 2)
CalcFlag = True
Exit For
End If
Case "/":
If IsNumeric(ExStr(i - 1)) And IsNumeric(ExStr(i + 1)) Then
Oper1 = CDbl(ExStr(i - 1))
Oper2 = CDbl(ExStr(i + 1))
Result = Round(Oper1 / Oper2, 2)
ExStr(i - 1) = CStr(Result)
For j = i To UBound(ExStr) - 2
ExStr(j) = ExStr(j + 2)
Next
ReDim Preserve ExStr(UBound(ExStr) - 2)
CalcFlag = True
Exit For
End If
End Select
Next
For i = 1 To UBound(ExStr)
If CalcFlag Then Exit For
Select Case ExStr(i)
Case "+":
If IsNumeric(ExStr(i - 1)) And IsNumeric(ExStr(i + 1)) Then
Oper1 = CDbl(ExStr(i - 1))
Oper2 = CDbl(ExStr(i + 1))
Result = Oper1 + Oper2
ExStr(i - 1) = CStr(Result)
For j = i To UBound(ExStr) - 2
ExStr(j) = ExStr(j + 2)
Next
ReDim Preserve ExStr(UBound(ExStr) - 2)
Exit For
End If
Case "-":
If IsNumeric(ExStr(i - 1)) And IsNumeric(ExStr(i + 1)) Then
Oper1 = CDbl(ExStr(i - 1))
Oper2 = CDbl(ExStr(i + 1))
Result = Oper1 - Oper2
ExStr(i - 1) = CStr(Result)
For j = i To UBound(ExStr) - 2
ExStr(j) = ExStr(j + 2)
Next
ReDim Preserve ExStr(UBound(ExStr) - 2)
Exit For
End If
End Select
Next
Loop
Print "=" & ExStr(1)
End Sub
Private Function ExistOperator() As Boolean '检测数组中是否含有运算符,含有则返回 True,否则返回 False
Dim i As Integer
For i = 1 To UBound(ExStr)
If InStr(1, "+-*/", ExStr(i)) > 0 Then
ExistOperator = True
Exit Function
End If
Next
ExistOperator = False
End Function