水溶c100尺寸:VF分解质因数

来源:百度文库 编辑:神马品牌网 时间:2024/05/05 19:55:19
在VF中分解质因数
是在VFP里的程序段喔````不要其他的

判断是否为质数的函数:
Function IsPrime(X As Long) As Boolean
Dim Y As Long
IsPrime = True
For Y = 2 To Sqr(X)
If X / Y = X \ Y Then
IsPrime = False
Y = X + 1
End If
Next Y

End Function

基本功能的实现:

Dim Num1 As Long
Dim Num2 As Long
Dim Num3 As Long
Dim Hms As Long

Private Sub Command1_Click()
Timer1.Enabled = True
Command1.Enabled = False
Text2.Text = ""
'If Val(Text1.Text) > 1000000 Then
' MsgBox "数值太大", vbCritical, "提示"
' Command1.Enabled = True
' Exit Sub
'End If
If Len(Trim(Text1.Text)) < 1 Then
MsgBox "数值不能空", 16, "数据错误"
Text1.SetFocus
Command1.Enabled = True
Exit Sub
End If
If Len(Text1.Text) < 1 Or Int(Val(Text1.Text)) <> Val(Text1.Text) Or Val(Text1.Text) < 2 Then
MsgBox "数值必须为不小于2的正整数", 16, "数据错误"
Text1.Text = ""
Text1.SetFocus
Command1.Enabled = True
Exit Sub
End If
Label1.Visible = True
Num1 = Text1.Text

If IsPrime(Num1) = True Then
Timer1.Enabled = False
If Hms < 0 Then Hms = 0
If Hms = 0 Then
Label2.Caption = "一共用了0秒"
Else
Label2.Caption = "一共用了0" & Hms / 1000 & "秒"
End If
Hms = 0
Text2.Text = Num1 & "是素数"

Command1.Enabled = True
Label1.Visible = False
Exit Sub
End If
Text2.Visible = False
For Num2 = 2 To Num1 - 1
If IsPrime(Num2) = True Then
Do While Num1 / Num2 = Num1 \ Num2
DoEvents
If Len(Trim(Text2.Text)) < 1 Then
Text2.Text = Text1.Text & "=" & Num2
Else
Text2.Text = Text2.Text & "*" & Num2
End If
Num1 = Num1 / Num2

Loop
End If

Next Num2

End Sub

Private Sub Text1_Change()

Text2.Text = ""
Label1.Visible = False
Label2.Caption = ""

End Sub

接收键盘内容:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 And Len(Text1.Text) > 1 Then

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End If
If KeyAscii = 13 Then Command1_Click
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
End If
End Sub

input to x
for i=1 to x-1
if x%i=0
??i
i=1
x=int(x/i)
endif
next