某学校进行问卷调查,共n个班级,现将参与问卷调查的学生名单在文本框Text1中按照“班级编号+姓名”的方式连续输入。单击“查询”按钮,统计每个班参与问卷调查的人数。在列表框List1中按“参与人数”降序输出,当“参与人数”相同时,按班级编号升序输出。程序运行界面如图。实现上述功能的VB程序段如下:
Private Sub Search_Click()
Const n = 12
Dim s As String, c As String
Dim i As Integer, t As Integer
Dim rs(0 To n) As Integer '数组rs存储班级人数
Dim bj(1 To n) As Integer '数组bj存储班级编号
Dim flag As Boolean
s = Text1.Text
t = 0
For i = 1 To Len(s)
c = Mid(s, i, 1)
If c >= "0" And c <= "9" Then
t = ①
flag = True
ElseIf flag = True Then
rs(t) = rs(t) + 1
flag = False
t = 0
End If
Next i
For i = 1 To n
bj(i) = i
Next i
For i = 1 To n-1
k = i
For j = i + 1 To n
If ② Then k = j
Next j
If k <> i Then
t = rs(k): rs(k) = rs(i): rs(i) = t
t = bj(k): bj(k) = bj(i): bj(i) = t
End If
Next i
For i = 1 To n
List1.AddItem Str(bj(i)) + "班有:" + " " + + "人"
Next i
End Sub