'因數分解 - 副程式、函數、字串應用綜合演練版
Public Class Form1
Dim bqn
Dim a(20)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str2 = ""
' Dim qn = 720
For qn = 1 To 50
bqn = qn
factorize(qn)
str2 = str2 & toexpType(a) & vbNewLine
Next
MsgBox(str2)
End
End Sub
Sub factorize(ByVal qn)
ReDim a(20)
Dim j = 0
For k = 2 To qn
While qn Mod k = 0
qn = qn / k
a(j) = k
j = j + 1
End While
Next
j = j - 1
ReDim Preserve a(j)
End Sub
Function toexpType(ByVal a) As String
Dim str1 = ""
Dim p = 0
Dim n = 1
For i = 0 To a.Length - 1
Dim c = a(i)
If c <> p Then
If str1 = "" Then
str1 = str1 & c & "^"
Else
str1 = str1 & n & "*" & c & "^"
End If
n = 1
Else
n = n + 1
End If
p = c
Next
str1 = str1 & n
str1 = Replace(str1, "^1", "")
str1 = bqn & "=" & str1
Return str1
End Function
End Class
沒有留言:
張貼留言