置换码的输出(逐块) - VB6编码

时间:2012-12-18 18:44:17

标签: vb6 permutation

基本上,我有这段代码:

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

我唯一不明白的是检查结尾,最后一行。在我看来,它正在检查是否存在从“位置”数组中找到的更多索引,这些索引是临时创建的,而不是来自输入数组的索引。是对的吗?我不遵循的是这里的数学,迭代性质,如果你愿意的话。

1 个答案:

答案 0 :(得分:1)

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

你可以看到最后将填充'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

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