按上述要求,编写一个VB程序,功能如下:在列表框List 1中显示整理前的数据,单击“整理”按钮Command 1,整理结果显示在列表框List 2中,程序运行界面如图所示。
Const n=200 ‘报名总人数
Const nc=10 ‘城市数
Dim city(1 To n) As Integer, pname(1 To n) As String, times(1 To n) As Integer
Dim b(1 To nc) As Integer ‘存储每个城市的报名人数
Dim c(1 To nc) As Integer
Dim q(1 To nc) As Integer
Private Sub Form_Load()
‘本过程读取城市序号,姓名和参加次数的数据分别存储在数组city;pname和times中,并在List 1中显示,代码略
End Sub
Private Sub Command 1_Click( )
Dim i As Integer, j As Integer, k As Integer, t As Integer, pos As Integer
For i=1 To nc
b(i)=0
Next i
For i=1 To n ‘统计每个城市报名人数
b(k)=b(k)+1
Next i
k=1
For i=1 To nc
c(i)=k
k=k+b(1)
Next i
For i=1 To n
k=city(i)
c(k)=c(k)+1
Next i
pos=1
For i=1 To nc ‘对各城市报名数据按参加志愿服务的次数进行排序
For j=pos To pos +b(i)-2
k=fp(j, pos+b(i)-1)
t=q(k) : q(k )=q(j) : q(j)=t
Next j
pos=pos+b(i)
Next i
For i-1 To n
List 2.AddItem " " & city(q(i))& " " & pname(q(i))& " " & times(q(i))
Next i
End Sub
Function fp(head As Integer,tail As Integer)As Integer
Dim i As Integer,k As Integer
k=head
For i=
If times (q(i))>times (q(k)) Then k=i
Next i
fp=k
End Function