
一个VB问题需要自定义函数
编写两个自定义函数Max和Min,其功能分别是求三个数中最大值和最小值。用InputBox输入5个整数,分别存到a,b,c,e,f这5分变量中,请用Max和Min函数求这...
编写两个自定义函数Max和Min,其功能分别是求三个数中最大值和最小值。用InputBox输入5个整数,分别存到a, b, c, e, f 这5分变量中,请用Max和Min函数求这5个数中的最大值和最小值。
展开
2个回答
展开全部
Private Function formax(a As Double, b As Double, c As Double) As Double //三数求最大
Dim maxdate As Double
maxdate = a
If maxdate < b Then
maxdate = b
End If
If maxdate < c Then
maxdate = c
End If
formax = maxdate
End Function
Private Function formin(a As Double, b As Double, c As Double) As Double //三数求最小
Dim mindate As Double
mindate = a
If mindate > b Then
mindate = b
End If
If mindate > c Then
mindate = c
End If
formin = mindate
End Function
主程序部分:
最大值变量max 最小值变量 min
五个输入值 a b c e f
求最大值:
max=formax(a,b,c)
max=formax(max,e,f)
求最小值:
min=formin(a,b,c)
min=formin(min,e,f)
Dim maxdate As Double
maxdate = a
If maxdate < b Then
maxdate = b
End If
If maxdate < c Then
maxdate = c
End If
formax = maxdate
End Function
Private Function formin(a As Double, b As Double, c As Double) As Double //三数求最小
Dim mindate As Double
mindate = a
If mindate > b Then
mindate = b
End If
If mindate > c Then
mindate = c
End If
formin = mindate
End Function
主程序部分:
最大值变量max 最小值变量 min
五个输入值 a b c e f
求最大值:
max=formax(a,b,c)
max=formax(max,e,f)
求最小值:
min=formin(a,b,c)
min=formin(min,e,f)
展开全部
Dim a As Double, b As Double, c As Double, d As Double, e As Double
Private Function GetMax(ByVal x1 As Double, ByVal x2 As Double, ByVal x3 As Double) As Double
GetMax=x1
If GetMax < x2 Then GetMax = x2
If GetMax < x3 Then GetMax = x3
End Function
Private Function GetMin(ByVal x1 As Double, ByVal x2 As Double, ByVal x3 As Double) As Double
GetMin=x1
If GetMin > x2 Then GetMin = x2
If GetMin > x3 Then GetMin = x3
End Function
Private Sub Command1_Click()
Print GetMax(a, b, GetMax(c, d, e))
Print GetMin(a, b, GetMin(c, d, e))
End Sub
Private Sub Form_Load()
a = Val(InputBox("输入a的值", "输入"))
b = Val(InputBox("输入b的值", "输入"))
c = Val(InputBox("输入c的值", "输入"))
d = Val(InputBox("输入d的值", "输入"))
e = Val(InputBox("输入e的值", "输入"))
End Sub
Private Function GetMax(ByVal x1 As Double, ByVal x2 As Double, ByVal x3 As Double) As Double
GetMax=x1
If GetMax < x2 Then GetMax = x2
If GetMax < x3 Then GetMax = x3
End Function
Private Function GetMin(ByVal x1 As Double, ByVal x2 As Double, ByVal x3 As Double) As Double
GetMin=x1
If GetMin > x2 Then GetMin = x2
If GetMin > x3 Then GetMin = x3
End Function
Private Sub Command1_Click()
Print GetMax(a, b, GetMax(c, d, e))
Print GetMin(a, b, GetMin(c, d, e))
End Sub
Private Sub Form_Load()
a = Val(InputBox("输入a的值", "输入"))
b = Val(InputBox("输入b的值", "输入"))
c = Val(InputBox("输入c的值", "输入"))
d = Val(InputBox("输入d的值", "输入"))
e = Val(InputBox("输入e的值", "输入"))
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询