【算法】VB 24点计算

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

下面代码基于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")

返回值是所有可能的组合的后缀表达式

转载于:https://www.cnblogs.com/sunsoftresearch/archive/2013/04/30/3051925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值