排序问题,求教!
Option ExplicitOption Base 1
Dim a%(80), b%(80), c%(80), d%(80)
Dim j%, k%, l%
Private Sub Command1_Click()
Dim i As Integer
Dim sum As Integer
Text1.Text = "随机序列" & vbCrLf
Text2.Text = "偶数" & vbCrLf
Text3.Text = "奇数" & vbCrLf
Text4.Text = "素数" & vbCrLf
Text5.Text = "素数求和" & vbCrLf
j = 1
k = 1
l = 1
Randomize
For i = 1 To 80
a(i) = Int(Rnd * 90) + 10
If a(i) Mod 2 = 0 Then
b(j) = a(i)
j = j + 1
Else
c(k) = a(i)
k = k + 1
End If
Text1.Text = Text1.Text & a(i) & " "
If i Mod 10 = 0 Then
Text1.Text = Text1.Text & vbCrLf
End If
If IsPrimeNum(a(i)) = True Then
d(l) = a(i)
l = l + 1
sum = sum + a(i)
End If
Next i
Sort b, j - 2
Sort c, k - 2
For i = 1 To j - 1
Text2.Text = Text2.Text & b(i) & " "
If i Mod 10 = 0 Then
Text2.Text = Text2.Text & vbCrLf
End If
Next i
For i = 1 To k - 1
Text3.Text = Text3.Text & c(i) & " "
If i Mod 10 = 0 Then
Text3.Text = Text3.Text & vbCrLf
End If
Next i
For i = 1 To l - 1
Text4.Text = Text4.Text & d(i) & " "
If i Mod 10 = 0 Then
Text4.Text = Text4.Text & vbCrLf
End If
Next
Text5.Text = "素数求和:" & vbCrLf & sum
Text6.Text = "素数个数:" & vbCrLf & l
End Sub
Private Function IsPrimeNum(ByVal V As Integer) As Boolean
Dim i As Integer
IsPrimeNum = True
If V < 2 Then IsPrimeNum = False: Exit Function
For i = 3 To V - 1
If V Mod i = 0 Then
IsPrimeNum = False
Exit Function
End If
Next i
End Function
题目如下:
用随机函数产生80个10~99之间的随机整数,将其中的奇数按由小到大的顺序排列并输出,偶数从小到大输出;在产生的80个随机整数中,找出其中的素数,输出素数个数及素数的和。
(三)实验要求
1.将随机产生的80个整数存入数组,按每行10个数输出到窗体或文本框中。
2.奇数,偶数也各存入一个数组,将排好序的奇数和偶数输出到窗体或文本框中。
3.用Sub子过程或Function函数过程判断随机整数是否为素数。
4.将素数按每行10个数在窗体或文本框中输出。
程序如下:
Private Sub Sort(dat() As Integer, Dl As Integer)
Dim x As Integer
Dim y As Integer
Dim t As Integer
For x = LBound(dat) To LBound(dat) + Dl
For y = x + 1 To LBound(dat) + Dl
If dat(x) < dat(y) Then
t = dat(x)
dat(x) = dat(y)
dat(y) = t
End If
Next y
Next x
End Sub
想让Text2中的数字按从小到大排列,用什么方法都可以,谢谢帮忙!