📄 frmbinary.frm
字号:
VERSION 5.00
Begin VB.Form frmBinary
Caption = "Fig. 7.9: Binary Search"
ClientHeight = 3600
ClientLeft = 2715
ClientTop = 1410
ClientWidth = 6300
BeginProperty Font
Name = "Courier"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H80000008&
LinkTopic = "Form1"
PaletteMode = 1 'UseZOrder
ScaleHeight = 3600
ScaleWidth = 6300
Begin VB.CommandButton cmdSearch
BackColor = &H80000005&
Caption = "&Search"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 2483
TabIndex = 2
Top = 2760
Width = 1335
End
Begin VB.TextBox txtKey
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 420
Left = 4305
TabIndex = 1
Top = 120
Width = 975
End
Begin VB.Label lblKey
Caption = "Enter integer search key:"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 375
Left = 1020
TabIndex = 0
Top = 120
Width = 3135
End
End
Attribute VB_Name = "frmBinary"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Fig. 7.9
' Demonstrating a binary search
Option Explicit
Option Base 1
Dim mArray(15) As Integer
Dim mLowBound As Integer
Dim mUpperBound As Integer
Private Sub Form_Load()
Dim x As Integer
mLowBound = LBound(mArray)
mUpperBound = UBound(mArray)
' Generate some array data
For x = mLowBound To mUpperBound
mArray(x) = 2 * x
Next x
End Sub
Private Sub cmdSearch_Click()
Dim x As Integer
Call Cls
' Print blanks so printing does not
' print behind Label and TextBox
For x = 1 To 5
Print
Next x
Call BinarySearch
End Sub
Private Sub BinarySearch()
Dim middle As Integer
Dim low As Integer, high As Integer
Dim searchKey As Integer
low = mLowBound
high = mUpperBound
Call PrintHeader
searchKey = txtKey.Text
Do While (low <= high)
middle = (low + high) \ 2
Call PrintRow(low, middle, high)
If (searchKey = mArray(middle)) Then
Print "Found " & searchKey & " in " _
& "index " & middle
Exit Sub
ElseIf searchKey < mArray(middle) Then
high = middle - 1
Else
low = middle + 1
End If
Loop
Print searchKey & " not found."
End Sub
Private Sub PrintHeader()
Dim x As Integer
Print "Indexes:"
For x = mLowBound To mUpperBound
Print Format$(x, "!@@@@");
Next x
Print
For x = mLowBound To 4 * mUpperBound
Print "-";
Next x
Print
End Sub
Private Sub PrintRow(low As Integer, middle As Integer, _
high As Integer)
Dim x As Integer
For x = mLowBound To mUpperBound
If (x < low Or x > high) Then
Print Space$(4);
ElseIf (x = middle) Then
Print Format$(mArray(x) & "*", "!@@@@");
Else
Print Format$(mArray(x), "!@@@@");
End If
Next x
Print
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -