分配方法是:按考场号递增、同一考场座位号递增的顺序逐一分配座位。每次分配,先选班级,再选学生。选择班级时,在班级降序序列(按未分配人数)中选择第1个班级,但如果该班和前一次分配选定的班级相同,则改选第2个班级。选定班级后,再为该班耒分配学生中序号最大的学生分配考场座位,并维护班级降序序列(按未分配人数)。
编写VB程序,实现考场分配功能:在文本框Text1中填写入学年份,单击“读取”按钮Command1后,将各班数据按人数降序显示在列表框List1中,然后单击“分配”按钮Command2,在列表框List2中显示分配结果。程序运行界面如图所示。
请回答下列问题:
Dim n As Integer, y As String
Dim cla(1 To 20)As Integer, num(I To 20)As Integer
Dim room as Integer ‘存储考场号
Dim seat as Integer ‘存储座位号
Function fm(k As Integer) As String
‘返回整数k(l≤k≤99)对应的数字字符串,不足两位左侧补“0”,代码略
End function
Private Sub Command 1_ Click( )
‘从Text1中读取入学年份存入变量y,从数据库中读取该人学年份的班级数据
‘将班级个数存人变量n(1<n≤20),各班班号和人数分别存人数组cla和num
‘各班班号均大于0,各班人数均未超过总人数的一半
‘将数组cla和num按班级人数降序排列后,显示在List l中,代码略
End Sub
Private Sub Command 2_ Click( )
Dim i As Integer, t As Integer, s As Integer
Dim choice As Integer, m As Integer, f As Boolean
room=1 : seat=1 : choice=0
Do While f = True
If cla (1) <> choice Then m= 1 Else m = 2
choice = cla(m)
‘在列表框List2中显示准考证号、考场号、座位号
List 2. Addltem y+fm(cla(m))+fm(num(m))+" "+fm(room)+" "+fm(seat)
seat = seat+ 1
If seat > 30 Then
seat = 1
End If
num(m)= num(m)-1
For i=To n-1 ‘维护班级降序序列(按未分配人数)
If num(i)< num(i + 1)Then
t=num(i) : num(i)=num(i+1) : num(i+1)=t
s = cla(i) : cla(i)= cla(i + 1) : cla(i+1)=s
Else
Exit For ‘ExitFor表示退出循环
End If
Next i
IfThen f = False
Loop
End Sub