15.編寫一個VB抽獎程序,功能如下:從數據庫讀取相關數據并顯示在列表框List1中,在文本框Text1中輸入中獎人數,單擊“抽獎”按鈕Command1,在列表框List2中輸出 結果。 抽獎規則:中獎人數為1人,男女不限; 中獎人數超1人,男女對半(奇數時女性多1人)。 程序運行界面如圖所示。 實現上述功能的VB程序如下: Dim xm(1 To 500)As String,xh(1 To 500)As String,xb(1 To 500)As String Dim n As Integer'學生總人數 Dim a(1 To 100)As Integer'抽中序號 Private Sub Form_Load ( ) '本過程讀取n位同學的序號、姓名和性別, '分別存儲在數組xh、xm、xb中 '并在List1中顯示,代碼略 End Sub Private Sub Command1_Click ( ) Dim rs As Integer,m As Integer,x As Integer,girl As Integer,i As Integer Dim xbs(1 To 500)As Integer Randomize List2.Clear List2.AddItem“姓名“&““&“性別“ rs=
For i=1 To n If xb(i)=“女“Then xbs(i)=1 Else xbs(i)=0 Next i m=0 girl=0 If rs=1 Then x=Int(Rnd*n)+1 List2.AddItem xm(x) &““& xb(x) Else Do While m<rs x=Int(Rnd*n)+1 If checkf(x) Then m=m+1:a(m)=x girl=girl+xbs(a(m)) If
Then a(m)=0:m=m-1 ElseIf girl>(rs+1)\2 Then a(m)=0:m=m-1
End If End If Loop For i=1 To m List2.AddItem Next i End If End Sub Function checkf(p As Integer) As Boolean '核對重復中獎,未中過獎返回True '代碼略 End Function (1)請在橫線處填入合適的代碼。 (2)程序中加框代碼有錯,請改正。
種(填數字)。 (2)請在畫線處填入合適的代碼。 Const n=2000'物資總項數 Const nc=26'物資種別數,最多不會超過26 Dim items(1 To n)As String,supplies(1 To n)As String,totals(1 To n)As Long Dim g(1 To 2*nc)As Long'存儲各種物資3天、7天的需求數量 Dim b(1 To nc)As Long'存儲各種物資的庫存量 Dim c(1 To nc)As Long Dim q(1 To nc)As Integer Private Sub Form_Load
'本過程讀取編號、品名、收支、需求的數據分別存儲在數組items、supplies、totals、g中,并在List1中顯示有關數據;g數組中g(1)、g(2)存儲物資A的3天、7天的需求數量;g(3)、g(4)存儲物資B的3天、7天的需求數量……代碼略 '代碼略 End Sub Private Sub Command1_Click
Dim i As Integer,j As Integer,k As Integer,top As Integer,bottom As Integer Dim r As Long,s As String,t As Integer,m As Integer For i=1 To nc b(i)=0:c(i)=0 Next i For i=1 To n'統計每種物資的庫存量 k=Asc(Mid(items(i),2,1))-Asc(“A“)+1
c(k)=i Next i j=0 For i=1 To nc If c(i)<>0 Then j=j+1:q(j)=i Next i top=0:bottom=j+1 j=1:k=bottom-1 Do While i<bottom '按各種物資的需求量進行等級分類 m=q(j)
If r<g(2*m-1)Then top=top+1 If top<>j Then t=q(top):q(top)=q(j):q(j)=t j=j+1 ElseIf r>=g(2*m)Then bottom=bottom-1 t=q(j):q(j)=q(bottom):q(bottom)=t Else j=j+1 End If Loop List2.AddItem“編號 品名 庫存“ List2.AddItem“------“ For i=1 To k s=fp(i,top,bottom) List2.AddItem““& s &““& supplies(c(q(i)))&““& b(q(i)) Next i End Sub Function fp(pos As Integer,head As Integer,tail As Integer)As String If
Then fp=“A“ ElseIf pos<tail Then fp=“B“ Else fp=“C“ End If fp=fp & Mid(items(c(q(pos))),2,1) End Function