如何从数组中读取两个插槽并使用递归函数将它们合并?

时间:2019-04-18 16:39:53

标签: arrays excel vba

我正在尝试使用VBA实现计算器。我在使用递归计算结果时遇到了麻烦。

我尝试实现该功能。我总是最终使结果为零。

想法:

例如,要计算2 + 3 + 4 + 5

该函数将递归读取并在每个步骤中合并两个元素。例如,在第2步中,从第一个位置开始,数组的前两个插槽为您提供“ 2”和“ +”,因此您知道需要从位置3开始向数组的其余部分加2。最后,数组的结果将是5(来自步骤5)+ 4(来自步骤4)+ 3(来自步骤3)+ 2(来自步骤2)= 14。

请在下面找到代码。我已经尝试实现它,但出现类型不匹配错误。 “显示”是一个字符串,用于记住计算器的当前显示。

Dim Memory(1 To 100) As String

' The current position used by the calculator inside the memory array
Dim CurrentPos As Integer

' This function is a recursive function for calculating the result of the expression
' stored in the Memory array, starting at the position Pos.
'
' Note that the return value is a Long number. When you calculate the result you need
' to convert the numbers (as text) in the memory to long numbers using CLng().
Function CalcTotal(ByVal Pos As Integer) As Long
    '
    ' Task 4: Calculating the Result Using Recursion
    '

    ' Case 1: if Pos is bigger than what you have in the Memory array

        ' Nothing is available

    ' Case 2: if Pos is exactly at the end of the Memory array

        ' Return the number in the position

    ' Case 3: Memory(Pos) is a number and Memory(Pos + 1) is an operator

        ' Return the number in the current position together with the rest of the Memory array
    If Pos > CurrentPos Then ' Case 1: Nothing left to read
        Display = "0"
        'return 0 as the result because there is nothing to do...
    ElseIf Pos = CurrentPos Then ' Case 2: There is only a number left
        Display = CLng(Memory(Pos))
        'return the number in the current position...
    Else ' Case 3: Read the next two slots of the array and combine with the rest of the array
        Display = CLng(Memory(Pos)) + CLng(Memory(Pos + 1))
        CalcTotal (Pos + 2)
    End If
End Function

2 个答案:

答案 0 :(得分:1)

这可以帮助您入门:

Public MyInputs As Variant

Sub Test()
    MyInputs = Array("2", "+", "3", "+", "4", "+", "5")
    Debug.Print Application.Evaluate(CalcTotal(UBound(MyInputs)))  '~~> prints 14
End Sub

Function CalcTotal(n As Integer) As String
    If n = 0 Then
        CalcTotal = MyInputs(n)
    Else
        CalcTotal = MyInputs(n) & (CalcTotal(n - 1))
    End If
End Function

注意:

  • CalcTotal将返回一个字符串,例如5+4+3+2
  • Application.Evaluate将该字符串解析为计算结果,并输出14

答案 1 :(得分:0)

代码中的问题是,您正在返回一个字符串“ 0”,对于返回LONG的函数,更正是将“ 0”(字符串)替换为0(长整数)。

' This function is a recursive function for calculating the result of the expression
' stored in the Memory array, starting at the position Pos.
'
' Note that the return value is a Long number. When you calculate the result you need
' to convert the numbers (as text) in the memory to long numbers using CLng().
Function CalcTotal(ByVal Pos As Integer) As Long
    '
    ' Task 4: Calculating the Result Using Recursion
    '

    ' Case 1: if Pos is bigger than what you have in the Memory array

        ' Nothing is available

    ' Case 2: if Pos is exactly at the end of the Memory array

        ' Return the number in the position

    ' Case 3: Memory(Pos) is a number and Memory(Pos + 1) is an operator

        ' Return the number in the current position together with the rest of the Memory array
    If Pos > CurrentPos Then ' Case 1: Nothing left to read
        ' Display = "0" WRONG THATS A STRING
          Display = 0
        'return 0 as the result because there is nothing to do...
    ElseIf Pos = CurrentPos Then ' Case 2: There is only a number left
        Display = CLng(Memory(Pos))
        'return the number in the current position...
    Else ' Case 3: Read the next two slots of the array and combine with the rest of the array
        Display = CLng(Memory(Pos)) + CLng(Memory(Pos + 1))
        CalcTotal (Pos + 2)
    End If
End Function