VB程序中,编写一个求三个数中最大值max和最小值min的function过程,然后用这个过程分别 10
max As Long
min As Long
End Type
Private Function MaxAndMin(ByRef a() As Long, l As Integer) As MAXMINVAL
MaxAndMin.max = a(0)
MaxAndMin.min = a(0)
For i = 0 To l
If (a(i) > MaxAndMin.max) Then MaxAndMin.max = a(i)
If (a(i) < MaxAndMin.max) Then MaxAndMin.max = a(i)
Next
End Function
Private Sub Command1_Click()
Dim a(4) As Long
Dim val As MAXMINVAL
a(0) = 500
a(1) = 300
a(2) = 1000
a(3) = 2000
a(4) = 3000
val = MaxAndMin(a, 4)
'Text1.Text = val.max
'Text2.Text = val.min
End Sub
Private Sub Form_Click()
Dim i As Integer, n As Integer
Dim a() As Integer
n = 3
ReDim a(n)
For i = 1 To n
a(i) = Rnd * 100
Next
Call Sortnum(a, n)
Print "Max:" & MaxNum(a, n)
Print "Min:" & MinNum(a, n)
n = 5
ReDim a(n)
For i = 1 To n
a(i) = Rnd * 100
Next
Call Sortnum(a, n)
Print "Max:" & MaxNum(a, n)
Print "Min:" & MinNum(a, n)
n = 7
ReDim a(n)
For i = 1 To n
a(i) = Rnd * 100
Next
Call Sortnum(a, n)
Print "Max:" & MaxNum(a, n)
Print "Min:" & MinNum(a, n)
End Sub
Function MaxNum(a() As Integer, n) As Integer
Dim i As Integer, m As Integer
m = a(1)
For i = 1 To n
If m < a(i) Then m = a(i)
Next
MaxNum = m
End Function
Function MinNum(a() As Integer, n) As Integer
Dim i As Integer, m As Integer
m = a(1)
For i = 1 To n
If m >a(i) Then m = a(i)
Next
MinNum = m
End Function