7,787
社区成员
发帖
与我相关
我的任务
分享Dim symbol() As String, data() As String
Private Sub Command1_Click()
ReDim symbol(0), data(0)
Dim s As String
Analysis " / * + 2 3 4 5"
Debug.Print Calc(s)
End Sub
Public Function Analysis(ByVal exp As String) As String
Dim strCh As String
Dim i As Integer
For i = 1 To Len(exp)
strCh = Mid$(exp, i, 1)
If strCh <> " " Then
If strCh = "+" Or strCh = "-" Or strCh = "*" Or strCh = "/" Then
symbol(UBound(symbol)) = strCh
ReDim Preserve symbol(UBound(symbol) + 1)
ElseIf IsNumeric(strCh) Then
data(UBound(data)) = strCh
ReDim Preserve data(UBound(data) + 1)
Else
Analysis = "error"
End If
End If
Next
End Function
Public Function Calc(ByVal ErrMsg As String) As Double
On Error GoTo ToExit '´ò¿ª´íÎóÏÝÚå
'------------------------------------------------
Dim i As Integer, intPos As Integer
Dim result As Double
For i = UBound(symbol) - 1 To 0 Step -1
Select Case symbol(i)
Case "+"
If pos = 0 Then
result = Val(data(pos)) + Val(data(pos + 1))
pos = pos + 1
Else
result = result + Val(data(pos + 1))
pos = pos + 1
End If
Case "-"
If pos = 0 Then
result = Val(data(pos)) - Val(data(pos + 1))
pos = pos + 1
Else
result = result - Val(data(pos + 1))
pos = pos + 1
End If
Case "*"
If pos = 0 Then
result = Val(data(pos)) * Val(data(pos + 1))
pos = pos + 1
Else
result = result * Val(data(pos + 1))
pos = pos + 1
End If
Case "/"
If pos = 0 Then
result = Val(data(pos)) / Val(data(pos + 1))
pos = pos + 1
Else
result = result / Val(data(pos + 1))
pos = pos + 1
End If
End Select
Next
Calc = result
'------------------------------------------------
Exit Function
'----------------
ToExit:
ErrMsg = Err.Number & ":" & Err.Description
End Function
sub main()
debug.print f("*+234",1)
end sub
function f(byval exp as string, byref i as long) as double
dim ch as string, a as double, b as double
ch = mid$(exp,i,1)
i = i+1
select case ch
case "+"
a = f(exp, i)
b = f(exp, i)
f = a + b
case "-" '参考 case "+"
case "*" '参考 case "+"
case "/" '参考 case "+"
case else
f = val(ch)
end select
end function