📄 form1.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents lblCh As System.Windows.Forms.Label
Friend WithEvents txtCh As System.Windows.Forms.TextBox
Friend WithEvents lblR As System.Windows.Forms.Label
Friend WithEvents btnSeek As System.Windows.Forms.Button
Friend WithEvents lblKeyIn As System.Windows.Forms.Label
Friend WithEvents txtKeyIn As System.Windows.Forms.TextBox
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lblKeyIn = New System.Windows.Forms.Label
Me.btnSeek = New System.Windows.Forms.Button
Me.txtCh = New System.Windows.Forms.TextBox
Me.lblR = New System.Windows.Forms.Label
Me.lblCh = New System.Windows.Forms.Label
Me.txtKeyIn = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'lblKeyIn
'
Me.lblKeyIn.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.lblKeyIn.Location = New System.Drawing.Point(19, 7)
Me.lblKeyIn.Name = "lblKeyIn"
Me.lblKeyIn.Size = New System.Drawing.Size(173, 23)
Me.lblKeyIn.TabIndex = 1
Me.lblKeyIn.Text = "请输入一个字串:"
'
'btnSeek
'
Me.btnSeek.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.btnSeek.Location = New System.Drawing.Point(134, 112)
Me.btnSeek.Name = "btnSeek"
Me.btnSeek.Size = New System.Drawing.Size(68, 22)
Me.btnSeek.TabIndex = 3
Me.btnSeek.Text = "搜索"
'
'txtCh
'
Me.txtCh.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.txtCh.Location = New System.Drawing.Point(250, 75)
Me.txtCh.Name = "txtCh"
Me.txtCh.Size = New System.Drawing.Size(48, 26)
Me.txtCh.TabIndex = 2
Me.txtCh.Text = ""
'
'lblR
'
Me.lblR.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.lblR.Location = New System.Drawing.Point(29, 149)
Me.lblR.Name = "lblR"
Me.lblR.Size = New System.Drawing.Size(288, 23)
Me.lblR.TabIndex = 0
'
'lblCh
'
Me.lblCh.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.lblCh.Location = New System.Drawing.Point(19, 82)
Me.lblCh.Name = "lblCh"
Me.lblCh.Size = New System.Drawing.Size(211, 23)
Me.lblCh.TabIndex = 1
Me.lblCh.Text = "请键入要寻找的字符:"
'
'txtKeyIn
'
Me.txtKeyIn.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.txtKeyIn.Location = New System.Drawing.Point(19, 37)
Me.txtKeyIn.MaxLength = 20
Me.txtKeyIn.Name = "txtKeyIn"
Me.txtKeyIn.Size = New System.Drawing.Size(288, 26)
Me.txtKeyIn.TabIndex = 0
Me.txtKeyIn.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(326, 183)
Me.Controls.Add(Me.txtKeyIn)
Me.Controls.Add(Me.lblKeyIn)
Me.Controls.Add(Me.btnSeek)
Me.Controls.Add(Me.lblR)
Me.Controls.Add(Me.txtCh)
Me.Controls.Add(Me.lblCh)
Me.Name = "Form1"
Me.Text = "f10_3_3 二分搜索法"
Me.ResumeLayout(False)
End Sub
#End Region
'************** f10_3_3 **************
Dim arr(19), ts As String
Dim sit(19), tn As Integer
Dim i, j, k, high, low As Integer
Private Sub btnSeek_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSeek.Click
high = 19 : low = 0
For i = low To high
arr(i) = Mid(txtKeyIn.Text, i + 1, 1)
sit(i) = i
Next i
'排序
For j = high - 1 To low Step -1
For k = low To j
If arr(k) > arr(k + 1) Then
ts = arr(k) : arr(k) = arr(k + 1) : arr(k + 1) = ts
tn = sit(k) : sit(k) = sit(k + 1) : sit(k + 1) = tn
End If
Next k
Next j
'二分法搜索
Dim middle, num As Integer
num = -1
Do
middle = (high + low) \ 2
If arr(middle) = txtCh.Text Then
num = middle
Exit Do
End If
If arr(middle) > txtCh.Text Then
high = middle - 1
Else
low = middle + 1
End If
Loop Until low > high
'显示数据
If num = -1 Then
lblR.Text = "查无数据"
Else
Do
If arr(num) = arr(num - 1) Then
num = num - 1
Else
Exit Do
End If
Loop
lblR.Text = txtCh.Text & "字符排列在第" & Str(sit(num) + 1) & "顺序的位置"
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -