某医院接受捐赠物资,各项物资数据包含编号、品名和收支数量。编号构成规则是:急需等级(单个大写字母,由高到低划分为A、B、C三个等级)+物资种别(单个大写字母,至多有A~Z共26种)。为提高库存管理效率,现需要整理物资库存,要求:先对同种物资数量进行合并统计;然后按各种物资库存量,更新其急需等级,并依等级由高到低分类显示。等级划分规则为:当库存量低于3天所需,设置为A级;当库存量低于7天所需,设置为B级;否则设置为C级。
按上述要求,编写一个VB程序,功能如下:在列表框List1中显示整理前的所有物资相关数据,单击“库存合并统计”按钮Command1,在列表框List2中显示整理结果,程序运行界面如图所示。
(1)
由图中示例可知,急需等级发生变化的物资有种(填数字)。
(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 To2 * 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 j<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
① ② ③
答案: 【1】2
【1】b(k)= b(k) + totals(i)【2】r= b(q(j))或r= b(m)【3】pos <= head