| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4666 人关注过本帖
标题:求教,四则运算计算器如何实现?
只看楼主 加入收藏
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
'''''''''''主运算函数'''''''''''

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

2008-09-13 00:27
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
'''''''''(1)双目运算''''''''''

'''加法运算'''''
Public Function Addition(Number1 As Double, Number2 As Double) As Double
    Addition = Number1 + Number2
   
End Function

'''减法运算'''''
Public Function Subtration(Number1 As Double, Number2 As Double) As Double
    Subtration = Number1 - Number2
   
End Function

'''乘法运算'''''
Public Function Multiplication(Number1 As Double, Number2 As Double) As Double
    Multiplication = Number1 * Number2
   
End Function

'''浮点除法运算'''''
Public Function Division(Number1 As Double, Number2 As Double) As Double
    If Number2 = 0 Then
        MsgBox "进行除法运算时,除数不能为零!", 48, "浮点整除运算"
        Me.ErrorString = "进行除法运算时,除数不能为零!"
        
    Else
        Division = Number1 / Number2
   
    End If

End Function

2008-09-13 00:29
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
'''''''''(2)单目运算''''''''''''''

'''非运算'''''
Public Function NotOpertor(Number As Double) As Double
    NotOpertor = Not Number

End Function

'''正弦运算'''''
Public Function Sin(Number As Double) As Double
    Sin = Math.Sin(Me.DegreeToRas(Number))

End Function

'''余弦运算'''''
Public Function Cos(Number As Double) As Double
    Cos = Math.Cos(Me.DegreeToRas(Number))

End Function

'''正切运算'''''
Public Function Tan(Number As Double) As Double
    Tan = Math.Tan(Me.DegreeToRas(Number))

End Function

'''余切运算'''''
Public Function Ctg(Number As Double) As Double
    If Number = 0 Then
        MsgBox "进行于切运算时,自变量不能为零!", 48
        Me.ErrorString = "进行于切运算时,自变量不能为零!"
        
    Else
        Ctg = 1 / Math.Tan(Me.DegreeToRas(Number))
   
    End If
   
End Function

'''反正弦运算'''''
Public Function ArcSin(Number As Double) As Double
    If Number > 1 Or Number < -1 Then
        MsgBox "在进行反正弦运算时,输入的数字只能“1≤x≤-1”!", 48, "反正弦运算"
        Me.ErrorString = "在进行反正弦运算时,输入的数字只能“1≤x≤-1”!"
        
    Else
        If Number = 1 Then
            ArcSin = 90
            
        ElseIf Number = -1 Then
            ArcSin = -90
            
        Else
            ArcSin = Me.RasToDegree(Math.Atn(Number / Sqr(1 - Number ^ 2)))
        
        End If
        
    End If
   
End Function

2008-09-13 00:30
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
''''''''获取字符串右端数值或操作符的操作状态''''''''''

Public Function GetOperationState(ByVal Expression As String) As OperateStateEnum
Dim MSG As String
Dim Msg1 As String
Dim Msg2 As String
Dim Msg3 As String
Dim Msg6 As String

Dim i As Integer
Dim L As Integer
Dim k As Integer
Dim IsExistS As Boolean

On Error Resume Next
   
    Me.IsExistDot = False
    Expression = Replace$(Expression, " ", "")
   
    If Expression = "" Then
        GetOperationState = 无
        Exit Function
        
    End If
   
    L = Len(Expression)
   
    MSG = Right(Expression, 1)
    Msg1 = Mid(Expression, L, 1)
    Msg2 = Mid(Expression, L - 1, 2)
    Msg3 = Mid(Expression, L - 2, 3)
    Msg6 = Mid(Expression, L - 5, 6)
   
    i = Asc(MSG)
   
    If Me.IsExistConst(Expression, True) = True Then
        GetOperationState = 常数
   
    ElseIf Me.IsExistVariant(Expression, True) = True Then
        GetOperationState = 变量
        
    ElseIf i >= 48 And i <= 57 Or i = 46 Then  '''如果最后一个为数字或小数点
        L = Len(Expression)
        Msg1 = ""
        
        For i = L To 1 Step -1
            MSG = Mid(Expression, i, 1)
            k = Asc(MSG)
            If (k >= 48 And k <= 57 Or k = 46 Or MSG = "-") And IsExistS = False Then
                If MSG = "-" Then IsExistS = True
               
                If Msg1 = "" Then
                    Msg1 = MSG
                    
                Else
                    Msg1 = MSG & Msg1
               
                End If
               
                If k = 46 Then
                    Me.IsExistDot = True
                    
                End If
               
            Else
                Exit For
               
            End If
            
        Next

        If Msg1 = "" Then
            GetOperationState = 无
            
        Else
            GetOperationState = 数字
        
        End If
        
    ElseIf MSG = "(" Then
        GetOperationState = 前括号
        
    ElseIf MSG = ")" Then
        GetOperationState = 后括号
        
    ElseIf Msg1 = "+" Then
        GetOperationState = 加法运算
        
    ElseIf Msg1 = "-" Then
        GetOperationState = 减法运算
        
    ElseIf Msg1 = "*" Then
        GetOperationState = 乘法运算
        
    ElseIf Msg1 = "^" Then
        GetOperationState = 幂
        
    ElseIf Msg1 = "/" Then
        GetOperationState = 浮点除法运算
        
    ElseIf Msg1 = "\" Then
        GetOperationState = 整除运算
        
    ElseIf Msg1 = "!" Then
        GetOperationState = 阶乘运算
        
    ElseIf Msg2 = "Or" Then
        GetOperationState = 或运算
        
    ElseIf Msg2 = "Ln" Then
        GetOperationState = 对数运算
        
    ElseIf Msg3 = "And" Then
        GetOperationState = 与运算
      
    ElseIf Msg3 = "Not" Then
        GetOperationState = 非运算
   
    ElseIf Msg3 = "Mod" Then
        GetOperationState = 取模运算
   
    ElseIf Msg3 = "Not" Then
        GetOperationState = 非运算
   
    ElseIf Msg3 = "Xor" Then
        GetOperationState = 异或运算
        
    ElseIf Msg3 = "Log" Or Msg3 = "Exp" Then
        GetOperationState = 对数运算
        
    ElseIf Msg6 = "Arcsin" Then
        GetOperationState = 反正弦运算
        
    ElseIf Msg6 = "Arccos" Then
      GetOperationState = 反余弦运算
         
    ElseIf Msg6 = "Arctan" Then
        GetOperationState = 反正切运算
   
    ElseIf Msg6 = "Arcctg" Then
        GetOperationState = 反于切运算
        
    ElseIf Msg3 = "Sin" Then
        GetOperationState = 正弦运算
        
    ElseIf Msg3 = "Cos" Then
        GetOperationState = 余弦运算
        
    ElseIf Msg3 = "Tan" Then
        GetOperationState = 正切运算
        
    ElseIf Msg3 = "Ctg" Then
        GetOperationState = 于切运算
   
    ElseIf Me.IsExistVariant(Expression, True) = True Then
        GetOperationState = 变量
        
        
    Else
        GetOperationState = 未知
        
    End If

End Function

2008-09-13 00:33
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
''''''''''替换函数(表达式)中的变量''''''''''''

Public Function ReplaceVariable(ByVal Expression As String, Optional RecorndIndex As Long = -2, Optional Isdisplay As Boolean = False) As String
Dim Db1 As New ClsADOOperation
Dim x As Variant
Dim MSG As String
Dim colV As New Collection
Dim colValue As New Collection
Dim i As Integer

    If Len(Expression) <= 0 Then
        Exit Function
        
    End If
   
    MSG = Me.ReplaceFunctionName(Expression)      ''''获取函数(表达式)中的函数名称集合并进行替换成“,”
    If Trim$(MSG) = "," Or MSG = "" Then
        Exit Function

    End If
   
    '''''''打开常数表,查找并替换''''''''''''
        
    Set Db1 = New ClsADOOperation
    Db1.Path = App.Path & "\Calc.mdb"
   
    Db1.LinkString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Db1.Path & ";Persist Security Info=False"
    Db1.UserName = "admin"
    Db1.PassWord = ""
   
    Db1.FieldName = "*"
    Db1.Condition = ""
    Db1.OrderBy = ""
    Db1.GroupBy = ""
   
    Db1.SQL = "select * from const"
    Set Db1.Recordset = Db1.GetRecordset
   
'    MsgBox Db1.Recordset.RecordCount, 48
   
    Do Until Db1.Recordset.EOF
        MSG = Replace$(MSG, Db1.Recordset.Fields(1), Db1.Recordset.Fields(2))
        
        Db1.Recordset.MoveNext
   
    Loop
   
    If Isdisplay = True Then
        MsgBox "原函数(表达式)“" & Expression & "”全部常数替换为:" & MSG
   
    End If

    '''''''打开函数(表达式),查找并替换''''''
   
'    If RecorndIndex = -2 Then
'        GoTo 10
'
'    End If
   
    Db1.SQL = "select * from bds"
    Set Db1.Recordset = Db1.GetRecordset
   
'    MsgBox Db1.Recordset.RecordCount, 48
   
   
'    If RecorndIndex <> -2 Then
'        Db1.Recordset.Move RecorndIndex
'
'    End If
   
    Do Until Db1.Recordset.EOF
        i = 1
        
'        MsgBox Db1.Recordset.Fields(2)
        Set colV = Me.GetVariable(Db1.Recordset.Fields(2))
        Set colValue = Me.GetVariableValue(Db1.Recordset.Fields(2))
        
'        MsgBox "colV.Count=" & colV.Count
'        MsgBox "colValue.Count=" & colValue.Count
        
        
        For Each x In colV
            MSG = Replace$(MSG, x, colValue(i))
        
            i = i + 1
            
        Next
        
        Db1.Recordset.MoveNext
        
    Loop
   
    If Isdisplay = True Then
        MsgBox "原函数(表达式)“" & Expression & "”全部变量替换为:" & MSG
   
    End If
   
10:
   
    '''''''''将标准函数将空格填充上''''''''
    For Each x In Me.FunctionCollection
        MSG = Replace$(MSG, ",", x, 1, 1)
   
    Next
   
    If Isdisplay = True Then
        MsgBox "原函数(表达式)“" & Expression & "”替换为:" & MSG
   
    End If
   
    ReplaceVariable = MSG
   
End Function

''''''''''替换函数(表达式)中的函数名称''''''''''''

Public Function ReplaceFunctionName(ByVal Expression As String) As String
Dim colSign As New Collection
Dim x As Variant

    If Len(Expression) <= 0 Then
        Exit Function
        
    End If
   
    Set colSign = Me.GetFunctionCollection(Expression)
   
    For Each x In colSign
        Expression = Replace$(Expression, x, ",", 1, 1)

    Next
   
    ReplaceFunctionName = Expression
   
    Set colSign = Nothing
   
End Function



''''''''''获取标准函数集合''''''''''''
Public Function GetFunctionCollection(Expression As String, Optional Isdisplay As Boolean = False) As Collection
Dim L As Integer
Dim Msg1 As String
Dim Msg2 As String
Dim Msg3 As String
Dim Msg6 As String
Dim i As Integer

On Error Resume Next
   
    Set GetFunctionCollection = New Collection
    Set Me.FunctionCollection = New Collection
    Set Me.FunctionPosition = New Collection
    Set Me.FunctionLenght = New Collection
   
    If Expression = "" Then
        Exit Function
   
    End If
   
    L = Len(Expression)
   
    For i = 1 To L
        If L = 1 Then   '''''''取出字符串
            Msg1 = Expression
            Msg2 = ""
            Msg3 = ""
            Msg6 = ""
            
        ElseIf L <= 2 Then
            Msg1 = Mid(Expression, i, 1)
            Msg2 = Expression
            Msg3 = ""
            Msg6 = ""
        
        ElseIf L <= 3 Then
            Msg1 = Mid(Expression, i, 1)
            Msg2 = Mid(Expression, i, 2)
            Msg3 = Expression
            Msg6 = ""
        
        ElseIf L <= 6 Then
            Msg1 = Mid(Expression, i, 1)
            Msg2 = Mid(Expression, i, 2)
            Msg3 = Mid(Expression, i, 3)
            Msg6 = Expression
        
        Else
            Msg1 = Mid(Expression, i, 1)
            Msg2 = Mid(Expression, i, 2)
            Msg3 = Mid(Expression, i, 3)
            Msg6 = Mid(Expression, i, 6)
            
        End If
   
        If Msg1 = "!" Then
            GetFunctionCollection.Add Msg1  ''''函数名称
            Me.FunctionPosition.Add i       ''''函数位置
            Me.FunctionLenght.Add 1         ''''函数长度
            
        ElseIf Msg2 = "Ln" Or Msg2 = "Or" Then
            GetFunctionCollection.Add Msg2  ''''函数名称
            Me.FunctionPosition.Add i       ''''函数位置
            Me.FunctionLenght.Add 2         ''''函数长度
            
            i = i + 2
            
        ElseIf Msg3 = "Sin" Or Msg3 = "Cos" Or Msg3 = "Tan" Or Msg3 = "Ctg" Or Msg3 = "Log" Or Msg3 = "Exp" Or Msg3 = "And" Or Msg3 = "Not" Or Msg3 = "Mod" Or Msg3 = "Xor" Then
            GetFunctionCollection.Add Msg3  ''''函数名称
            Me.FunctionPosition.Add i       ''''函数位置
            Me.FunctionLenght.Add 3         ''''函数长度
            
            i = i + 3
            
        ElseIf Msg6 = "Arcsin" Or Msg6 = "Arccos" Or Msg6 = "Arctan" Or Msg6 = "Arcctg" Then
            GetFunctionCollection.Add Msg6  ''''函数名称
            Me.FunctionPosition.Add i       ''''函数位置
            Me.FunctionLenght.Add 6         ''''函数长度
            
            i = i + 6
            
        Else
            
        
        End If
        
    Next
   
    Set Me.FunctionCollection = GetFunctionCollection
   
    If GetFunctionCollection.Count > 0 Then
        Me.IsExistFunction = True
   
    Else
        Me.IsExistFunction = False
        
    End If
   
    If Isdisplay = True Then
        Dim x As Variant
        
        Msg1 = ""
        i = 1
        
        For Each x In Me.FunctionCollection
            If Msg1 = "" Then
                Msg1 = x & "(P = " & Me.FunctionPosition(i) & ",L = " & Me.FunctionLenght(i) & ")"
               
            Else
                Msg1 = Msg1 & "," & x & "(P = " & Me.FunctionPosition(i) & ",L = " & Me.FunctionLenght(i) & ")"
            
            End If
            i = i + 1
            
        Next
        
        MsgBox "在函数(表达式)“" & Expression & "”中的函数为:" & Msg1
        
    End If
   
End Function

2008-09-13 00:37
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
'''''''(3)拼接表达式''''''

Private Function CombinExpression(Optional IfDisPlay As Boolean = False) As String
Dim i As Integer
Dim x As Variant
Dim IfDM As Boolean
Dim V1 As String
   
On Error GoTo ProcExit

    V1 = ""
    i = 1
    IfDM = False
   
'    MsgBox "总共有数值" & Value.Count & "个!", 64, "拼接表达式"
'    MsgBox "总共有符号" & Sign.Count & "个", 64, "拼接表达式"
   
    For Each x In Sign
        If x = "Sin" Or x = "Cos" Or x = "Ctg" Or x = "Tan" Or x = "Exp" Or x = "Ln" Or x = "Log" _
        Or x = "Arcsin" Or x = "Arccos" Or x = "Arctan" Or x = "Arcctg" _
        Or x = "Not" Then '''单目
        
            V1 = V1 + x + Value.Item(i)
            i = i + 1
            IfDM = True
            
        ElseIf x = "!" Then
            V1 = V1 + Value.Item(i) + x
            i = i + 1
            IfDM = True
            
        Else    '''双目
            If IfDM = True Then
                V1 = V1 + x
               
            Else
                V1 = V1 + Value.Item(i) + x
                i = i + 1
               
            End If
            IfDM = False
            
        End If
        
    Next
    If IfDM = False Then V1 = V1 + Value.Item(i)
   
    CombinExpression = V1
    If IfDisPlay = True Then
        MsgBox "拼接的表达式为:" & V1, 64, "拼接表达式"
   
    End If
   
ProcExit:
    Exit Function
   
ProcError:
    ErrorString = "表达式存在错误!"
    Resume ProcExit
   
End Function

2008-09-13 00:41
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
'''''''''''''提取括号内的表达式''''''''

Public Function GetBracketExpression(ByVal mString As String) As String
Dim Temp1 As String
Dim Temp As String
Dim P1 As Integer
Dim P2 As Integer

Dim i As Integer
Dim L As Integer
   
On Error GoTo ProcExit

    Temp1 = mString
    L = Len(Temp1)
   
    For i = 1 To L
        Temp = Mid$(Temp1, i, 1)
        If Temp = "(" Then
            P1 = i
            
        ElseIf Temp = ")" Then
            P2 = i
            Exit For
            
        End If
        
    Next
   
    QB = Left$(mString, P1 - 1)
    HB = Right$(mString, L - P2)
   
    ZB = Mid$(mString, P1 + 1, P2 - P1 - 1)
    GetBracketExpression = ZB
   
ProcExit:
    Exit Function
   
ProcError:
    ErrorString = "表达式错误!"
    Resume ProcExit
   
End Function

2008-09-13 00:41
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
''''''''函数运算

Private Function FunctionOperation() As String
Dim Temp1 As String
Dim x As Variant
Dim i As Integer

'Dim F As String
Dim V As String
Dim V1 As String
'Dim Msg As String

On Error GoTo ProcExit

    i = 1
   
    For Each x In Sign      ''''单目运算
        If x = "Sin" Or x = "Cos" Or x = "Tan" Or x = "Atn" Or x = "Log" Or x = "Exp" Or x = "Ln" Or _
            x = "Arcsin" Or x = "Arccos" Or x = "Arctan" Or x = "ArcActg" Or _
            x = "Not" Or x = "!" Then
            
            If x = "Sin" Then
                Temp1 = Me.Sin(Val(Value.Item(i)))
            
            ElseIf x = "Cos" Then
                Temp1 = Me.Cos(Val(Value.Item(i)))
               
            ElseIf x = "Tan" Then
                Temp1 = Me.Tan(Val(Value.Item(i)))
            
            ElseIf x = "Ctg" Then
                Temp1 = Me.Ctg(Val(Value.Item(i)))
            
            ElseIf x = "Log" Then
                Temp1 = Me.Log(Val(Value.Item(i)))
            
            ElseIf x = "Exp" Then
                Temp1 = Me.Exp(Val(Value.Item(i)))
               
            ElseIf x = "Ln" Then
                Temp1 = Me.Ln(Val(Value.Item(i)))
            
            ElseIf x = "Arcsin" Then
                Temp1 = Me.ArcSin(Val(Value.Item(i)))
               
            ElseIf x = "Arccos" Then
                Temp1 = Me.ArcCos(Val(Value.Item(i)))
               
            ElseIf x = "Arctan" Then
                Temp1 = Me.ArcTan(Val(Value.Item(i)))
               
            ElseIf x = "Arcctg" Then
                Temp1 = Me.ArcCtg(Val(Value.Item(i)))
               
            ElseIf x = "Not" Then
                Temp1 = Me.NotOpertor(Val(Value.Item(i)))
               
            ElseIf x = "!" Then
                Temp1 = Me.Factorial(Val(Value.Item(i)))
               
            Else
            End If
            
            Value.Add Temp1, , , i
            Value.Remove i
            
            Sign.Remove i
            i = i - 1
            NumExponent = NumExponent - 1
            
        End If
        i = i + 1
        
    Next
   
    i = 1
    For Each x In Sign          '''双目运算,"^"
        If x = "^" Then
            V = Value.Item(i)
            V1 = Value.Item(i + 1)
            
            Temp1 = Val(Value.Item(i)) ^ Val(Value.Item(i + 1))
            Value.Remove i + 1
            
            Value.Add Temp1, , , i
            Sign.Remove i
            Value.Remove i
            i = i - 1
            NumExponent = NumExponent - 1
        End If
        i = i + 1
    Next
    If NumExponent = 0 Then
        IfFunction = False
   
    End If
   
    '''''''(3)拼接表达式''''''
'    Temp1 = CombinExpression(True)
   Temp1 = CombinExpression(False)
    FunctionOperation = Temp1
            
ProcExit:
    Exit Function
   
ProcError:
    ErrorString = "表达式存在错误!"
    Resume ProcExit
   
End Function

'''''''算术运算

Private Function ArithmeticOperation() As String
Dim Temp1 As String
Dim x As Variant
Dim xx As Variant

Dim i As Integer

'Dim F As String
'Dim V As String
'Dim V1 As String
'Dim Msg As String

On Error GoTo ProcError

    i = 1
   
    For Each x In Sign
        If x = "*" Or x = "/" Or x = "\" Then   ''''完成乘除运算
            If x = "*" Then
                Temp1 = Val(Value.Item(i)) * Val(Value.Item(i + 1))
               
            ElseIf x = "/" Then
                Temp1 = Val(Value.Item(i)) / Val(Value.Item(i + 1))
            
            ElseIf x = "\" Then
                Temp1 = Val(Value.Item(i)) \ Val(Value.Item(i + 1))
               
            End If
            Value.Remove i + 1
            
            Value.Add Temp1, , , i
            Sign.Remove i
            Value.Remove i
            i = i - 1
            NumArithmetic = NumArithmetic - 1
        
        End If
        i = i + 1
        
    Next
   
    i = 1
   
    For Each x In Sign      '''完成+-运算
        If x = "+" Or x = "-" Then
            If x = "+" Then
                Temp1 = Val(Value.Item(i)) + Val(Value.Item(i + 1))
            
            ElseIf x = "-" Then
                Temp1 = Val(Value.Item(i)) - Val(Value.Item(i + 1))
               
            End If
            
            Value.Remove i + 1
            Value.Add Temp1, , , i
            Sign.Remove i
            Value.Remove i
            i = i - 1
            NumArithmetic = NumArithmetic - 1
        End If
        
        i = i + 1
        
    Next
   
    If Sign.Count = 0 Then
        IfArithmetic = False
        
    End If
   
   
    '''''''(3)拼接表达式''''''
'    Temp1 = CombinExpression(True)
    Temp1 = CombinExpression(False)
    ArithmeticOperation = Temp1
   
ProcExit:
    Exit Function
   
ProcError:
    ErrorString = "表达式存在错误!"
    Resume ProcExit
   
End Function

''''''''''''逻辑运算''''''''''''

Private Function LogicOperation() As String
Dim Temp1 As String
Dim x As Variant
Dim i As Integer

'Dim F As String
'Dim V As String
'Dim V1 As String
'Dim Msg As String

On Error GoTo ProcError

    i = 1
    For Each x In Sign          '''双目And运算,"^"
        If x = "And" Or x = "Or" Or x = "Xor" Then
            If x = "And" Then
                Temp1 = Val(Value.Item(i)) And Val(Value.Item(i + 1))
            
            ElseIf x = "Or" Then
                Temp1 = Val(Value.Item(i)) Or Val(Value.Item(i + 1))
               
            ElseIf x = "Xor" Then
                Temp1 = Val(Value.Item(i)) Xor Val(Value.Item(i + 1))
               
            End If
            
            Value.Remove i + 1
            Value.Add Temp1, , , i
            Sign.Remove i
            Value.Remove i
            i = i - 1
            NumLogic = NumLogic - 1
        End If
        i = i + 1
    Next
    If NumLogic = 0 Then
        IfLogic = False
   
    End If
   
    '''''''(3)拼接表达式''''''
'    Temp1 = CombinExpression(True)
   Temp1 = CombinExpression(False)
    LogicOperation = Temp1
   
ProcExit:
    Exit Function
   
ProcError:
    ErrorString = "表达式存在错误!"
    Resume ProcExit
   
End Function


'''''''''''获取变量集合''''''''
Public Function GetVariable(VariableExpression As String, Optional IfDisPlay As Boolean = False) As Collection
Dim Ve() As String
Dim i As Integer
Dim MSG As String
Dim x As Variant

    Ve = Split(Replace$(VariableExpression, " ", ""), ",")
    Set GetVariable = New Collection
   
    For Each x In Ve
        i = InStr(1, x, "=") - 1
        MSG = Left$(x, i)
        GetVariable.Add MSG
        
    Next
   
    If IfDisPlay = True Then
        MSG = ""
        For Each x In GetVariable
            If MSG = "" Then
                MSG = x
               
            Else
                MSG = MSG & "," & x
            
            End If
            
        Next
        
        MsgBox "获取变量集合为:" & MSG
        
    End If
   
End Function


'''''''''''获取变量值集合''''''''
Public Function GetVariableValue(VariableExpression As String, Optional IfDisPlay As Boolean = False) As Collection
Dim Ve() As String
Dim L As Integer
Dim i As Integer
Dim MSG As String
Dim x As Variant

    Ve = Split(Replace$(VariableExpression, " ", ""), ",")
    Set GetVariableValue = New Collection
   
    For Each x In Ve
        i = Len(x) - InStr(1, x, "=")
        MSG = Right$(x, i)
        GetVariableValue.Add MSG
        
    Next
   
    If IfDisPlay = True Then
        MSG = ""
        For Each x In GetVariableValue
            If MSG = "" Then
                MSG = x
               
            Else
                MSG = MSG & "," & x
            
            End If
            
        Next
        
        MsgBox "获取变量值集合为:" & MSG
        
    End If
   
End Function

2008-09-13 00:43
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
''''''''''''''获取表达式左端或右端常数的长度'''''''''''''

Public Function GetConstLenght(Expression As String, Optional IsGetRight As Boolean = True) As Integer
Dim Db1 As New ClsADOOperation
Dim L As Integer
Dim MSG As String

        
    Set Db1 = New ClsADOOperation
    Db1.Path = App.Path & "\Calc.mdb"
   
    Db1.LinkString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Db1.Path & ";Persist Security Info=False"
    Db1.UserName = "admin"
    Db1.PassWord = ""
   
    Db1.FieldName = "*"
    Db1.Condition = ""
    Db1.OrderBy = ""
    Db1.GroupBy = ""
   
    Db1.SQL = "select * from const"
    Set Db1.Recordset = Db1.GetRecordset
   
'    MsgBox Db1.Recordset.RecordCount, 48
   
    Do Until Db1.Recordset.EOF
        L = Len(Db1.Recordset.Fields(1))
        
        If IsGetRight = True Then
            MSG = Right$(Expression, L)
            
        Else
            MSG = Left$(Expression, L)
        
        End If
        
        If MSG = Db1.Recordset.Fields(1) Then
            GetConstLenght = L
            Exit Do
        
        End If
        
        Db1.Recordset.MoveNext
   
    Loop
   
End Function

''''''''''''''获取表达式左端或右端变量的长度'''''''''''''

Public Function GetVariantLenght(Expression As String, Optional IsGetRight As Boolean = True) As Integer
Dim Db1 As New ClsADOOperation
Dim L As Integer
Dim MSG As String
Dim x As Variant
Dim colV As New Collection
        
    Set Db1 = New ClsADOOperation
    Db1.Path = App.Path & "\Calc.mdb"
   
    Db1.LinkString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Db1.Path & ";Persist Security Info=False"
    Db1.UserName = "admin"
    Db1.PassWord = ""
   
    Db1.FieldName = "*"
    Db1.Condition = ""
    Db1.OrderBy = ""
    Db1.GroupBy = ""
        
    Db1.SQL = "select * from bds"
    Set Db1.Recordset = Db1.GetRecordset
   
'    MsgBox Db1.Recordset.RecordCount, 48
   
    Do Until Db1.Recordset.EOF
        Set colV = Me.GetVariable(Db1.Recordset.Fields(2))     '''获取变量集合
        
        For Each x In colV
            L = Len(x)
            
            If IsGetRight = True Then
                MSG = Right$(Expression, L)
               
            Else
                MSG = Left$(Expression, L)
            
            End If
            
            If colV.Count > 0 And MSG = x Then
                GetVariantLenght = L
                Exit Do
            
            End If

        
        Next

        Db1.Recordset.MoveNext

    Loop
   
    Set colV = Nothing
End Function

''''''''''''''获取表达式左端或右端常数的名称'''''''''''''

Public Function GetConstName(Expression As String, Optional IsGetRight As Boolean = True) As String
Dim Db1 As New ClsADOOperation
Dim L As Integer
Dim MSG As String

        
    Set Db1 = New ClsADOOperation
    Db1.Path = App.Path & "\Calc.mdb"
   
    Db1.LinkString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Db1.Path & ";Persist Security Info=False"
    Db1.UserName = "admin"
    Db1.PassWord = ""
   
    Db1.FieldName = "*"
    Db1.Condition = ""
    Db1.OrderBy = ""
    Db1.GroupBy = ""
   
    Db1.SQL = "select * from const"
    Set Db1.Recordset = Db1.GetRecordset
   
'    MsgBox Db1.Recordset.RecordCount, 48
   
    Do Until Db1.Recordset.EOF
        L = Len(Db1.Recordset.Fields(1))
        
        If IsGetRight = True Then
            MSG = Right$(Expression, L)
            
        Else
            MSG = Left$(Expression, L)
        
        End If
        
        If MSG = Db1.Recordset.Fields(1) Then
            GetConstName = MSG
            Exit Do
        
        End If
        
        Db1.Recordset.MoveNext
   
    Loop
   
End Function

''''''''''''''获取表达式左端或右端变量的名称'''''''''''''

Public Function GetVariantName(Expression As String, Optional IsGetRight As Boolean = True) As String
Dim Db1 As New ClsADOOperation
Dim L As Integer
Dim MSG As String
Dim x As Variant
Dim colV As New Collection
        
    Set Db1 = New ClsADOOperation
    Db1.Path = App.Path & "\Calc.mdb"
   
    Db1.LinkString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Db1.Path & ";Persist Security Info=False"
    Db1.UserName = "admin"
    Db1.PassWord = ""
   
    Db1.FieldName = "*"
    Db1.Condition = ""
    Db1.OrderBy = ""
    Db1.GroupBy = ""
        
    Db1.SQL = "select * from bds"
    Set Db1.Recordset = Db1.GetRecordset
   
'    MsgBox Db1.Recordset.RecordCount, 48
   
    Do Until Db1.Recordset.EOF
        Set colV = Me.GetVariable(Db1.Recordset.Fields(2))     '''获取变量集合
        
        For Each x In colV
            L = Len(x)
            
            If IsGetRight = True Then
                MSG = Right$(Expression, L)
               
            Else
                MSG = Left$(Expression, L)
            
            End If
            
            If colV.Count > 0 And MSG = x Then
                GetVariantName = MSG
                Exit Do
            
            End If

        
        Next

        Db1.Recordset.MoveNext

    Loop
   
    Set colV = Nothing
End Function

2008-09-13 00:45
hyhhd
Rank: 2
等 级:论坛游民
威 望:1
帖 子:502
专家分:44
注 册:2006-5-12
收藏
得分:0 
我觉得编该类程序,几乎全部都是字符串的处理。

2008-09-13 00:46
快速回复:求教,四则运算计算器如何实现?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020090 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved