我想用VisualBasic寫出"因式分解"的程式
(要輸入一未知數然後做出該數的因式分解)
自己研究很久,可以還是寫不出來...ˊˋ毫無頭緒的說...
2007-03-25 14:20:42 · 3 個解答 · 發問者 Anonymous in 電腦與網際網路 ➔ 程式設計
提供VB6遞迴的版本
------------------------------------------
Private Sub Command1_Click()
Dim Num%
Num = Val(InputBox("請輸入一大於0的整數", , 792))
If Num >= 2 Then
MsgBox Ans(Num, 2, "")
Else
MsgBox "輸入值須大於等於2"
End If
End Sub
Public Function Ans$(N%, M%, S$)
If N Mod M Then
Ans = Ans(N, M + 1, S)
ElseIf N / M = 1 Then
Ans = S & M
Else
Ans = Ans(N / M, M, S & M & "*")
End If
End Function
2007-03-27 14:13:27 補充:
輸入訊息:"請輸入一大於0的整數"
更正為"大於等於2"
2007-03-27 09:46:41 · answer #1 · answered by 幽靈 5 · 0⤊ 0⤋
物件:
一個 text 名為 int_a (用來輸入)
一個 command button 名為 find (按此執行)
一個 text 名為 ans (答案輸出)
程式碼:
Option Explicit
Private Sub find_Click()
Dim x As Long, a As Long
If Not Fix(Val(int_a)) = Val(int_a) Or Val(int_a) < 1 Then
MsgBox "請輸入正整數!!", 16, "錯誤"
int_a = ""
int_a.SetFocus
Exit Sub
End If
a = Val(int_a)
ans = 1
For x = 2 To Fix(a ^ 0.5)
If Fix(a / x) = a / x Then ans = ans & ", " & x
Next x
For x = Fix(a ^ 0.5) To 1 Step -1
If Fix(a / x) = a / x Then ans = ans & ", " & a / x
Next x
int_a = ""
int_a.SetFocus
End Sub
應該沒錯啦...目前我還沒發現BUG
2007-03-25 20:55:09 · answer #2 · answered by 小孟 2 · 0⤊ 0⤋
'版本VB6.0
Private Sub Command1_Click()
Dim S$, N&
Do
S = InputBox("請輸入1個正整數", , 20)
If S = "" Then Exit Sub
Loop Until IsNumeric(S)
N = Abs(Int(S)): Cls
Print N; "= "; PCD(N)
End Sub
Function PCD$(ByVal N&)
Dim P&, S$
Do
P = IsPrime(N)
If P Then
S = S & P & "*"
N = N / P
Else
S = S & N
PCD = S
End If
Loop While P
End Function
Function IsPrime(N As Long) As Long
Dim I&, D&
If N > 2 Then
If N Mod 2 = 0 Then IsPrime = 2: Exit Function
For I = 3 To Int(Sqr(N)) Step 2
D = N Mod I
If D = 0 Then IsPrime = I: Exit Function
Next
End If
End Function
2007-03-25 20:22:07 · answer #3 · answered by W.J.S. 7 · 0⤊ 0⤋