根据上述要求编写VB程序,功能如下:读取n个人进,出馆的时间,显示在列表框List1中。程序运行时,在文本框Text1中输入指定人数,单击“统计”按钮Command1,在标签Label1中显示馆内人数超过指定人数的总时长。程序运行界面如图所示.
Const n = 800
Dim d(1 To n)As String,a(1 To 2 * n)As String
Private Sub Form_Load( )
‘读取n个人进、出馆的时间,存入数组d并显示在列表框List1中,代码略
End Sub
Private Sub Command 1_ Click( )
Dim i As Integer,j As Integer,k As Integer,sp As Integer
Dim t As Integer,cnt As Integer,mts As Integer,sum As Integer
Dim s As String
‘提取每个人的进、出馆时间,分别加上IN、OUT标记后存入数组a
For i = 1 To n
a(i)=Mid(d(i),1,5)+“IN”
= Mid(d(i),7,5)+“OUT”
Next i
For i = 1 To 2 * n-1 ‘对数组元素a(1)到a(2*n)从小到大排序
For j = i To 2 * n-1
If a(j) < a(k) Then k = j
Next j
s = a(i):a(i) = a(k):a(k)=s
Next i
sp =Val(Text1.Text)
t=-1:cnt=0:sum=0
For i = 1 To 2 * n
mts=Val(Mid(a(i),1,2))*60+Val(Mid(a(i),4,2))
If Mid(a(i),6,2)=“IN” Then cnt = cnt + 1 Else
If cnt > sp Then
If t = -1 Then t = mts
ElseIf t > -1 Then
t=-1
End If
Next i
Label1.Caption = “超过指定人数的总时长:”+Sir(sum)+“分钟”
End Sub