VB
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

实验六 PowerPoint PPT Presentation


  • 51 Views
  • Uploaded on
  • Presentation posted in: General

VB 实验. 实验六. 数组程序设计Ⅱ. 返回.   掌握数组应用的常用算法——数据排序(选择法、冒泡法),数据查找(顺序法、折半法),有序数列的插入和删除。  掌握自定义类型数据(变量和数组)的使用。. 实验目的. 实验要求.  要求完成实验任务  利用循环语句和数组结合完成各种操作 实验任务 1.练习并掌握数组的排序算法 2.练习并掌握数据的查找算法 3.练习并掌握有序数组(数列)的插入与删除算法. 实验步骤 一、练习数组的排序算法 1. 实验目的    掌握数组的排序算法。

Download Presentation

实验六

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6352627

VB实验

实验六

数组程序设计Ⅱ

返回


6352627

 掌握数组应用的常用算法——数据排序(选择法、冒泡法),数据查找(顺序法、折半法),有序数列的插入和删除。

掌握自定义类型数据(变量和数组)的使用。

实验目的


6352627

实验要求

 要求完成实验任务

 利用循环语句和数组结合完成各种操作

实验任务

1.练习并掌握数组的排序算法

2.练习并掌握数据的查找算法

3.练习并掌握有序数组(数列)的插入与删除算法


6352627

实验步骤

一、练习数组的排序算法

1. 实验目的

   掌握数组的排序算法。

2. 实验内容

【实验6-1】从键盘上输入任意一串字符,分别用冒泡法、选择法和比较交换法,对输入的字符串按ASCII码从小到大排序,并输出排序后的结果。(工程文件保存为“VB实验6-1.vbp”,窗体文件保存为“VB实验6-1.frm”)


6352627

3. 创建应用程序界面(如下图)


6352627

4. 控件属性的设置


6352627

效果如下图:


6352627

For i = 1 To n

S(i) = Mid(Si, i, 1)

Next i

For i = 1 To n - 1

For j = 1 To n - i

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

t = S(j)

S(j) = S(j + 1)

S(j + 1) = t

End If

Next j

Next i

(5)代码参考(VB实验6-1)

Option Base 1

Dim Si As String, S(40) As String * 1, i%, j%, n%, t$

Private Sub Command1_Click()

Si = InputBox("请输入英文字符串(20字符以内):")

Print "排序前字符串为:"

Print Si

n = Len(Si)


6352627

n = Len(Si)

For i = 1 To n

S(i) = Mid(Si, i, 1)

Next i

For i = 1 To n - 1

p = i

For j = i + 1 To n

If S(p) > S(j) Then p = j

Next j

t = S(p):S(p) = S(i):S(i) = t

Next i

Print "排序后字符串为:"

For i = 1 To n

Print S(i);

Next i

Print

End Sub

Private Sub Command2_Click()

Si = InputBox("请输入英文字符串(20字符以内):")

Print "排序前字符串为:"

Print Si


6352627

Print "排序后字符串为:"

For i = 1 To n

Print S(i);

Next i

Print

End Sub

Private Sub Command3_Click()

Si = InputBox("请输入英文字符串(20字符以内):")

Print "排序前字符串为:"

Print Si

n = Len(Si)

For i = 1 To n

S(i) = Mid(Si, i, 1)

Next i

For i = 1 To n - 1

For j = i + 1 To n

If S(i) > S(j) Then

t = S(i)

S(i) = S(j)

S(j) = t

End If

Next j

Next i


6352627

Print "排序后字符串为:"

For i = 1 To n

Print S(i);

Next i

Print

End Sub

Private Sub Command4_Click()

End

End Sub

Private Sub Form_Load()

AutoRedraw = True

Print "字符串排序实验:"

Print“单击不同的命令按钮,完成不同的排序"

Print

End Sub

运行效果如下:


6352627

运行效果如下:


6352627

二、练习数据的查找算法

1. 实验目的

掌握数据的查找算法

2. 实验内容

【实验6-2】从键盘上输入任意一串字符S,然后输入一个字符(设字符变量名为c),用顺序查找的方法查找输入的字符c是否出现在S中,若出现则输出其首次出现的位置(下标),没出现则输出查找失败的信息。

(工程文件保存为“VB实验6-2.vbp”,窗体文件保存为“VB实验6-2.frm”)


6352627

3. 创建应用程序界面(如下图)


6352627

4. 控件属性的设置

效果如下图:


6352627

5.代码参考(VB实验6-2(查找))

Option Base 1

Dim Si As String, c As String * 1, S(40) As String * 1, i%, n%, find%

Private Sub Command1_Click()

Si = InputBox(“请输入字符串(40字符以内):”)

c = InputBox("请输入一个字符:")

n = Len(Si)

find = 0

For i = 1 To n

S(i) = Mid(Si, i, 1)

Next i


6352627

Print "查找失败:"

Print " 在串"; Si

Print "中没有找到字符"; c

End If

Print

End Sub

For i = 1 To n

If S(i) = c Then

find = 1

Exit For

End If

Next i

If find = 1 Then

Print "查找成功:"

Print " 字符"; c; "在串"; Si

Print "中首次出现的位置为"; i

Else


6352627

Private Sub Command2_Click()

Si = InputBox(“请输入字符串(40字符以内):")

k = InputBox("请输入一个字符:")

n = Len(Si)

find = 0

For i = 1 To n

S(i) = Mid(Si, i, 1)

Next i

low = 1

hig = n

find = -1


6352627

While low <= hig And find = -1

m = (low + hig) \ 2

If k = S(m) Then

find = 1

Print k; "has be found,on"; m; "the position."

Else

If k < S(m) Then

hig = m - 1

Else

low = m + 1

End If

End If

Wend


6352627

If find = -1 Then

Print k; "has not be found."

End If

End Sub

Private Sub Command3_Click()

End

End Sub

Private Sub Form_Load()

AutoRedraw = True

Print "字符串查找实验:"

Print "单击窗体开始..."

Print

End Sub


6352627

6.运行结果如图所示


6352627

三、练习有序数组(数列)的插入与删除算法

1. 实验目的

掌握有序数组(数列)的插入与删除算法

2. 实验内容

【实验6-3】设有一个已经按ASCII码升序排列的有序字符数组S,且S中没有重复字符(设数组中字符构成的串为"123456XYZabcdhijk“)。

要求输入一个字符(设字符变量名为c),用折半查找的方法查找c是否出现在S中,若出现则先输出其出现的位置(下标),并从串S中删除字符c;若不出现则将字符c插入到串S中。


6352627

要求删除或插入字符c后,S仍保持为有序字符数组。(工程文件保存为“VB实验6-3.vbp”,窗体文件保存为“VB实验6-3.frm”)

3.功能要求

把结果输出到窗体上。

4.属性设置


6352627

5.代码参考(VB实验6_3)

Option Base 1

Private Sub Form_Click()

Si$ = "123456XYZabcdhijk"

Dim c As String * 1, S(80) As String * 1, i%, j%, n%, find%, low%, high%

c = InputBox("请输入一个字符:")

n = Len(Si)

For i = 1 To n

S(i) = Mid(Si, i, 1)

Next i


6352627

'Print S(n + 1); "A" '折半法查找

low = 1: high = n: find = 0

Do While low <= high And find = 0

i = (low + high) \ 2

If c = S(i) Then

find = 1

Exit Do

Else

If c < S(i) Then

high = i - 1

Else

low = i + 1

End If

End If

Loop


6352627

If find = 1 Then

Print "查找成功:"

Print "字符"; c; "在串"; Si;

Print "中首次出现的位置为"; i; ","

'删除

For j = i + 1 To n

S(j - 1) = S(j)

Next j

S(n) = ""

n = n - 1 '删除字符c后串长减1


6352627

Print "删除字符"; c; "后,字符串为:"

For i = 1 To n

Print S(i);

Next i

Print

Else

Print "查找失败:"

Print "在串"; Si; "中没有找到字符"; c; ","

'插入

i = 1

Do While c > S(i) And i <= n

i = i + 1

Loop


6352627

For j = n To i Step -1

S(j + 1) = S(j)

Next j

S(i) = c

n = n + 1

Print "在原串中插入"; c; "后,字符串变为:"

For i = 1 To n

Print S(i);

Next i

Print

End If

Print

End Sub


6352627

Private Sub Form_Load()

AutoRedraw = True

Print "折半法查找及元素的插入与删除查找实验:"

Print "单击窗体开始..."

Print

End Sub

6.运行结果如图所示

四、完成课后作业


  • Login