1、VB 实习四 数组与自定义数据类型实习四 数组与自定义数据类型一、实习目的 1.理解数组的概念、分类,数组定义方法; 2.掌握数组的操作(赋值、运算、输出)的基本方法; 3.掌握与数组有关的算法; 4.理解自定义数据类型; 5.了解控件数组。二、实习内容 1.掌握一维数组的定义、赋值和输出方法; 2.理解静态数组与动态数组的区别和使用 3.掌握一维数组基本应用和有关算法(统计、求极值、排序、查询)等; 4.掌握二维数组程序设计方法和的基本应用; 5.完成课后编程习题。 6.阅读教材自定义数据类型和控件数组的内容,通过上机验证进一步理解。三、程序算法及代码 课本习题验证: 例题5-1、 界面设置
2、:数据来源:属性结果:代码编写:Private Sub Command1_Click() Dim sum!, aver!, i%, x!(1 To 10) Open c:000.txt For Input As #1 aver = 0 For i = 1 To 10 Input #1, x(i) Print 第 & i & 个学生的成绩为: & x(i) aver = aver + x(i) Next i Close aver = aver / 10 Print 平均分为: & aver Print =以下成绩高于平均分= For i = 1 To 10 If x(i) aver Then P
3、rint 第 & i & 个学生成绩为: & x(i) End If Next iEnd Sub例题5-2代码编写:Private Sub Command1_Click() Dim aver!, i%, n%, x!() Open c:000.txt For Input As #1 aver = 0 n = 0 Do Until EOF(1) n = n + 1 ReDim Preserve x(n) Input #1, x(n) Print 第 & i & 个学生的成绩为: & x(n) aver = aver + x(n) Loop Close aver = aver / n Print
4、平均分为: & aver Print =以下成绩高于平均分= For i = 1 To n If x(i) aver Then Print 第 & i & 个学生成绩为: & x(i) End If Next iEnd Sub属性结果:例题5-3、属性结果:代码编写: Private Sub Command1_Click() Dim a(), x% a = Array(sunday, monday, tuesday, wednesday, thursday, friday, saturday) Do x = InputBox(请输入0-6之间的一个数字) Loop While x 6 Or x
5、 = A And c 0 Then Picture1.Print ; Chr(j); =; a(j); ; Next jEnd Sub例题5-7属性结果:代码编写:Option Base 1Private Sub Command1_Click() Dim a%(10), i%, k% Randomize For i = 1 To 10 a(i) = Int(Rnd * 90) + 10 Print a(i); Next i Print k = 1 For i = 2 To 10 If a(k) a(j) Then k = j Next j t = a(i): a(i) = a(k): a(k)
6、 = t Next i Print 排序后:; For i = 1 To n Print a(i); Next iEnd Sub例题5-9 属性及结果:代码编写:Private Sub Command1_Click() Dim a(), x!, m%, n%, i%, p As Boolean a = Array(15, 8, 4, 13, 6, 10, 17, 1) m = LBound(a) n = UBound(a) For i = m To n Print a(i); c Next i Print x = Val(InputBox(请输入要查找的数!) p = False For i
7、= m To n If x = a(i) Then p = True Exit For End If Next i If p Then MsgBox 找到了,在数组中的位置为第 & i & 位 Else MsgBox 没找到 End IfEnd Sub例题5-10属性设计:结果:代码编写:Option Base 1Dim a%(5, 5), b%(5, 5), s%, i%, j%Private Sub Command1_Click() For i = 1 To 5 For j = 1 To 5 a(i, j) = Int(Rnd * 10) Picture1.Print Tab(3 * j)
8、; a(i, j); Next j Next iEnd SubPrivate Sub Command2_Click() For i = 1 To 5 For j = 1 To 5 b(i, j) = a(j, i) Picture2.Print Tab(3 * j); b(i, j); Next j Next iEnd SubPrivate Sub Command3_Click() For i = 1 To 5 s = s + a(i, i) + a(i, 5 - i + 1) Next i Label1.Caption = 对角线个元素之和为: & s End Sub例题5-11 属性及数据
9、:属性结果:代码编写:Private Type student xm As String * 5 sx As Integer yy As Integer yw As Integer pj As SinglePrivate Sub Command1_Click() Const stucount = 10 Dim stu(1 To stucount) As student, t As student Dim i As Integer, j As Integer Open c:in.txt For Input As #1 Picture1.Print Tab(2); 姓名; Tab(12); 数学;
10、 Tab(22); 英语; Tab(32); 语文; Tab(42); 平均分 For i = 1 To stucount Input #1, stu(i).sx, stu(i).yy, stu(i).yw stu(i).pj = (stu(i).sx + stu(i).yy + stu(i).yw) / 3 Picture1.Print Tab(2); stu(i).xm; Tab(12); stu(i).sx; Tab(22); stu(i).yy; Tab(32); stu(i).yw; Tab(42); stu(i).pj Next For i = 1 To stucount - 1
11、k = i For j = i + 1 To stucount If stu(i).pj stu(k).pj Then k = j Next t = stu(k): stu(k) = stu(i): stu(i) = t Next Picture1.Print =全班成绩按期平均分由高到低排序输出如下= Picture1.Print Tab(2); 姓名; Tab(12); 数学; Tab(22); 英语; Tab(32); 语文; Tab(42); 平均分 For i = 1 To stucount Picture1.Print Tab(2); stu(i).xm; Tab(12); stu
12、(i).sx; Tab(22); stu(i).yy; Tab(32); stu(i).yw; Tab(42); stu(i).pj NextEnd Sub例题5-12属性设置:属性结果:代码编写:Private Sub Form_Load() Dim i%, size% size = 375 With Command1(0) .Width = size .Height = size .Left = 0 .Top = 0 .Caption = End With Me.Height = size * 10 + 800 Me.Width = size * 10 + 400 With Picture
13、1 .Height = size * 10 + 50 .Width = size * 10 + 50 .Top = 100 .Left = 100 End With For i = 1 To 99 Load Command1(i) With Command1(i) .Left = (i Mod 10) * size .Top = (i 10) * size .Caption = True End With Next i End Sub例题5-13属性设计:运算结果:代码编写:Private Sub Command1_Click(index As Integer) Dim x!, y! x =
14、Val(Text1.Text) y = Val(Text2.Text) Select Case index Case 0 Label3 = x & + & y & = & x + y Case 1 Label3 = x & - & y & = & x - y Case 2 Label3 = x & * & y & = & x * y Case 4 If y = 0 Then Label3 = 除数为o! Else Label3 = x & / & y & = & x / y End If End SelectEnd Sub课后习题:5-1 属性设计: 运算结果:代码编写:Private Sub
15、 Command1_Click() Dim c(0 To 7) a = Array(1, 3, 5, 2, 4, 18, 50, 25) b = Array(5, 27, 30, 35, 60, 41, 87, 33) For i = 0 To 7 c(i) = a(i) + b(i) Next Print a(), b(), c() For i = 0 To 7 Print a(i), b(i), c(i) NextEnd Sub5-2属性设计: 运算结果:代码编写:Private Sub Command1_Click() 先声明一个动态数组a,因为其大小不能确定 Dim a() As In
16、teger n = Val(Text1.Text) m = Val(Text2.Text) 指定数组的大小 ReDim a(1 To n, 1 To m) As Integer Picture1.Cls 用随机数给数组赋值,并打印到窗体 For i = 1 To n For j = 1 To m a(i, j) = Int(Rnd * 901) Picture1.Print Tab(5 * (j - 1) + 2); a(i, j); Next Next 假设第一个元素的值最大,用r和c分别存放最大数所在的行和列 r = 1: c = 1 For i = 1 To n For j = 1 To
17、 m If a(r, c) a(i, j) Then r = i c = j End If Next Next Label3.Caption = 矩阵中的最大值为: & a(r, c) & vbCrLf Label3.Caption = Label3.Caption & 位置: & r & 行, & c & 列 End Sub5-3属性设计:预算结果:代码编写:Private Sub Command1_Click() Dim a(1 To 10) As Integer Picture1.Cls Picture1.Print 调换前的数组: For i = 1 To 10 a(i) = Int(
18、Rnd * 100) Picture1.Print a(i); Next Picture1.Print 数组元素是两两互换,因此需要5次既可以调换完成 For i = 1 To 5 t = a(i) a(i) = a(11 - i) a(11 - i) = t Next Picture1.Print 调换后的数组: For i = 1 To 10 Picture1.Print a(i); NextEnd Sub5-4 属性设计:运算结果:代码编写:Private Sub Command1_Click() Dim x$(), a%(4 To 9), b(), i%, n%, y, k% b =
19、Array(无效数据, 小于60分, 6069, 7079, 8089, 90100) x = Split(Text1, ,) n = UBound(x) For Each y In x If y 100 Then y = 40 ElseIf y 60 Then y = 50 ElseIf y = 100 Then y = 90 End If k = y 10 a(k) = a(k) + 1 Next y For i = 4 To 9 Picture1.Print b(i - 4), a(i) Next iEnd Sub5-5属性设计:运算结果:代码编写:Private Sub Command
20、1_Click() 用wi数组保存加权因子 wi = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2) Y数组保存对应的第18位编码 y = Array(1, 0, X, 9, 8, 7, 6, 5, 4, 3, 2) 获取用户输入的身份证的前17位 sfz = Text1.Text 若不足17位,报错并退出 If Len(sfz) 17 Then MsgBox 你输入的位数不够, vbCritical Exit Sub 退出子程序 End If 计算加权值 s = 0 For i = 0 To 16 t = Val(Mid(
21、sfz, i + 1, 1) s = s + t * wi(i) Next yt = s Mod 11 Label2.Caption = 第十八位编码是: & y(yt)End Sub5-6 属性结果:运算结果:代码编写:Private Sub Command1_Click() 声明一个数组保存数列的前30项 Dim a(1 To 30) As Long 根据题目,前两项为1 a(1) = 1 a(2) = 1 计算后28项 For i = 3 To 30 a(i) = a(i - 1) + a(i - 2) Next For i = 1 To 30 Picture1.Print Tab(i
22、- 1) Mod 5) * 8 + 2); a(i); NextEnd Sub5-7 属性设计:运算结果:代码编写:Private Sub Command1_Click() 用数组a保存产生的字母 Dim a(1 To 15) As String 因为要求产生的字母不能重复,因此循环多少次来产生不能确定,因此不能使用for循环 首先产生一个 a(1) = Chr(Int(Rnd * 26 + 65) n = 1 Do While n 15 产生一个字母 t = Chr(Int(Rnd * 26 + 65) 查询此字母是否已经产生 假设此字母还未产生 f = False For i = 1 To
23、 n If t = a(i) Then 已经出现重复 f = True Exit For End If Next 如果没有重复,则插入数组,并给n加1 If f = False Then n = n + 1 a(n) = t End If Loop 输出产生的字母 For i = 1 To 15 Picture1.Print Tab(i - 1) Mod 5) * 8 + 2); a(i); NextEnd Sub5-8属性设计:运算结果:代码编写:在此位置声明可以让所有的过程使用Dim a()Dim n As Integer 保存数组元素的个数Private Sub Command1_Click() m = Val(Text1.Text) p = Val(Text2.Text) 首先给数组扩容 n = n + 1 ReDim Preserve a(1 To n) 如果p1,则插入到第一个位置 If p n,则插入到第最后位置 If p n Then p = n 从第p个元素开始,将后面的元素后移一位,将p的位置腾出 For i = n - 1 To p Step -1 a(i + 1) = a(i) Next 将m插入指定位置 a(p) = m 输出结果 Picture1.Print Pict
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1