小陈按上述要求编写了一个VB程序,功能如下:程序运行时,在列表框List1中显示排序前数据,单击“排序”按钮Command1,在列表框List2中显示排序结果。程序运行界面如图所示。请回答下列问题:
Const n = 200 ’学生数
Const m = 4 ’班级数
Dim xh(1 To n) As String, tmp1(1 To n) As String
Dim score(1 To n) As Integer, tmp2(1 To n) As Integer
Dim class(1 To n) As Integer, tmp3(1 To n) As Integer
Private Sub Form_Load()
'读取学生的学号、班级和成绩数据分别存储在数组xh,class和score中并在List1中显示
'代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim t3 As Integer, t2 As Integer, t1 As String
Dim c(0 To m + 1) As Integer
For i = 0 To m + 1
c(i) = 0
Next i
For i = 1 To n
c(k) = c(k) + 1
Next i
For i = 1 To m
c(i) = c(i) + c(i - 1)
Next i
For i = n To 1 Step -1
k = class(i)
tmp1(c(k)) = xh(i)
tmp2(c(k)) = score(i)
tmp3(c(k)) = class(i)
Next i
c(m + 1) = n
For k = 1 To m '对每一个班的学生成绩进行排序
For i = c(k) + 1 To c(k + 1) - 1
For j = c(k + 1) To i + 1 Step -1
If Then
t1 = tmp1(j): tmp1(j) = tmp1(j-1): tmp1(j-1) = t1
t2 = tmp2(j): tmp2(j) = tmp2(j-1): tmp2(j-1) = t2
t3 = tmp3(j): tmp3(j) = tmp3(j-1): tmp3(j-1) = t3
End If
Next j
Next i
Next k
'在List2中按统一格式输出排序后数据,代码略
End Sub