vb程式碼大全
(一)實驗目的
學會綜合運用選擇結構、迴圈結構、陣列和過程進行程式設計的方法。
(二)實驗內容
利用隨機函式產生80個10~99之間的隨機整數,將其中的偶數按由小到大的順序排列並輸出,將奇數按由大到小的順序排列並輸出;在產生的80個隨機整數中,找出其中的素數,並將這些素數求和。
(三)實驗要求
1.將隨機產生的80個整數存入陣列,按每行10個數輸出到窗體或文字框中。
2.偶數和奇數也分別存入陣列,將排好序的偶數和奇數分別輸出到窗體或兩個文字框中。
3.用Sub子過程或Function函式過程判斷隨機整數是否為素數。
4.將素數按每行10個數在窗體或文字框中輸出。
5.將素數的個數及求和結果輸出到窗體或文字框中。
6.介面由學生自行設計。
Option Explicit
Function fc(a) As Boolean
Dim i As Long
For i = 2 To Int(a / 2)
If a Mod i = 0 Then
fc = False
Exit For
Else
fc = True
End If
Next i
End Function
Private Sub Form_Click()
Dim i, j, k, m, s, g, f As Integer
j = 0
k = 0
f = 0
Dim a(1 To 80) As Integer
Dim b(1 To 80) As Integer
Dim c(1 To 80) As Integer
Dim d(1 To 80) As Integer
For i = 1 To 80
a(i) = Int(Rnd() * 100)
if a(i)<10 then a(i)=a(i)+10
If a(i) Mod 2 = 0 Then
j = j + 1
b(j) = a(i)
k = k + 1
c(k) = a(i)
For i = 1 To j
For m = i + 1 To j
If b(i) < b(m) Then
s = b(m)
b(m) = b(i)
b(i) = s
Next m
For i = 1 To k
For m = i + 1 To k
s = c(m)
c(m) = c(i)
c(i) = s
If fc(a(i)) = True Then
g = g + 1
d(g) = a(i)
Print "隨機數:"
f = f + 1
If f < 11 Then
Print " " & a(i);
f = 1
Print
Print "偶數:"
Print " " & b(i);
Print "奇數:"
Print " " & c(i);
Print "素數:"
For i = 1 To g
Print " " & d(i);
s = 0
Print "素數個數:" & g
s = s + d(i)
Print "素數總和:" & s
End Sub
vb程式碼大全
(一)實驗目的
學會綜合運用選擇結構、迴圈結構、陣列和過程進行程式設計的方法。
(二)實驗內容
利用隨機函式產生80個10~99之間的隨機整數,將其中的偶數按由小到大的順序排列並輸出,將奇數按由大到小的順序排列並輸出;在產生的80個隨機整數中,找出其中的素數,並將這些素數求和。
(三)實驗要求
1.將隨機產生的80個整數存入陣列,按每行10個數輸出到窗體或文字框中。
2.偶數和奇數也分別存入陣列,將排好序的偶數和奇數分別輸出到窗體或兩個文字框中。
3.用Sub子過程或Function函式過程判斷隨機整數是否為素數。
4.將素數按每行10個數在窗體或文字框中輸出。
5.將素數的個數及求和結果輸出到窗體或文字框中。
6.介面由學生自行設計。
Option Explicit
Function fc(a) As Boolean
Dim i As Long
For i = 2 To Int(a / 2)
If a Mod i = 0 Then
fc = False
Exit For
Else
fc = True
End If
Next i
End Function
Private Sub Form_Click()
Dim i, j, k, m, s, g, f As Integer
j = 0
k = 0
f = 0
Dim a(1 To 80) As Integer
Dim b(1 To 80) As Integer
Dim c(1 To 80) As Integer
Dim d(1 To 80) As Integer
For i = 1 To 80
a(i) = Int(Rnd() * 100)
if a(i)<10 then a(i)=a(i)+10
If a(i) Mod 2 = 0 Then
j = j + 1
b(j) = a(i)
Else
k = k + 1
c(k) = a(i)
End If
Next i
For i = 1 To j
For m = i + 1 To j
If b(i) < b(m) Then
s = b(m)
b(m) = b(i)
b(i) = s
End If
Next m
Next i
For i = 1 To k
For m = i + 1 To k
If b(i) < b(m) Then
s = c(m)
c(m) = c(i)
c(i) = s
End If
Next m
Next i
For i = 1 To 80
If fc(a(i)) = True Then
g = g + 1
d(g) = a(i)
End If
Next i
Print "隨機數:"
For i = 1 To 80
f = f + 1
If f < 11 Then
Print " " & a(i);
Else
f = 1
Print
Print " " & a(i);
End If
Next i
Print
Print "偶數:"
f = 0
For i = 1 To j
f = f + 1
If f < 11 Then
Print " " & b(i);
Else
f = 1
Print
Print " " & b(i);
End If
Next i
Print
Print "奇數:"
f = 0
For i = 1 To k
f = f + 1
If f < 11 Then
Print " " & c(i);
Else
f = 1
Print
Print " " & c(i);
End If
Next i
Print
Print "素數:"
f = 0
For i = 1 To g
f = f + 1
If f < 11 Then
Print " " & d(i);
Else
f = 1
Print
Print " " & d(i);
End If
Next i
s = 0
Print
Print "素數個數:" & g
For i = 1 To g
s = s + d(i)
Next i
Print "素數總和:" & s
End Sub