(2)
实现挑选两样商品功能的VB程序如下,请在划线处填入合适的代码。
Private Sub Command1_Click()
Dim n,i,j,k,x,L,R,c As Integer
Dim goods(1 To 20)As String 'goods(i)存储商品的名称
Dim price(1To 20)As Integer 'price(i)存储商品的价格
Dim s,ch As String
s=Text1.Text:x=Val(Text2. Text)
j=1:k=0
For i=1 To Len(s)
ch=Mid(s,i,1)
If
ch="," Then
k=k+1
If k Mod
2=1 Then
________
Else
price(k\2)=Val(Mid(s,j,i-j))
End If
j=i+1
End If
Next i
n=k\2
For i=1 To n-1
For i=1 To
n-i
If price(j)>price(j+1)Then
k=price(j):price(j)=price(j+1):price(j+1)=k
End If
Next j
Next i
For i=2 To n
________
L=i:R=n
Do While L<=R
m=(L+R)\2
If price(m)+price(k)=x Then
c=c+1
List1. Addltem goods(k)& Str(price(k))&"元 和" & goods(m)& Str(price(m))& "元"
j=m
Do While price(j)=price(j-1)
j=j-1
c=c+1
Listl. Addltem goods(k)& Str(price(k))& "元 和" & goods(j)& Str(price(j))& "元"
Loop
Do While________
m=m+1
c=c+1
List1. Addltem goods(k)& Str(price(k))& "元 和" & goods(m)& Str(price(m))& "元"
Loop
Exit Do ‘Exit Do 表示退出Do循环
Elself price(m)+price(k)>x Then
R=m-1
Else
L=m+1
End If
Loop
Next i
List1. Addltem"共有:" & Str(c)& "种组合"
End Sub