VB语言,编写一个需要使用Function过程调用的题目,求高手指点!
编写程序,输入任意一个不超过9位的正整数,求出由该正整数的全部数字组成的同样位数的一个最大整数和最小整数。题目要求使用Function的过程调用,主调过程应该不至于太复杂...
编写程序,输入任意一个不超过9位的正整数,求出由该正整数的全部数字组成的同样位数的一个最大整数和最小整数。题目要求使用Function的过程调用,主调过程应该不至于太复杂,思路大概只要把数字进行下排序就可以了,最大数好做,最小数注意0不能放在第一位,所有还得单独来处理0的问题,程序界面如图:
求高手指点代码! 展开
求高手指点代码! 展开
2个回答
展开全部
Function f_Max(num As Long) As Long
Dim sA As String
Dim sB As String
Dim sC As String
Dim sD As String
Dim sE As String
Dim iA As Integer
Dim iB As Integer
sA = CStr(num)
Do While sA <> ""
sB = ""
For iA = 1 To Len(sA)
'求出最大的数
sC = Mid(sA, iA, 1)
If sB < sC Then
sB = sC '记录数字值
iB = iA '记录数字位置
End If
Next
sE = sE & sB '这是最大的数
'求出剩余的数字
sD = ""
For iA = 1 To Len(sA)
If iA <> iB Then
sD = sD & Mid(sA, iA, 1)
Else
sD = sD & Mid(sA, iA + 1)
Exit For
End If
Next
sA = sD
Loop
f_Max = CLng(sE)
End Function
Function f_Min(num As Long)
Dim sA As String
Dim sB As String
Dim sC As String
Dim sD As String
Dim sE As String
Dim iA As Integer
Dim iB As Integer
sA = CStr(num)
Do While sA <> ""
sB = "9"
For iA = 1 To Len(sA)
'求出最小的数
sC = Mid(sA, iA, 1)
If sB > sC Then
sB = sC '记录数字值
iB = iA
End If
Next
sE = sE & sB '这是最小的数
'求出剩余的数字
sD = ""
For iA = 1 To Len(sA)
If iA <> iB Then
sD = sD & Mid(sA, iA, 1)
Else
sD = sD & Mid(sA, iA + 1)
Exit For
End If
Next
sA = sD
Loop
iA = Len(sE)
iB = Len(CStr(CLng(sE)))
If iA > iB Then
f_Min = CLng(Left(CLng(sE), 1) & String(iA - iB, "0") & Mid(CStr(CLng(sE)), 2))
Else
f_Min = CLng(sE)
End If
End Function
Dim sA As String
Dim sB As String
Dim sC As String
Dim sD As String
Dim sE As String
Dim iA As Integer
Dim iB As Integer
sA = CStr(num)
Do While sA <> ""
sB = ""
For iA = 1 To Len(sA)
'求出最大的数
sC = Mid(sA, iA, 1)
If sB < sC Then
sB = sC '记录数字值
iB = iA '记录数字位置
End If
Next
sE = sE & sB '这是最大的数
'求出剩余的数字
sD = ""
For iA = 1 To Len(sA)
If iA <> iB Then
sD = sD & Mid(sA, iA, 1)
Else
sD = sD & Mid(sA, iA + 1)
Exit For
End If
Next
sA = sD
Loop
f_Max = CLng(sE)
End Function
Function f_Min(num As Long)
Dim sA As String
Dim sB As String
Dim sC As String
Dim sD As String
Dim sE As String
Dim iA As Integer
Dim iB As Integer
sA = CStr(num)
Do While sA <> ""
sB = "9"
For iA = 1 To Len(sA)
'求出最小的数
sC = Mid(sA, iA, 1)
If sB > sC Then
sB = sC '记录数字值
iB = iA
End If
Next
sE = sE & sB '这是最小的数
'求出剩余的数字
sD = ""
For iA = 1 To Len(sA)
If iA <> iB Then
sD = sD & Mid(sA, iA, 1)
Else
sD = sD & Mid(sA, iA + 1)
Exit For
End If
Next
sA = sD
Loop
iA = Len(sE)
iB = Len(CStr(CLng(sE)))
If iA > iB Then
f_Min = CLng(Left(CLng(sE), 1) & String(iA - iB, "0") & Mid(CStr(CLng(sE)), 2))
Else
f_Min = CLng(sE)
End If
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-11-24
展开全部
Private Sub Command1_Click() '运行
Text2.Text = max(Val(Text1.Text))
Text3.Text = min(Val(Text1.Text))
End Sub
Private Sub Command2_Click() '清空
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Command3_Click() '结束
End
End Sub
Function max(n As Long)
Dim a() As Integer
Dim s As String
s = Trim(Str(n))
ReDim a(0 To Len(s))
For i = 1 To UBound(a)
a(i) = Val(Mid(s, i, 1))
Next i
For i = 1 To UBound(a) - 1
For j = UBound(a) To i + 1 Step -1
If a(i) < a(j) Then b = a(i): a(i) = a(j): a(j) = b
Next j
Next i
s = ""
For i = 1 To UBound(a)
s = s & a(i)
Next i
max = s
End Function
Function min(n As Long)
Dim a() As Integer, m As Integer
Dim s As String
s = Trim(Str(n))
ReDim a(0 To Len(s))
For i = 1 To UBound(a)
a(i) = Val(Mid(s, i, 1))
Next i
For i = 1 To UBound(a) - 1
For j = UBound(a) To i + 1 Step -1
If a(i) > a(j) Then b = a(i): a(i) = a(j): a(j) = b
Next j
Next i
m = 2
Do While a(1) = 0 And m <= Len(s)
a(1) = a(m): a(m) = 0
m = m + 1
Loop
s = ""
For i = 1 To UBound(a)
s = s & a(i)
Next i
min = s
End Function
Text2.Text = max(Val(Text1.Text))
Text3.Text = min(Val(Text1.Text))
End Sub
Private Sub Command2_Click() '清空
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Command3_Click() '结束
End
End Sub
Function max(n As Long)
Dim a() As Integer
Dim s As String
s = Trim(Str(n))
ReDim a(0 To Len(s))
For i = 1 To UBound(a)
a(i) = Val(Mid(s, i, 1))
Next i
For i = 1 To UBound(a) - 1
For j = UBound(a) To i + 1 Step -1
If a(i) < a(j) Then b = a(i): a(i) = a(j): a(j) = b
Next j
Next i
s = ""
For i = 1 To UBound(a)
s = s & a(i)
Next i
max = s
End Function
Function min(n As Long)
Dim a() As Integer, m As Integer
Dim s As String
s = Trim(Str(n))
ReDim a(0 To Len(s))
For i = 1 To UBound(a)
a(i) = Val(Mid(s, i, 1))
Next i
For i = 1 To UBound(a) - 1
For j = UBound(a) To i + 1 Step -1
If a(i) > a(j) Then b = a(i): a(i) = a(j): a(j) = b
Next j
Next i
m = 2
Do While a(1) = 0 And m <= Len(s)
a(1) = a(m): a(m) = 0
m = m + 1
Loop
s = ""
For i = 1 To UBound(a)
s = s & a(i)
Next i
min = s
End Function
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询