'''''''''''主运算函数'''''''''''
Private Function Operation(Optional Num) As String
Dim Temp As String
Dim Temp1 As String
Dim Temp2 As String
Dim i As Integer
Dim L As Integer
On Error GoTo ProcError
Temp1 = ExpressionValue
Do
Set Sign = New Collection
Set Value = New Collection
L = Len(Temp1)
ZKH = 0
FKH = 0
NumArithmetic = 0
NumExponent = 0
NumLogic = 0
QB = ""
ZB = ""
HB = ""
IfLogic = False
IfBracket = False
IfArithmetic = False
IfFunction = False
For i = 1 To L
Temp = Mid$(Temp1, i, 1)
If Temp = "(" Then
IfBracket = True
ZKH = ZKH + 1
ElseIf Temp = ")" Then
IfBracket = True
FKH = FKH + 1
ElseIf Temp = "+" Or Temp = "-" Or Temp = "*" Or Temp = "/" Or Temp = "\" Then
IfArithmetic = True
NumArithmetic = NumArithmetic + 1
ElseIf Temp = "^" Then
IfFunction = True
NumExponent = NumExponent + 1
Else
End If
Next
If ZKH > FKH Then
ErrorString = "正括号和反括号没有成对配对!正括号比反括号多了" & ZKH - FKH & "个,请检查表达式!"
GoTo ProcExit
ElseIf ZKH < FKH Then
ErrorString = "正括号和反括号没有成对配对!正括号比反括号少了" & FKH - ZKH & "个,请检查表达式!"
GoTo ProcExit
End If
If L >= 6 Then
For i = 1 To L - 6
Temp = Mid$(Temp1, i, 6)
If Temp = "ArcSin" Or Temp = "ArcCos" Or Temp = "ArcAtn" Or Temp = "ArcTan" Then
IfFunction = True
NumExponent = NumExponent + 1
End If
Next
End If
If L >= 3 Then
For i = 1 To L - 3
Temp = Mid$(Temp1, i, 3)
If Temp = "Sin" Or Temp = "Cos" Or Temp = "Atn" Or Temp = "Tan" Or Temp = "Log" Or Temp = "Exp" Then
IfFunction = True
NumExponent = NumExponent + 1
ElseIf Temp = "And" Or Temp = "Xor" Or Temp = "Not" Then
IfLogic = True
NumLogic = NumLogic + 1
End If
Next
End If
If L >= 2 Then
For i = 1 To L - 2
Temp = Mid$(Temp1, i, 2)
If Temp = "Ln" Then
IfFunction = True
NumExponent = NumExponent + 1
ElseIf Temp = "Or" Then
IfLogic = True
NumLogic = NumLogic + 1
End If
Next
End If
'''''是否有括号'''''
If IfBracket = True Then
''''''(1)提取嘴里层括号的表达式
Temp1 = GetBracketExpression(Temp1)
' MsgBox "提取嘴里层括号的表达式为:" + Temp1, 64
ZKH = ZKH - 1
FKH = FKH - 1
If ZKH = 0 And FKH = 0 Then IfBracket = False
End If
' GetSignAndValue Temp1, True ''''提取表达式的运算副和数值
GetSignAndValue Temp1
'''''''(2)进行Not和阶乘运算''''
' Temp1 = NotOperation
' Temp1 = FactorialOperation
''''''(3)函数运算
Temp1 = FunctionOperation
''''''(4)全部函数运算完后,才能进行算术运算
Temp1 = ArithmeticOperation
''''''(5)进行逻辑运算''''''
Temp1 = LogicOperation
Temp1 = QB + Temp1 + HB
If QB = "" And HB = "" Then
IfComplate = True
Else
IfComplate = False
End If
Loop Until IfBracket = False And IfFunction = False And IfArithmetic = False And IfLogic = False And IfComplate = True Or ErrorString <> ""
If ErrorString <> "" Then
Operation = ErrorString
Else
If Len(Temp1) >= 1 Then
If Left$(Temp1, 1) = "+" Then
Temp1 = Right$(Temp1, Len(Temp1) - 1)
End If
End If
If IsMissing(Num) = False Then
If Num = 0 Then
Operation = Fix(Temp1)
Else
Temp = Replace$("0." & Space$(Num), " ", "0")
Operation = Format$(Temp1, Temp)
End If
Else
If Abs(CDbl(Temp1)) <= 1 Then
Temp = Replace$("0." & Space$(Len(Temp1) - 1), " ", "#")
Operation = Format$(Temp1, Temp)
Else
Operation = Temp1
End If
End If
End If
ProcExit:
Set Value = Nothing
Set Sign = Nothing
Exit Function
ProcError:
ErrorString = "表达式存在错误!"
Resume ProcExit
End Function
Private Function Operation(Optional Num) As String
Dim Temp As String
Dim Temp1 As String
Dim Temp2 As String
Dim i As Integer
Dim L As Integer
On Error GoTo ProcError
Temp1 = ExpressionValue
Do
Set Sign = New Collection
Set Value = New Collection
L = Len(Temp1)
ZKH = 0
FKH = 0
NumArithmetic = 0
NumExponent = 0
NumLogic = 0
QB = ""
ZB = ""
HB = ""
IfLogic = False
IfBracket = False
IfArithmetic = False
IfFunction = False
For i = 1 To L
Temp = Mid$(Temp1, i, 1)
If Temp = "(" Then
IfBracket = True
ZKH = ZKH + 1
ElseIf Temp = ")" Then
IfBracket = True
FKH = FKH + 1
ElseIf Temp = "+" Or Temp = "-" Or Temp = "*" Or Temp = "/" Or Temp = "\" Then
IfArithmetic = True
NumArithmetic = NumArithmetic + 1
ElseIf Temp = "^" Then
IfFunction = True
NumExponent = NumExponent + 1
Else
End If
Next
If ZKH > FKH Then
ErrorString = "正括号和反括号没有成对配对!正括号比反括号多了" & ZKH - FKH & "个,请检查表达式!"
GoTo ProcExit
ElseIf ZKH < FKH Then
ErrorString = "正括号和反括号没有成对配对!正括号比反括号少了" & FKH - ZKH & "个,请检查表达式!"
GoTo ProcExit
End If
If L >= 6 Then
For i = 1 To L - 6
Temp = Mid$(Temp1, i, 6)
If Temp = "ArcSin" Or Temp = "ArcCos" Or Temp = "ArcAtn" Or Temp = "ArcTan" Then
IfFunction = True
NumExponent = NumExponent + 1
End If
Next
End If
If L >= 3 Then
For i = 1 To L - 3
Temp = Mid$(Temp1, i, 3)
If Temp = "Sin" Or Temp = "Cos" Or Temp = "Atn" Or Temp = "Tan" Or Temp = "Log" Or Temp = "Exp" Then
IfFunction = True
NumExponent = NumExponent + 1
ElseIf Temp = "And" Or Temp = "Xor" Or Temp = "Not" Then
IfLogic = True
NumLogic = NumLogic + 1
End If
Next
End If
If L >= 2 Then
For i = 1 To L - 2
Temp = Mid$(Temp1, i, 2)
If Temp = "Ln" Then
IfFunction = True
NumExponent = NumExponent + 1
ElseIf Temp = "Or" Then
IfLogic = True
NumLogic = NumLogic + 1
End If
Next
End If
'''''是否有括号'''''
If IfBracket = True Then
''''''(1)提取嘴里层括号的表达式
Temp1 = GetBracketExpression(Temp1)
' MsgBox "提取嘴里层括号的表达式为:" + Temp1, 64
ZKH = ZKH - 1
FKH = FKH - 1
If ZKH = 0 And FKH = 0 Then IfBracket = False
End If
' GetSignAndValue Temp1, True ''''提取表达式的运算副和数值
GetSignAndValue Temp1
'''''''(2)进行Not和阶乘运算''''
' Temp1 = NotOperation
' Temp1 = FactorialOperation
''''''(3)函数运算
Temp1 = FunctionOperation
''''''(4)全部函数运算完后,才能进行算术运算
Temp1 = ArithmeticOperation
''''''(5)进行逻辑运算''''''
Temp1 = LogicOperation
Temp1 = QB + Temp1 + HB
If QB = "" And HB = "" Then
IfComplate = True
Else
IfComplate = False
End If
Loop Until IfBracket = False And IfFunction = False And IfArithmetic = False And IfLogic = False And IfComplate = True Or ErrorString <> ""
If ErrorString <> "" Then
Operation = ErrorString
Else
If Len(Temp1) >= 1 Then
If Left$(Temp1, 1) = "+" Then
Temp1 = Right$(Temp1, Len(Temp1) - 1)
End If
End If
If IsMissing(Num) = False Then
If Num = 0 Then
Operation = Fix(Temp1)
Else
Temp = Replace$("0." & Space$(Num), " ", "0")
Operation = Format$(Temp1, Temp)
End If
Else
If Abs(CDbl(Temp1)) <= 1 Then
Temp = Replace$("0." & Space$(Len(Temp1) - 1), " ", "#")
Operation = Format$(Temp1, Temp)
Else
Operation = Temp1
End If
End If
End If
ProcExit:
Set Value = Nothing
Set Sign = Nothing
Exit Function
ProcError:
ErrorString = "表达式存在错误!"
Resume ProcExit
End Function