天下一家·MJ

博客园 首页 新随笔 联系 订阅 管理

根据老师上课讲的方法,完成了这些代码:

下面代码基于StackClass.cls这个堆栈类,具体内容参见本博客其他博文。

代码如下:

Public WorkStack As New StackClass
Public Function CalcStringEx(ByVal numStr As String) As String '完成
    Dim i As Long
    Dim opCode() As String
'    Stop
    Dim ops1 As String, ops2 As String, opC As String '计算符表示,【保留】
    '初始化堆栈
        WorkStack.Clear '保留
    '堆栈初始化结束
    opCode = Split(numStr, ",")
'    Stop
    For i = 0 To UBound(opCode)
        If IsSymbolEx(opCode(i)) Then
'        Stop
            op2 = WorkStack.Pop
            op1 = WorkStack.Pop
            If CDbl(op2) = 0 And opCode(i) = "/" Then
                CalcStringEx = "除数为 0 的错误发生!"
                Exit Function
            Else
                WorkStack.Push CStr(Calc(op1, op2, opCode(i)))
            End If
        Else
'        Stop
            WorkStack.Push opCode(i)
        End If
    Next i
    CalcStringEx = WorkStack.Pop
End Function
Public Function TrueBackToMid(ByVal strBack As String) As String
    Dim i As Long
    Dim opCode() As String
'    Stop
    Dim ops1 As String, ops2 As String, opC As String '计算符表示,【保留】
    '初始化堆栈
        WorkStack.Clear '保留
    '堆栈初始化结束
    opCode = Split(strBack, ",")
'    Stop
    For i = 0 To UBound(opCode)
        If IsSymbolEx(opCode(i)) Then
'        Stop
            op2 = WorkStack.Pop
            op1 = WorkStack.Pop
            WorkStack.Push "(" & op1 & opCode(i) & op2 & ")"
        Else
'        Stop
            WorkStack.Push opCode(i)
        End If
    Next i
    op1 = WorkStack.Pop
    If Len(op1) > 2 Then
        op1 = Mid(op1, 2, Len(op1) - 2)
    End If
    
    TrueBackToMid = op1
End Function
Public Function IsSymbolEx(ByVal strs As String) As Boolean
    IsSymbolEx = True
    Select Case strs
        Case "+"
        Case "-"
        Case "*"
        Case "/"
        Case Else
            IsSymbolEx = False
    End Select
End Function
Public Function Calc(ByVal op1 As String, ByVal op2 As String, ByVal options As String) As Double
On Error Resume Next
Calc = 0
Select Case options
    Case "+"
        Calc = CDbl(op1) + CDbl(op2)
    Case "-"
        Calc = CDbl(op1) - CDbl(op2)
    Case "*"
        Calc = CDbl(op1) * CDbl(op2)
    Case "/"
        Calc = CDbl(op1) / CDbl(op2)
End Select
End Function

Public Function EnumeBy(ByVal ar1 As String, ByVal ar2 As String, ByVal ar3 As String, ByVal ar4 As String) As String
    Dim strs() As String '生成的一堆表达式
    Dim outStr As String
    Dim iCount As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    ReDim strs(iCount)
    For i = 1 To 4
        For j = 1 To 4
            For k = 1 To 4
                ReDim Preserve strs(iCount)
                strs(iCount) = ar1 & "," & ar2 & "," & GetSign(i) & "," & ar3 & "," & GetSign(j) & "," & ar4 & "," & GetSign(k)
                iCount = iCount + 1
                ReDim Preserve strs(iCount)
                strs(iCount) = ar1 & "," & ar2 & "," & GetSign(i) & "," & ar3 & "," & ar4 & "," & GetSign(j) & "," & GetSign(k)
                iCount = iCount + 1
                ReDim Preserve strs(iCount)
                strs(iCount) = ar1 & "," & ar2 & "," & ar3 & "," & GetSign(i) & "," & GetSign(j) & "," & ar4 & "," & GetSign(k)
                iCount = iCount + 1
                ReDim Preserve strs(iCount)
                strs(iCount) = ar1 & "," & ar2 & "," & ar3 & "," & GetSign(i) & "," & ar4 & "," & GetSign(j) & "," & GetSign(k)
                iCount = iCount + 1
                ReDim Preserve strs(iCount)
                strs(iCount) = ar1 & "," & ar2 & "," & ar3 & "," & ar4 & "," & GetSign(i) & "," & GetSign(j) & "," & GetSign(k)
                iCount = iCount + 1
            Next k
        Next j
    Next i
    For i = 0 To UBound(strs)
        If CalcStringEx(strs(i)) = "24" Then
            outStr = outStr & strs(i) & vbCrLf
        End If
    Next i
    EnumeBy = outStr
End Function

Function GetSign(ByVal lngN As Long) As String
Select Case lngN
    Case 1
        GetSign = "+"
    Case 2
        GetSign = "-"
    Case 3
        GetSign = "*"
    Case 4
        GetSign = "/"
End Select
End Function

使用的时候,调用EnumeBy函数,传入四个字符串型的数字内容,比如

EnumeBy("4","2","9","3")

返回值是所有可能的组合的后缀表达式
posted on 2013-04-30 15:06  天下一家·MJ  阅读(574)  评论(0编辑  收藏  举报
友情链接Tkin的技术博客