设计一个VB程序,模拟生成10件货物的上升或者下降需求,在数组en和ex中分别记录每件货物起始楼层和目标楼层,将初始需求输出到列表框List1中,然后点击按钮Command1完成货物起始楼层先升后降的排序,当起始楼层相同时,上升中则目标楼层升序,下降中则目标楼层降序。将排序结果输出到列表框List2,最后计算出上升和下降分别需要的总时长输出到文本框Text1。程序界面如下:
Const n = 10:Const m = 7
Dim en(1 To n) As Integer, ex(1 To n) As Integer
Dimf(1 To n) As Integer '值为1表示货物往上运送,值为-1表示货物往下运送
'n件货物起始楼层、目标楼层以及升降值分别存入数组en,ex,f,输出到List1,代码略
Private Sub Command1_Click()
Dim i As Integer, j As Integer, up1 As Integer, up2 As Integer
Dim t(1 To 2) As Integer, first As Integer, last As Integer, p As Integer
For i = 1 To n -1
For j = n To i + 1 Step -1
up1 = f(j)*(en(j)*10+ex(j)): up2 = f(j-1)*(en(j-1)*10+ex(j-1))
If f(j) > f(j -1) Or f(j) = f(j -1) AndThen
p = f(j): f(j) = f(j -1): f(j -1) = p
p = en(j): en(j) = en(j -1): en(j -1) = p
p = ex(j): ex(j) = ex(j -1): ex(j -1) = p
End If
Next j
Next i
i = 1: first = en(1): last = ex(1): p = 1
Do While i < n '计算货梯上升时间和下降时间
i = i + 1
If Then '改错
t(p) = t(p) + (last -first) * 3
last = ex(i): first = en(i)
ElseIf f(i) * (en(i) -last) > 0 Then
t(p) = t(p) + f(i) * (last -first) * 3
last = ex(i): first = en(i)
ElseIf f(i) * (en(i) -last) <= 0 And f(i) * (ex(i) -last) > 0 Then
End If
Loop
t(p) = t(p) + f(i) * (last -first) * 3
Text1.Text = "升降货梯分别需要" & t(1) & "和" & t(2)& "分钟完成。"
'将排序后的升降信息输出到List2,代码略
End Sub