我能得到一些优化我的程序的帮助吗?我试图使我的代码尽可能高效,我也需要帮助排序一个二维阵列。
程序分析
编写一个程序,其中将包含一份名单,滑雪跳台的竞争对手和他们的分数。然后将此列表存储在外部文件中。对于每一个跳投者,删除最高和最低分数,然后计算其余分数的总和,将这个总数与跳投者的名字一起输出到一个新的外部文件中,其中包含竞争对手分数的文件将从最高到最低排序。必须包括一个函数。
代码:
Private Sub cmdStart_Click()
'Declare Variables used to Store Values
Dim Names(5) As String
'2D Array used to Display Score
'Value 1 represents number of competitor, while value 2 is ScoreNumber
'i.e Score(2,5) Represents Score 5(out of 5) of competitor 2( out of 5)
'Sixth Value in Array used to store Total Score
Dim Score(5, 8) As Integer
Dim CompetitorPointer As Integer
Dim counter As Integer
Dim Filename As String
'Input Scores for each Competitor into an Array
counter = 1
For counter = 1 To 5
Names(counter) = InputBox("What is the Name of Competitor " & counter & " ?")
For scorecounter = 1 To 5
Score(counter, scorecounter) = InputBox("What is score " & scorecounter & " of Competitor " & counter & " ?")
Next
Next
For CompetitorPointer = 1 To 5
maximum = FindMax(CompetitorPointer, Score)
minimum = FindMin(CompetitorPointer, Score)
Score(CompetitorPointer, 7) = maximum
Score(CompetitorPointer, 8) = minimum
Next
'Find total Score of Competitor
For CompetitorPointer = 1 To 5
CompetitorScore = 6
TotalScore = Score(CompetitorPointer, 1) + Score(CompetitorPointer, 2) + Score(CompetitorPointer, 3) + Score(CompetitorPointer, 4) + Score(CompetitorPointer, 5)
Score(CompetitorPointer, CompetitorScore) = TotalScore
MaxMin = Score(CompetitorPointer, 7) + Score(CompetitorPointer, 8)
Score(CompetitorPointer, CompetitorScore) = TotalScore - MaxMin
Next
Min_Index = 1
Max_Index = 5
'File manipulation Program
'Create File
Open "Z:\AHComputing\VB\PROJECT\File.txt" For Output As #1
intMsg = MsgBox("File opened")
'Sorting Algorithm
'Selection sort is a simple sorting algorithm that mimics the way humans instinctively sort.
'It works by first scanning the entire list to find the smallest element, swapping it into the first position.
'It then finds the next smallest element, swapping that into the second position, and so on until the list is sorted.
Dim i As Long
Dim j As Long
Dim iMin As Long
Dim iMax As Long
Dim varSwap As Variant
For CompetitorPointer = 1 To 5
Min = 1
Max = 5
For i = Min To Max - 1
iMin = Score(CompetitorPointer, i)
For j = (i + 1) To 5
If Score(CompetitorPointer, j) < iMin Then iMin = Score(CompetitorPointer, j)
Next
varSwap = Score(CompetitorPointer, i)
Score(CompetitorPointer, i) = iMin
iMin = varSwap
Next
Print #1, Names(CompetitorPointer), Score(CompetitorPointer, CompetitorScore)
Next CompetitorPointer
Close #1
intMsg = MsgBox("File closed")
End Sub
'Used to Find Highest Score of Competitor
Private Function FindMax(CompetitorPointer As Integer, ByVal Score)
maximum = Score(CompetitorPointer, 1)
scorecounter = 1
For scorecounter = 1 To 5
If Score(CompetitorPointer, scorecounter) > maximum Then
maximum = Score(CompetitorPointer, scorecounter)
End If
Next
FindMax = maximum
End Function
'Used to Find Lowest Score of Competitors
Private Function FindMin(ByRef CompetitorPointer As Integer, ByVal Score)
minimum = Score(CompetitorPointer, 1)
scorecounter = 1
For scorecounter = 1 To 5
If Score(CompetitorPointer, scorecounter) < minimum Then
minimum = Score(CompetitorPointer, scorecounter)
End If
Next
FindMin = minimum
End Function我能得到一些排序数组的帮助吗?我能得到一些改进代码以提高效率的技巧吗?我正在阅读记录,我想知道它们是否更适合存储这些值?
请注意,这些评论都是供我个人使用的,所以你可以忽略它们。谢谢
发布于 2015-03-24 15:09:42
我会将数据放入数据库文件(我使用MS ),然后您可以使用SQL语句进行排序、查找max/min值等。您不需要运行Access,一切都可以在VB6内部完成。可以使用Access创建包含定义的表和字段的空数据库文件。其他的一切都可以在VB6 John内部完成
发布于 2015-03-25 09:29:20
对于排序,请使用ADO和断开连接的记录集。您不需要硬盘上的数据库,它是内存中的全部解决方案.它既快速又健壮,没有任何实验。请参阅我以前关于sorting with ADO and disconnected recosrdet的文章以及完整的示例代码。
https://stackoverflow.com/questions/29213727
复制相似问题