vb学习递归的典型例子

这篇博客通过四个具体的例子深入讲解VB中的递归应用,包括计算阶乘展示递归基础,使用递归实现快速排序算法,递归删除空目录的方法,以及递归搜索文件的功能。这些实例帮助读者理解递归在VB编程中的实际运用。

1。阶乘

 

‘写个简单的例子,所谓阶乘的递归 

Private Sub Command1_Click() 
    Debug.Print dg(
7
End Sub
 

Function dg(n) 
    
If n = 0 Then 
        dg 
= 1 
    
Else 
        dg 
= dg(n - 1* n 
    
End If 
End Function
 

 

2。快速排序法

 



Private Sub Command1_Click() 
Dim a(10000As Long 
Dim ii As Long 
For ii = 0 To 10000 
a(ii) 
= Fix(10000 * Rnd + 10
Next 

Call QkSort(a, 0UBound(a)) 


For ii = 0 To UBound(a) 
Debug.Print a(ii) 
Next 

End Sub
 
'升序快速排序法 
Sub QkSort(Ay() As Long, Io As Long, Jo As Long

Dim I As Long, J As Long, X As Long, tp As Long 
Dim bQ As Boolean 'i到j跳跃开关 
  '初始化 
  I = Io 
  J 
= Jo 
  X 
= Ay(I) 
   
  
'一轮排序 
Do While I  < J 
    
If Not bQ Then 
        
If Ay(J)  < X Then 
        tp 
= Ay(J): Ay(J) = Ay(I): Ay(I) = tp 
        bQ 
= True 
        
Else 
        J 
= J - 1 
        
End If 
    
Else 
        
If Ay(I) >  X Then 
        tp 
= Ay(J): Ay(J) = Ay(I): Ay(I) = tp 
        bQ 
= False 
        
Else 
        I 
= I + 1 
        
End If 
    
End If 
 
Loop 
 
'递归 
  If I  < Jo Then QkSort Ay, J + 1, Jo '注意靠后的要加1 
  If Io  < J Then QkSort Ay, Io, I 
End Sub

 

3。删除空目录

 


Sub DeleteEmptyFolders(ByVal recentlyPath As String)
'''''''''''''''''''''''''''''''''''''''''''''''
'
删除指定目录下的所有目录.
'
recentlyPath参数:指定目录
'
''''''''''''''''''''''''''''''''''''''''''''''
Dim fsoFolder As Folder '一个由recentlyPath指定的目录对象
Dim fsoI As Folder
Dim Fso As FileSystemObject '文件系统对象
Set Fso = New FileSystemObject

'交出控制权,及除错
DoEvents
On Error Resume Next

'如果目录不存在的话,退出子过程
If Not Fso.FolderExists(recentlyPath) Then Exit Sub
Set fsoFolder = Fso.GetFolder(recentlyPath) '创建一个folder

'对所有的子目录递归
If fsoFolder.SubFolders.Count > 0 Then
   
For Each fsoI In fsoFolder.SubFolders
     DeleteEmptyFolders fsoI.Path 
'递归
   Next

'删除空目录
ElseIf fsoFolder.Files.Count = 0 Then
    fsoFolder.Delete
End If
end sub


 
 

 

4.搜索文件

 


Private Sub Command1_Click()
Dim aay() As String
Call SeachFile(aay, "e:vbAPI""*.txt,*.gif")
Dim i As Integer
For i = 1 To UBound(aay)
Debug.Print aay(i)

Next

End Sub

'==================================================
'
搜索文件子过程
Sub SeachFile(Ay() As StringByVal Path As String, _
              
Optional ByVal strSc As String = "")
'Ay():返回所有符合搜索条件的文件,Ay(0)不是,从Ay(1)开始
'
path:搜索路径
'
strSc:搜索条件,用逗号分开,比如 "*.txt,win*.*"
'
====================================================
Static scType() As String '搜索条件字串
Dim i As Long

DoEvents
On Error Resume Next

'初始化
If strSc <> "" Then
  scType 
= Split(Trim(strSc), ",")
  
ReDim Preserve Ay(0)
End If
If Right(Path, 1<> "" Then Path = Path & ""


'对当前的目录进行文件搜索
For i = 0 To UBound(scType)
   Ay(
0= Dir(Path & scType(i), vbNormal Or vbReadOnly Or vbHidden)
   
Do While Ay(0<> ""
         
ReDim Preserve Ay(1 + UBound(Ay))
         Ay(
UBound(Ay)) = Path & Ay(0)
         Ay(
0= Dir
   
Loop
Next

'列举出所有子目录
Dim subPath() As String '存放子目录数组
ReDim subPath(0)
subPath(
0= Dir(Path, vbDirectory)
 
Do While subPath(0<> ""
      
    
'去掉当前目录,和上级目录
    If subPath(0<> "." And subPath(0<> ".." Then
       
       
'判断是否是目录
       If (GetAttr(Path & subPath(0)) And vbDirectory) = vbDirectory Then
        
ReDim Preserve subPath(1 + UBound(subPath))
        subPath(
UBound(subPath)) = Path & subPath(0)
       
End If
    
    
End If
    subPath(
0= Dir
Loop

'如果有子目录的话,进行递归
If UBound(subPath) > 0 Then
  
For i = 1 To UBound(subPath)
     
Call SeachFile(Ay, subPath(i))
  
Next
End If

End Sub


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值