小王编写“合并区间” VB程序,功能如下:窗体加载时,获取并存储合并前的区间数据,并显示在列表框List1中。单击“合并”按钮后,以区间左端点数值对区间进行升序排序,然后相邻区间的相交进行合并,最后在列表框List2上显示合并后的区间。程序运行如图所示:
实现以上功能的VB程序如下,在横处填入合适的代码。
Dim a(1 To 20) As Integer ‘存储区间的左端点数值
Dim b(1 To 20) As Integer ‘存储区间的右端点数值
Private Sub Form_Load()
‘将区间左端点存入数组a,区间右端点存入数组b,并在列表框List1显示,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim curL As Integer, curR As Integer
For i=1 To n-1
For j=1 To n-i
If{#blank#}1{#/blank#}Then
t=a(j): a(j)=a(j+1): a(j+1)=t
t=b(j): b(j)=b(j+1): b(j+1)=t
End If
Next j
Next i
curL=a(1): curR=b(1)
For i=2 To n
If{#blank#}2{#/blank#}Then
If curR < b(i) Then{#blank#}3{#/blank#}
Else
List2.AddItem“["+Str(curL)+Str(curR)+"]”
curL=a(i): curR=b(i)
End If
Next i
List2. AddItem “[”+Str(curL)+Str(curR)+“]”
End Sub