VB语言,编写一个需要使用Function过程调用的题目,求高手指点!

编写程序,输入任意一个不超过9位的正整数,求出由该正整数的全部数字组成的同样位数的一个最大整数和最小整数。题目要求使用Function的过程调用,主调过程应该不至于太复杂... 编写程序,输入任意一个不超过9位的正整数,求出由该正整数的全部数字组成的同样位数的一个最大整数和最小整数。题目要求使用Function的过程调用,主调过程应该不至于太复杂,思路大概只要把数字进行下排序就可以了,最大数好做,最小数注意0不能放在第一位,所有还得单独来处理0的问题,程序界面如图:
求高手指点代码!
展开
 我来答
爱栀竹的人
2011-11-24 · TA获得超过1352个赞
知道大有可为答主
回答量:1743
采纳率:0%
帮助的人:1001万
展开全部
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
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
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式