请教一个VB编程

来源:百度知道 编辑:UC知道 时间:2024/06/18 03:41:28
题目是:输入一个正整数,求出它的所有质因子,并按乘积的形式打印。例如:28=2*2*7;20=2*2*5 ...

希望得到您的帮助,谢谢!

VB分解质因数

Private Sub Command1_Click()
Dim i As Long
Dim n As Long
Dim s As String

s = InputBox("输入一个整数", "输入")
n = CLng(s)

Print CStr(n); "=";
i = 2
While i < n
If n Mod i <> 0 Then
i = i + 1
Else
Print CStr(i); "*";
n = n \ i
i = 2
End If
Wend
Print CStr(i)

End Sub

在窗体上添加一个Command按钮Command1, 一个Text文本框Text1. 然后添加如下代码

Option Explicit

Private Sub Command1_Click()
Dim i As Long, Num As Long, Factor() As Long, Exp As String
If IsNumeric(Text1.Text) Then
Num = Val(Text1.Text)
ReDim Factor(0)
If Num > 1 Then
i = 1
Do While i <= Num
If IsPrime(i) Then
Do While Num Mod i = 0
ReDim Preserve Factor(UBound(Fact