谁告诉我 把几个数编成 杨辉三角 怎么做,我刚学vb,不懂。
我做了一个,完全满足要求:
1、在form1中:
Option Explicit
Private Sub Command1_Click()
Dim Z As New Class1
Dim x As Variant
If Text1(0) = "" Then Exit Sub
Screen.MousePointer = 11
Text1(1).Text = Z.PrimeNumber(CLng(Text1(0)))
With List1
.Clear
For Each x In Z.PNCollection
.AddItem x
Next
End With
Label1(2).Caption = "质数集合(" & Z.PNCollection.Count & "个):"
Screen.MousePointer = 0
End Sub
2、在class1中:
Option Explicit
Public PNCollection As New Collection ''''质数集合
Private CalcCollection As New Collection ''''结果集合
''''''''判断是否为质数'''''''''
Private Function IfPrimeNumber(ByVal Num As Long) As Boolean
Dim i As Long
Dim L As Long
IfPrimeNumber = True
If Num >= 2 Then
L = Sqr(Num)
For i = 2 To L
If Num Mod i = 0 Then
IfPrimeNumber = False
Exit For
End If
Next
End If
End Function
'''''''将质数添加进集合中''''''''
Private Sub Add(ByVal Num As Long)
Dim i As Long
For i = 2 To Num
If IfPrimeNumber(i) = True Then
PNCollection.Add i
End If
Next
End Sub
'''''''处理自然数''''''
Public Function PrimeNumber(ByVal Num As Long) As String
Add Num
PrimeNumber = Trim(Num)
End Function
''''''''处理质数集合'''''''
Private Function Trim(ByVal Num As Long) As String
Dim x As Variant
Dim Value As Long
Dim Value1 As Double
Dim Value2 As Long
Dim Msg As String
Value2 = Num
For Each x In PNCollection
10:
Value = Fix(Num / x) '''(1)取出一个质数,进行短除运算,得到商
Value1 = Num Mod x '''(2)取出余数
'''(3)判断该商是否为和数,余数是否同时为0
If IfPrimeNumber(Value) = False And Value1 = 0 Then
'''''(4)如果为合数,将质数x添加到CalcCollection中,
''''''同时,将商继续短除
CalcCollection.Add x
Num = Value
GoTo 10
ElseIf IfPrimeNumber(Value) = True And Value1 = 0 Then
'''(5)如果满足,这该数为质数,且满足x*value=num,此时添加到CalcCollection中
CalcCollection.Add x
CalcCollection.Add Value
Exit For
End If
Num = Value2
Next
Num = 1
For Each x In CalcCollection
If Msg = "" Then
Msg = x
Num = x
Else
Msg = Msg & " * " & x
Num = Num * x
If Num = Value2 Then Exit For
End If
Next
Msg = Msg & " = " & Value2
Trim = Msg
End Function
大家根据代码新建相关控件!
[此贴子已经被作者于2007-4-28 20:01:34编辑过]
[此贴子已经被作者于2007-4-28 21:55:20编辑过]