我要实现的程序很简单,0-9的循环
因为首位可能是0,所以比较麻烦,而且不确定位数大概是这样的
0
1
2
3
4
5
6
7
8
9
00
01
02
03
04
05
06
07
08
09
10
11
....
00000000000000000000
....
00000000010000000000
这样的无限穷举
请问有什么好方法吗?
当末尾为9的时候自动进位
当第2位位9的时候,自动进位,进位的第一位都是0
以次类推
因为首位可能是0,所以比较麻烦,而且不确定位数大概是这样的
0
1
2
3
4
5
6
7
8
9
00
01
02
03
04
05
06
07
08
09
10
11
....
00000000000000000000
....
00000000010000000000
这样的无限穷举
请问有什么好方法吗?
当末尾为9的时候自动进位
当第2位位9的时候,自动进位,进位的第一位都是0
以次类推
好简单,楼主没有学过编程?莫MM不做 俺也不敢抢分了 呵呵
菜鸟就不用回了,谢谢,除非你能做出来,我想你也拿不到这分
PS:有能力的回,不要浪费我时间,谢谢
class1Public Function GetNexData(str_num As String, n_pos) As String
Dim str_chr As String
Dim n_chr As Integer
If (n_pos > Len(str_num)) Then
GetNexData = Trim("0" + str_num)
Exit Function
End If
str_chr = Mid(str_num, Len(str_num) - n_pos + 1, 1) '判断是进位继续加
n_chr = Val(str_chr)
If (n_chr < 9) Then
str_chr = Trim(str(n_chr + 1))
GetNexData = Trim(Left(str_num, Len(str_num) - n_pos) + str_chr + Right(str_num, n_pos - 1))
Exit Function
Else
str_num = Trim(Left(str_num, Len(str_num) - n_pos) + "0" + Right(str_num, n_pos - 1))
GetNexData = GetNexData(str_num, n_pos + 1)
End If
End Function
窗体里的代码Private Sub Command1_Click()
Dim str As String
Dim s As Class1
Set s = New Class1
str = "0"
For i = 1 To 500
DoEvents
str = s.GetNexData(str, 1)
List1.AddItem str
Next i
End Sub我加了个list框用与你看的时候方便
Public Function GetNexData(str_num As String, n_pos) As String
Dim str_chr As String
Dim n_chr As Integer
If (n_pos > Len(str_num)) Then
GetNexData = Trim("0" + str_num) '这里的退出是99,9,999的情况,前加0
Exit Function
End If
str_chr = Mid(str_num, Len(str_num) - n_pos + 1, 1) '判断是进位继续加
n_chr = Val(str_chr) '当前位置的数字表示
If (n_chr < 9) Then '不需要进位的情况
str_chr = Trim(str(n_chr + 1)) '当前位置数子+1
GetNexData = Trim(Left(str_num, Len(str_num) - n_pos) + str_chr + Right(str_num, n_pos - 1)) '大众的退出
Exit Function
Else '如果需要进位npos+1,当前位置为0
str_num = Trim(Left(str_num, Len(str_num) - n_pos) + "0" + Right(str_num, n_pos - 1)) '需要进位的情况
GetNexData = GetNexData(str_num, n_pos + 1) '递归调用判断前一位
End If
End Function注释加上
如果最高位是9,那么前位补0,大家可以按这个思路去写下代码,因该比较简洁执行效率还没测试,可以结帖子了,发布代码的2位同志我给分了:)其他人也要感谢了,除了前3位
Option ExplicitPrivate Sub Command1_Click()
Dim i As Long
For i = 1 To 4
ListAll vbNullString, i
Next i
End SubFunction ListAll(ByVal Prefix As String, ByVal iLen As Long)
Dim i As Long
If iLen > 0 Then
For i = 0 To 9
ListAll = Prefix & CStr(i)
ListAll ListAll, iLen - 1
Next i
Else
List1.AddItem Prefix
End If
End Function
熬了一宿,呼呼,我这么辛苦,楼主再开贴给我点分好不好 :)