📄 顺序查找.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "例[8-15] 在数组中顺序查找元素"
ClientHeight = 1455
ClientLeft = 60
ClientTop = 345
ClientWidth = 3705
LinkTopic = "Form1"
ScaleHeight = 1455
ScaleWidth = 3705
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
Height = 375
Left = 1185
TabIndex = 1
Top = 225
Width = 2295
End
Begin VB.Label Label4
BorderStyle = 1 'Fixed Single
Height = 375
Left = 2145
TabIndex = 4
Top = 840
Width = 1335
End
Begin VB.Label Label3
BorderStyle = 1 'Fixed Single
Height = 375
Left = 1200
TabIndex = 3
Top = 840
Width = 855
End
Begin VB.Label Label2
Caption = "关键字:"
Height = 375
Left = 240
TabIndex = 2
Top = 840
Width = 855
End
Begin VB.Label Label1
Caption = "一维数组:"
Height = 375
Left = 240
TabIndex = 0
Top = 240
Width = 975
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Click()
Dim b()
Cls
Text1.Text = ""
b = Array(2, 4, 6, 8, 12, 15, 3)
For i = 0 To UBound(b)
Text1.Text = Text1.Text & b(i) & " "
Next i
k = Val(InputBox("输入要查找的关键值"))
Label3.Caption = Str(k)
Call Search(b(), k, n%) '调用子过程
If n <> -1 Then
Label4.Caption = "元素的下标为" & n
Else
Label4.Caption = "查找失败"
End If
End Sub
Public Sub Search(a(), ByVal key, index%) '子过程
Dim i%
For i = LBound(a) To UBound(a)
If key = a(i) Then '找到,元素的下标保存在index形参中,结束查找
index = i
Exit Sub
End If
Next i
index = -1 '找不到,index形参的值为-1
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -