发布网友 发布时间:2022-04-25 12:02
共3个回答
热心网友 时间:2024-11-01 13:17
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) = i
Next
For i = 1 To UBound(ord) '排序ing
For j = i + 1 To UBound(ord)
If all(ord(i)).AvgScore < all(ord(j)).AvgScore Then
ord(0) = ord(i)
ord(i) = ord(j)
ord(j) = ord(0)
End If
Next
Next
For i = 1 To UBound(ord) '按名次排序
all(ord(i)).No = i
Next
'按照名次显示(两段代码选其中一段)
Print "学号 姓名 英语 数学 平均 名次 (按名次排)" 'print title
For i = 1 To UBound(ord)
With all(ord(i))
tmp = .Id + " " + .StuName + " " + CStr(.English) + " " + CStr(.Math) + " " + CStr(.AvgScore) + " " + CStr(.No)
Print tmp
End With
Next
'按读入顺序显示(两段代码选其中一段)
Print "学号 姓名 英语 数学 平均 名次 (按读取顺序排)" 'print title
For i = 1 To UBound(ord)
With all(i)
tmp = .Id + " " + .StuName + " " + CStr(.English) + " " + CStr(.Math) + " " + CStr(.AvgScore) + " " + CStr(.No)
Print tmp
End With
Next
End Sub
热心网友 时间:2024-11-01 13:12
Dim p() as integer,i as integer,str as string,LineCount as long
Open "C:\Test.txt" For Input As #1
do while not eof(1)
Line Input #1,str
linecount = linecount+1
p=Split(str," ")
p(4)=format((p(2)+p(3))/2,"###.##") '平均分,以此类推,如果要跟上下文关联可以再加一个q()。
loop
close #1
================================
Split以后(p=Split(str," "))以后
p(0)就是学号
p(1)就是姓名
p(2)就是数学
p(3)就是英语
p(4)就是平均成绩
p(5)就是名字
如果后面还有空格,则p(6)就是后面的..
热心网友 时间:2024-11-01 13:12
Dim
p()
as
integer,i
as
integer,str
as
string,LineCount
as
long
Open
"C:\Test.txt"
For
Input
As
#1
do
while
not
eof(1)
Line
Input
#1,str
linecount
=
linecount+1
p=Split(str,"
")
p(4)=format((p(2)+p(3))/2,"###.##")
'平均分,以此类推,如果要跟上下文关联可以再加一个q()