某学校推出一款食堂套餐查询小程序。学生登录该小程序后,可以查询相关套餐信息,系统根据以往购买评分,给出相关套餐的平均评分以及推荐指数。
编写一个VB程序实现如下功能:程序运行后自动读取数据库中关于食堂套餐的相关数据,在文本框Text1中输人查找套餐的关键词,搜索到的套餐信息在列表框List1中显示,程序计算套餐平均评分(四舍五入保留一位小数)和推荐指数,并显示在套餐信息下方。程序运行界面如图所示。
Const n = 50
Dim food(1 To n) As String
Dim price(1 To n) As Integer
Dim score(1 To n) As Single
Private Sub Form_ Load()
‘读取数据库中套餐名称、套餐价格和套餐评分信息分别存储于数组food、price和score中,代码略。
End Sub
Private Sub Command1 _Click()
Dim food(1 To n) As String, s As String
Dim price(1 To n) As Integer
Dim judge(1 To n) As Boolean
Dim score(1 To n) As Single, sum As Single, ave As Single
Dim i As Integer, j As Integer, t As Integer, q As Integer
Dim w1 As Boolean, w2 As String, w3 As Integer, w4 As Single
s = Text1.Text : q= 0
For i=1 To n
①
j= 1
Do While True
If j+Len(s)-1<=t Then
If s = Mid( food(i), j, Len(s)) Then
judge(i) = True
q=q+1
Else
②
End If
End If
If Then
Exit Do
End If
Loop
Next i
For i=1 To n-1
For j=n To i+1 Step -1
If Not judge(j-1) And judge(j) Then
w1 = judge(j): judge(j) = judge(j-1): judge(j-1) = w1
w2 = food(j): food(j) = food(j - 1): food(j-1) = w2
w3 = price(j): price(j) = price(j -1): price(j-1) = w3
w4 = score(j): score(j) = score(j-1): score(j -1) = w4
End If
Next j
Next i
For i= ③
List1. AddItem food(i) +Str( price(i))+"元"
sum =sum+score(i)
Next i
ave = Int(sum/ q * 10 +0.5) / 10
‘根据平均评分ave 的值给出推荐指数。若平均评分高于4.5分显示推荐指数"* **",若平均评分高于4.0分显示推荐指数"* *",4.0分以下的显示推荐指数" *",代码略。
End Sub