如何用VB计算加权平均分

来源:百度知道 编辑:UC知道 时间:2024/05/15 04:51:02
条件为,有一班级学生成绩TXT文档.读取文档
学号 姓名 英语 数学 平均成绩 名次
1 张某 79 89
……
要求计算加权平均分,并按平均成绩显示名次(保留2位小数,分数相同名次一样)。英语学分为4,数学为6
显示效果为
学号 姓名 英语 数学 平均成绩 名次
1 张某 79 89 85.2 5
……
加权平均就是
(英语成绩*学分+数学*学分)/(学分之和)

并不只有一个数据。。名次要显示出来。。我只是列举了一个数据

Private Sub Command1_Click()
Open App.Path + "\score.txt" For Input As #1
Dim linetxt As String

Dim AvgScore() As Double

If Not EOF(1) Then
Line Input #1, linetxt '过滤掉第一行的标题
End If

Dim all() As StuInfo

Counter = 0
Do While Not EOF(1) '读数据
Line Input #1, linetxt
If Len(Trim(linetxt)) > 0 Then
arr = Split(linetxt, " ")
If UBound(arr) >= 3 Then
Counter = Counter + 1
ReDim Preserve all(Counter) As StuInfo
With all(Counter)
.Id = arr(0)
.StuName = arr(1)
.English = CDbl(arr(2))
.Math = CDbl(arr(3))
.AvgScore = Round((.English * 4 + .Math * 6) / (4 + 6), 2)
End With
End If
End If
Loop
Close #1

'排序
ReDim ord(Counter) As Integer '初始化
For i = 1 To UBound(ord)
ord(i) =