📄 顺序查找.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "顺序查找"
ClientHeight = 3945
ClientLeft = 60
ClientTop = 450
ClientWidth = 4290
LinkTopic = "Form1"
ScaleHeight = 3945
ScaleWidth = 4290
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "查找"
Height = 375
Left = 2760
TabIndex = 8
Top = 3360
Width = 975
End
Begin VB.TextBox Text3
Height = 375
Left = 2640
Locked = -1 'True
TabIndex = 7
Top = 2640
Width = 1335
End
Begin VB.TextBox Text2
Height = 375
Left = 2640
Locked = -1 'True
TabIndex = 5
Top = 1440
Width = 1335
End
Begin VB.TextBox Text1
Height = 375
Left = 2640
TabIndex = 3
Top = 480
Width = 1335
End
Begin VB.ListBox List1
Height = 3300
ItemData = "顺序查找.frx":0000
Left = 120
List = "顺序查找.frx":0002
TabIndex = 0
Top = 480
Width = 2175
End
Begin VB.Label Label4
Caption = "查找的次数"
Height = 375
Left = 2640
TabIndex = 6
Top = 2040
Width = 1335
End
Begin VB.Label Label3
Caption = "查找的结果"
Height = 255
Left = 2640
TabIndex = 4
Top = 960
Width = 1095
End
Begin VB.Label Label2
Caption = "输入查找键"
Height = 255
Left = 2640
TabIndex = 2
Top = 120
Width = 1215
End
Begin VB.Label Label1
Caption = "数组d中的数据"
Height = 255
Left = 240
TabIndex = 1
Top = 120
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim d(1 To 128) As Integer
Dim nc As Integer
Function adj(a As String, n As Integer) As String
Dim sa As String
sa = a: na = Len(a)
For i = 1 To n - na
sa = " " + sa
Next i
adj = sa
End Function
Function search(k As Integer) As Integer '顺序查找算法
Dim pos As Integer
For i = 1 To 128
nc = nc + 1
If d(i) = k Then pos = i: Exit For '依次查找,找到第一个相同的值,查找就结束
Next i
search = pos '返回结果,0表示未找到
End Function
Private Sub Command1_Click()
Dim k, pos As Integer
pos = search(Val(Text1.Text))
If pos = 0 Then Text2.Text = "找不到" Else Text2.Text = "在d(" + Str(pos) + ")中"
Text3.Text = nc
nc = 0
End Sub
Private Sub Form_Load()
For i = 1 To 128
d(i) = Fix((Timer() Mod 67 + 13) * Rnd * 10) '使用timer()使每次程序运行时产生的随机数不一样
List1.AddItem adj(Str(i), 3) + ":" + adj(Str(d(i)), 6)
Next i
End Sub
Private Sub Text1_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -