VB数组程序设计
实验三 数组程序设计
一、实验目的
通过实验掌握vb的数组的概念和使用。
二、实验设备
1(PC机一台;2(VB 6.0集成开发环境。
三、实验
及要求
1. 编写程序实现方阵的运算示例。点击“生成方阵A、B”按钮,允许用户输入方阵阶数,根据阶数随机产生两个方阵。分别点击运算框中的命令按钮,完成相应的运算。
要求:方阵A、B、C采用动态二维数组存放;三个运算命令按钮使用命令按钮控件数组。 2.编写程序实现一个信息输入与查询系统。用户从界面输入人员信息,点击新增按钮将信息加入记录数组;通过界面上的按钮完成人员信息的查询。要求使用记录数组。 四、程序代码草稿
五、实验结果
(1)方阵运算最终代码:
Dim A() As Integer Next i Dim B() As Integer Print
Dim C() As Integer Next j
Dim x As Integer End Sub
Dim j As Integer
Dim i As Integer
Private Sub Command2_Click(Index As
Private Sub Command1_Click() Integer)
Show x = InputBox("请输入方阵阶数", "阶数")
Show Select Case Index Randomize Case 0:
ReDim A(x, x) Print
ReDim B(x, x) Print "方阵A:"
For j = 1 To x Print "方阵A:"
For j = 1 To x For i = 1 To x
For i = 1 To x Print Format(A(i, j), "0 "); "";
A(j, i) = Int(10 * Rnd) Next i
Print Format(A(j, i), "0 "); ""; Print
Next i Next j
Print Print
Next j Print "方阵B:"
Print For j = 1 To x
For i = 1 To x Print "方阵B:"
For j = 1 To x Print Format(B(i, j), "0 "); "";
For i = 1 To x Next i
B(j, i) = Int(10 * Rnd) Print
Print Format(B(j, i), "0 "); ""; Next j
Case 1: Print "C=A*B" Print Randomize
Print "C=A+B" ReDim C(x, x) Randomize For j = 1 To x ReDim C(x, x) For i = 1 To x For j = 1 To x C(j, i) = A(j, i) * B(j, i)
For i = 1 To x Print Format(C(j, i), "0 "); "";
C(j, i) = A(j, i) + B(j, i) Next i
Print Format(C(j, i), "0 "); ""; Print
Next i Next j
Print End Select Next j
Case 2: End Sub
Print
程序运行结果考屏:
(2)输入与查询系统最终代码:
Dim M() As Member Text3.Text = M(j + 1).class Dim x As Integer Text4.Text = M(j + 1).pay
Exit For
Private Sub Command1_Click(Index As End If
Integer) Next
Dim i As Integer Case 3
Dim j As Integer Text1.Text = M(x).no Select Case Index Text2.Text = M(x).name Case 0 Text3.Text = M(x).class Text1.Text = M(1).no Text4.Text = M(x).pay Text2.Text = M(1).name End Select
Text3.Text = M(1).class
Text4.Text = M(1).pay End Sub
Case 1
For i = 1 To x Private Sub Command5_Click() If M(i).no = Text1.Text And i <> 1 Then ReDim Preserve M(1 To (x + 1)) As
Text1.Text = M(i - 1).no Member
Text2.Text = M(i - 1).name x = x + 1
Text3.Text = M(i - 1).class Cls
Text4.Text = M(i - 1).pay M(x).no = Text1.Text
M(x).name = Text2.Text
Exit For M(x).class = Text3.Text End If M(x).pay = Text4.Text Next End Sub
Case 2
For j = 1 To x Private Sub Command6_Click() If M(j).no = Text1.Text And j <> x Then Dim i As Integer
Text1.Text = M(j + 1).no For i = 1 To x
Text2.Text = M(j + 1).name If Text2.Text = M(i).name Then
Text1.Text = M(i).no 模块代码:
Text3.Text = M(i).class Type Member
Text4.Text = M(i).pay no As Long End If name As String * 5 Next class As String * 10
pay As Long End Sub End Type
程序运行结果考屏:
查询:
六、实验中遇到的问题与解决方法或实验收获