VB程序设计经典案
VB程序设计经典案Option Base 1 Dim score(10) As Integer Dim order(10) As Integer Private Sub sort(a As Integer) '将数组成从大到小排序 Dim i As Integer, j As Integer 'i获取数组的下界,j获取数组下界 Dim t As Integer, n As Integer Dim temp As Integer 'temp作为中间值 i = LBound(a) j = UBound(a) Rem 以下循环实现排序 For n = j - 1 To 1 Step -1 For t = j To j - n + 1 Step -1 If a(t - 1) < a(t) Then temp = a(t - 1) a(t - 1) = a(t) a(t) = temp End If Next Next For t = i To j Picture2.Print a(t); Next End Sub Private Sub ord(b As Integer) '求学生的名次 Dim i As Integer, j As Integer, t As Integer, temp As Integer i = LBound(b) j = UBound(b) temp = 1 order(1) = 1 Rem 思路:定义temp为名次变量,若比较的值和上一个元素不同,则名次加1,否则不变 For t = i To j - 1 If b(t) <> b(t + 1) Then temp = temp + 1 order(t + 1) = temp Else order(t + 1) = temp End If Next For t = i To j Picture3.Print order(t); Next End Sub Private Sub Command1_Click '在picture1中输出数组元素 For i = 1 To 10 score(i) = Val(InputBox("please input:") Picture1.Print score(i); Next End Sub Private Sub Command2_Click '调用排序过程 Call sort(score) End Sub Private Sub Command3_Click '调用求名次过程 Call ord(score) End Sub Private Sub Command4_Click End End Sub Private Sub Command5_Click '将成绩和名次写入文件d:file1.data Dim i As Integer Open "d:file1.data" For Output As #1 For i = 1 To 10 Print #1, score(i); order(i) Next Close #1 MsgBox "已经成功写入文件" End Sub