程序运行时,在文本框Text1中输入共有礼物数n,单击“读取数据”按钮Command1,将从数据库gift. accdb中读取n个礼物的信息存储在相应的数组中,并显示在列表框List1中;在文本框Text2中输入轮到你拿礼物的序号(即第几个拿),单击“计算”按钮 Command2,则在列表框List2显示你获得的礼物信息,并在列表框list2最后一行显示获得的礼物总数。程序运行界面如图所示。
实现上述功能的VB程序代码如下,请回答下列问题:
Dim conn as New ADODB.Connection
Dim rs as New ADODB.Recordset
Dim bh(1 To 100) As string '数组bh存储礼物的编号
Dim x(1 To 100) As Single,y(1 To 100) As single '数组x、y分别存储礼物的横坐标和纵坐标
Dim dist(1 To 100) As single '数组dist存储礼物到原点的距离
Dim n As Integer '变量n表示礼物数
Private Sub Command1_Click()
Dim i As Integer
'从数据库中读取n件礼物的编号、横坐标、纵坐标,并存储在相应的数组中'代码略,下面代码的功能是计算各礼物到原点的距离
For i=1 To n
dist(i)= ①
List1. AddItem bh(i)+""+Str(x(i))+""+Str(y (i))+""+Str(dist(i))
Next i
End Sub
Private Sub Command2_Click()
Dim xh As Integer, i As Integer, j As Integer, k As Integer
Dim templ As String, temp2 As Single, p As Single, s As Single
xh=Val(Text2. Text)
'将礼物按到原点的距离从小到大排序,代码略
i=0: j=1: s=0
Do While (i< xh) And (j<=n)
If dist(j)<>s Then
②
i=i+1
End if
j=j+1
Loop
u=0
j=j-1
Do While ③
List2. AddItem bh(j)+Str(x(j))+Str(y(j))+Str(dist(j))
u=u+1
j=j+1
Loop
List2. AddItem"total:"+Str(u)
End sub
① ② ③