1

基本上,我有这个代码:

ReDim aPos(1 To Len(sInput))

Do
    aPos(1) = aPos(1) + 1
    For X = 1 To Len(sInput) - 1 
        If aPos(X) > Len(sInput) Then 
            aPos(X) = 1 
            aPos(X + 1) = aPos(X + 1) + 1
        End If
    Next X
    If aPos(Len(sInput)) > Len(sInput) Then Exit Do

我唯一不明白的是最后一行的检查是什么。在我看来,它正在检查从临时创建的“位置”数组中找到的索引是否比来自输入数组的索引更多。那正确吗?我没有关注的是这里的数学,迭代的性质,如果你愿意的话。

4

1 回答 1

1

下面是一个小型测试项目,用于显示您的 apos 进度,我从您的代码中尽可能少地更改

你可以看到最后 apos 将用 '1' 填充,除了最后一个元素的字符串长度为 sInput 加 1

'1 form with
'    1 command button : name=Command1
Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub Command1_Click()
  Dim X As Integer
  Dim sInput As String
  Dim apos() As Integer
  Dim lngTime As Long
  sInput = "Hello"
  ReDim apos(1 To Len(sInput)) As Integer
  Do
    apos(1) = apos(1) + 1
    For X = 1 To Len(sInput) - 1
      If apos(X) > Len(sInput) Then
        apos(X) = 1
        apos(X + 1) = apos(X + 1) + 1
        ShowApos apos
        lngTime = GetTickCount + 100
        Do Until GetTickCount > lngTime
          DoEvents
        Loop
      End If
    Next X
    If apos(Len(sInput)) > Len(sInput) Then Exit Do
  Loop
End Sub

Private Sub ShowApos(apos() As Integer)
  Dim intIndex As Integer
  Dim strShow As String
  For intIndex = LBound(apos) To UBound(apos)
    strShow = strShow & CStr(apos(intIndex)) & " "
  Next intIndex
  Caption = strShow
End Sub

所以它所做的只是有一些计数器填充到 len(sInput)+1 并将数组的所有其他元素设置为 1

最后一行将确保您在达到最终值时退出循环

于 2012-12-19T08:15:32.280 回答