2.
大家期盼的校运会要来了,同学们踊跃报名,共有20人报了100米,30人报了200米。小明编写了一个VB程序,用于查询同时报名参加100米和200米比赛的学生,并统计学生数。具体功能如下:程序运行时,在列表框List1中显示报100米的学生学号和姓名,在列表框List2中显示报200米的学生学号和姓名,单击“排序”按钮Command1后,将List2中数据按“姓名”升序排序,并将排序后的数据重新显示在列表框List2中;单击“查询并统计”按钮Command2后,在列表框List3中显示同时报名参加100米和200米比赛的学生信息,并在该列表框的最后显示同时报名这两项比赛的学生数。程序运行界面如下图所示:
为实现上述功能的程序如下,请在划线处填入合适的语句或代码。
Dim xm1(1 To 20) As String, xh1(1 To 20) As String′数组xm1、xh1分别存储选报课程1的学生的姓名和学号
Dim xm2(1 To 30) As String, xh2(1 To 30) As String′数组xm2、xh2分别存储选报课程2的学生的姓名和学号
Const n = 20, m = 30
Private Sub Form_Load()
'获取选报数据,并显示在列表框List1和List2中
'代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, tt As String
List2.Clear
For i = 1 To m - 1
For j =Step -1
If xm2(j) < xm2(j - 1) Then
tt = xm2(j): xm2(j) = xm2(j - 1): xm2(j - 1) = tt
tt = xh2(j): xh2(j) = xh2(j - 1): xh2(j - 1) = tt
End If
Next j
Next i
For i = 1 To m
List2.AddItem xh2(i) + “ ” + xm2(i)
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer, w As Integer, tt As String, k As Integer
For i = 1 To n
p = 1
Do While p <= q
w = (p + q) \ 2
If xm1(i) = xm2(w) Then
List3.AddItem xh1(i) + “ ” + xm1(i)
k = k + 1
End If
If Then
q = w - 1
Else
p = w + 1
End If
Loop
Next i
List3.AddItem “共有” + Str(k) + “ 位同学”
End Sub