spc蜂毒面膜:在VB中如何实现混合运算

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 21:47:17
在文本框中输入算式,让它自动计算并得出结果。
详细点好吗

VB代码,输入表达式,返回数值,如果出错(表达式有问题),保存在ERRORIF中
Public Function GetValue(ByVal Expression As String) As Single
Dim IsUnary As Boolean, NextUnary As Boolean
Dim PaNum As Integer 'Number of Brackets
Dim Pos As Integer
Dim LenExpr As Integer
Dim MyStr As String, LeftExpr As String, RightExpr As String
Dim Value As String
Dim MyPos As Integer, MyPrec As Integer
Dim LeftStr As String, RightStr As String
Dim StdSng As Single, SngR As Single
Const StrNote As String = "+-*/%^\"

Expression = Trim$(Expression)
LenExpr = Len(Expression)

If LenExpr = 0 Then Exit Function
IsUnary = True: MyPrec = 11
For Pos = 1 To LenExpr
MyStr = Mid$(Expression, Pos, 1)
NextUnary = False
If MyStr = " " Then NextUnary = IsUnary
If Asc(MyStr) = 40 Then PaNum = PaNum + 1: NextUnary = True

If Asc(MyStr) = 41 Then
PaNum = PaNum - 1
NextUnary = True
End If

If PaNum = 0 Then
If InStr(1, StrNote, MyStr) <> 0 Then
NextUnary = True
Select Case MyStr
Case "^"
If MyPrec >= 9 Then MyPrec = 9: MyPos = Pos
Case "*", "/"
If MyPrec >= 8 Then MyPrec = 8: MyPos = Pos
Case "\"
If MyPrec >= 6 Then MyPrec = 6: MyPos = Pos
Case "%"
If MyPrec >= 5 Then MyPrec = 5: MyPos = Pos
Case "+", "-"
If (Not IsUnary) Or MyPrec >= 4 Then MyPrec = 4: MyPos = Pos
End Select
End If
End If
IsUnary = NextUnary
Next
'******************************************************************************************
If PaNum <> 0 Then ErrorIf = True
If MyPrec < 11 Then
LeftExpr = Left$(Expression, MyPos - 1): RightExpr = Right$(Expression, LenExpr - MyPos)
Select Case Mid$(Expression, MyPos, 1)
Case "^"
StdSng = GetValue(LeftExpr)
SngR = GetValue(RightExpr)
If StdSng < 0 Then
If Round(SngR / 2, 0) <> SngR / 2 Then
ErrorIf = True
Exit Function
End If
End If

GetValue = StdSng ^ GetValue(RightExpr)
Case "*"
GetValue = GetValue(LeftExpr) * GetValue(RightExpr)
Case "/"
StdSng = GetValue(RightExpr)
If StdSng = 0 Then ErrorIf = True: Exit Function
GetValue = GetValue(LeftExpr) / GetValue(RightExpr)
Case "\"
StdSng = GetValue(RightExpr)
If StdSng = 0 Then ErrorIf = True: Exit Function
GetValue = GetValue(LeftExpr) \ GetValue(RightExpr)
Case "%"
StdSng = GetValue(RightExpr)
If StdSng = 0 Then ErrorIf = True: Exit Function
GetValue = GetValue(LeftExpr) Mod GetValue(RightExpr)
Case "+"
GetValue = GetValue(LeftExpr) + GetValue(RightExpr)
Case "-"
GetValue = GetValue(LeftExpr) - GetValue(RightExpr)
End Select
Exit Function
End If
LeftStr = Left$(Expression, 1): RightStr = Right$(Expression, 1)
If LeftStr = "(" And RightStr = ")" Then
GetValue = GetValue(Mid$(Expression, 2, LenExpr - 2))
Exit Function
End If
If LeftStr = "-" Then
GetValue = -GetValue(Right$(Expression, LenExpr - 1))
Exit Function
End If
If Left$(Expression, 1) = "+" Then
GetValue = GetValue(Right$(Expression, LenExpr - 1))
Exit Function
End If
If LenExpr > 5 And RightStr = ")" Then
LeftExpr = LCase$(Left$(Expression, 4))
RightExpr = Mid$(Expression, 5, LenExpr - 5)
On Error Resume Next
Select Case LeftExpr
Case "sin("
GetValue = Sin(GetValue(RightExpr))
Exit Function
Case "cos("
GetValue = Cos(GetValue(RightExpr))
Exit Function
Case "tan("
GetValue = Tan(GetValue(RightExpr))
If Err.Number <> 0 Then: ErrorIf = True: Exit Function

Exit Function
Case "asn("
Value = GetValue(RightExpr)
If Value > 1 Or Value < -1 Then ErrorIf = True: Exit Function
If Value = -1 Then GetValue = -Pi / 2: Exit Function
If Value = 1 Then GetValue = Pi / 2: Exit Function
GetValue = Atn(Value / Sqr(1 - Value ^ 2))
Exit Function
Case "acs("
Value = GetValue(RightExpr)
If Value > 1 Or Value < -1 Then ErrorIf = True: Exit Function
If Value = -1 Then GetValue = Pi: Exit Function
If Value = 1 Then GetValue = 0: Exit Function
GetValue = Atn(-Value / Sqr(1 - Value ^ 2)) + Pi / 2
Exit Function
Case "lnn("
GetValue = Log(GetValue(RightExpr))
If Err.Number <> 0 Then: ErrorIf = True: Exit Function
Exit Function
Case "log("
GetValue = Log(GetValue(RightExpr)) / Log(10)
If GetValue(RightExpr) <= 0 Then ErrorIf = True: Exit Function
Exit Function
Case "abs("
GetValue = Abs(GetValue(RightExpr))
Exit Function
Case "rnd("
Randomize
GetValue = Rnd * GetValue(RightExpr)
Exit Function
Case "atn("
GetValue = Atn(GetValue(RightExpr))
Exit Function
Case "sqr("
GetValue = Sqr(GetValue(RightExpr))
If GetValue(RightExpr) < 0 Then: ErrorIf = True: Exit Function
Exit Function
Case "exp("
GetValue = Exp(GetValue(RightExpr))
If Err.Number <> 0 Then: ErrorIf = True: Exit Function
Case "sum("
GetValue = CalcClip(RightExpr, 1)
Exit Function
Case "ave("
GetValue = CalcClip(RightExpr, 2)
Exit Function
Case "max("
GetValue = CalcClip(RightExpr, 3)
Exit Function
Case "min("
GetValue = CalcClip(RightExpr, 4)
Exit Function
Case "mid("
GetValue = CalcClip(RightExpr, 5)
Exit Function
Case "fan("
GetValue = CalcClip(RightExpr, 6)
Exit Function
Case "bao("
GetValue = CalcClip(RightExpr, 7)
Exit Function

Case "sgn("
GetValue = Sgn(GetValue(RightExpr))
Exit Function
End Select
End If

If Expression = "x" Then
GetValue = TempValue
Exit Function
End If

'
On Error Resume Next
GetValue = CSng(Expression)
On Error GoTo 0
If Err.Number <> 0 Then
ErrorIf = True
Err.Clear
End If

End Function

这个问题很难,需要分析你的算式,那就是简单的人工智能了,其实,作一个软件,编一个程序,先要有一个思路,确定一个算法(不是计算方法,而是一堆用来实行你的软件的功能的逻辑)来完成你要的功能,也许有变通的办法。我认为你的算式可以分几个文本框来完成,其中有参数,还有加减乘除,这样就简单多了

可以用递归。

高难度