vb编写的对冒泡排序进行演示的程序,动态演示

vb编写的对冒泡排序进行演示的程序,动态演示,第1张

Dim a(8) As Integer

Dim cou As Integer

Dim temp As Integer

Sub shownum()

Command1.Caption = a(1)

Command2.Caption = a(2)

Command3.Caption = a(3)

Command4.Caption = a(4)

Command5.Caption = a(5)

Command6.Caption = a(6)

Command7.Caption = a(7)

Command8.Caption = a(8)

End Sub

Private Sub Command10_Click()

cou = 0

For i = 1 To 8

For j = 1 To 8 - i

cou = cou + 1

If a(j) >a(j + 1) Then

temp = a(j + 1)

a(j + 1) = a(j)

a(j) = temp

End If

Call shownum

MsgBox ("完成第" &cou &"次排序")

Next j

Next i

MsgBox ("完成所有排序,共进行" &cou &"次排序")

End Sub

Private Sub Command9_Click()

Randomize

For i = 1 To 8

a(i) = Int(Rnd * 100) + 10

Next i

Call shownum

End Sub

Private Sub Form_Load()

cou = 0

End Sub

在VB中演示过程冒泡法排序

要求把代码输入后能直接在VB环境中运行

在窗体中添加三个标签,一个按钮,一个框架在框架中添加一个文本框

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)

If KeyCode <48 Or KeyCode >57 And KeyCode <96 Or KeyCode >150 And KeyCode <>109 And _

KeyCode <>189 Then Text1 = ""

End Sub

Private Sub Form_Load()

Label1(0).Visible = False

Label1(0).Alignment = 2

Label1(0).Top = 100

Label1(0).Left = 100

Label1(0).Height = 300

Label1(0).Width = 800

Label1(0).BackColor = QBColor(15)

Label1(0).FontSize = 12

Caption = "演示“起泡法”从小到大的排序过程"

Top = 0

Left = 200

Width = 2 * Label1(0).Left + 12 * (Label1(0).Width + 100)

Height = 7000

Label2.Top = 5700

Label2.Left = 500

Label2.FontSize = 12

Label2.Width = Width - 2 * Label2.Left

Label2.WordWrap = True

Label2.AutoSize = True

a = Space(4) &"数从文本输入,每输完一个数后,按回车键,数的个数可变.排序开始之前所有"

a = a &"所有的数都是白底黑字).单击“开始”按钮,开始比较(红底)数与(黄底)数"

a = a &"若(红底)数大就交换两数的位置否则,两数仍保持原位置.位置已确定不变的数为"

Label2 = a &"(底黑字)."

Label3 = ""

Label3.FontSize = 15

Label3.Top = Label2.Top - 500

Label3.Left = 1300

Label3.AutoSize = True

Frame1.Top = Height - 2300

Frame1.Left = Width / 3

Frame1.Width = 2300

Frame1.Height = 850

Frame1.Caption = "输入-32768到32767的数"

Text1.Top = 300

Text1.Left = 500

Text1.Height = 370

Text1.Width = 1450

Text1.FontSize = 14

Text1.MaxLength = 6

Text1 = ""

Command1.Top = Frame1.Top + 200

Command1.Left = Frame1.Left + Frame1.Width + 100

Command1.Caption = "开始排序"

Command1.Enabled = False

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

Static kx As Byte, ky As Byte

If KeyAscii = 13 Then

If Text1 <>"" Then

x = Val(Text1)

If x >= -32768 And x <= 32767 Then

If n = 0 Then

Label1(0) = x: Label1(0).Visible = True

Else

Load Label1(n): Label1(n) = x

Label1(n).Visible = True

kx = kx + 1

If kx Mod 12 = 0 Then ky = ky + 1: kx = 0

Label1(n).Left = Label1(0).Left + kx * (Label1(0).Width + 100)

Label1(n).Top = Label1(0).Top + ky * (Label1(0).Height + 100)

Command1.Enabled = True

End If

Text1 = "": n = n + 1

End If

End If

End If

End Sub

定义动、静态变量

静态每次+1

循环8次

随机产生10-99的数

将随机数赋值给数组

结束循环

用冒泡函数(自定义的)从小到大排序(或直接写两个排序的循环)

msgbox提示显示排序后的数组,并将静态变量写在该函数中


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7787065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存