Private Sub Command1_Click()
Dim a%(), n%, i%
n = Val(InputBox(\"输入n的值\", \"输入n值应在20至100之间,含20和100\"))
ReDim a(n) As Integer
Print \"n\" & \"个随机整数\"
For i = 1 To n
a(i) = Int(90 * Rnd + 10)
Print a(i);
If i Mod 10 = 0 Then Print
Next i
End Sub
Private Sub Command2_Click()
Dim a%(), b%(), c%(), n%, m%, k%, l%, i%
For i = 1 To n
If a(i) Mod 2 <> 0 Then
k = k + 1
ReDim Preserve b(i)
b(k) = a(i)
End If
Next i
Call bsort(b, k)
Print
Print k & \"个奇数\"
For i = 1 To k
Picture1.Print b(i);
If i Mod 10 = 0 Then Picture1.Print
Next i
For i = 1 To n
If a(i) Mod 2 = 0 Then
m = m + 1
ReDim Preserve c(m)
c(m) = a(i)
End If
Next i
Call csort(c, m)
Print
Print m & \"个偶数\"
For i = 1 To m
Label1.Caption = Label1.Caption & c(i) & Space(2)
If i Mod 10 = 0 Then
Print
Label1.Caption = Label1.Caption & vbCrLf
End If
Next i
End Sub
Sub bsort(b() As Integer, l As Integer)
Dim a%(), n%, i%, j%, t%
For i = 1 To l - 1
For j = i + 1 To l
If b(i) > b(j) Then
t = b(i)
b(i) = b(j)
b(j) = t
End If
Next j
Next i
End Sub
Sub csort(c() As Integer, m As Integer)
Dim a%(), n%, i%, q%, t%
For i = 1 To m - 1
For q = 1 To m - i
If c(q) < c(q + 1) Then
t = c(q)
c(q) = c(q + 1)
c(q + 1) = t
End If
Next q
Next i
End Sub
Private Function isprime(o As Integer) As Boolean
Dim a%(), n%
Dim y As Integer
If o <= 1 Then isprime = False: Exit Function
isprime = True
For y = 2 To Sqr(o)
If o Mod y = 0 Then isprime = False: Exit Function
Next y
End Function
Private Sub Command3_Click()
Dim a%(), d%(), n%, i%, z%, p%
For i = 1 To n
If isprime(a(i)) Then
z = z + 1
ReDim Preserve d(z)
d(z) = a(i)
End If
Next i
For i = 1 To z
Text1.Text = Text1.Text & d(i) & Space(2)
If i Mod 10 = 0 Then
Print
Text1.Text = Text1.Text & vbCrLf
End If
Next i
For i = 1 To z
p = 0
Do While i <= z
p = p + d(i)
i = i + 1
Loop
Print
Print z & \"个素数\"
Text1.Text = Text1.Text & vbCrLf
Text1.Text = Text1.Text & \"素数的和是: \" & p
Next i
End Sub
Private Sub Command4_Click()
End
End Sub
|
|
|
|
评论
直达楼层