VB编程程序代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
10~100找数
Option Base 1
Dim a(10) As Integer
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 10
a(i) = Int(Rnd * 89) + 11
List1.AddItem a(i)
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer
For i = 1 To 10
If a(i) = Text1 Then Exit For
Next i
If i <= 10 Then
MsgBox Text1 & "出现在第" & i & "位"
Else
MsgBox "没有" & Text1
End If
End Sub
Private Sub Command3_Click()
List1.Clear
Text1 = ""
End Sub
Private Sub Command4_Click()
End
End Sub
10转2进制
Private Sub Command1_Click()
Dim n As Integer, st As String, p As String * 1
n = Val(Text1)
Do
p = n Mod 2
st = p & st
n = n \ 2
Loop Until n = 0
Text2 = st
End Sub
Private Sub Command2_Click() Text1 = ""
Text2 = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click() End
End Sub
选择排序
Option Base 1
Dim a(10) As Integer
Private Sub Command1_Click() Dim i As Integer
For i = 1 To 10
a(i) = Int(Rnd * 31) + 20
Text1 = Text1 & Str(a(i)) Next i
End Sub
Private Sub Command2_Click() Dim i As Integer, j As Integer
Dim t As Integer
For i = 1 To 9
For j = i + 1 To 10
If a(i) > a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
For i = 1 To 10
Text2 = Text2 & Str(a(i)) Next i
End Sub
Private Sub Command3_Click() Text1 = ""
Text2 = ""
Private Sub Command4_Click()
End
End Sub
求最大公约数
Private Sub Command1_Click()
Dim m As Long, n As Long, r As Long
m = Val(Text1.Text)
n = Val(Text2.Text)
If m < 1 Or n < 1 Then
Text3.Text = "数据错误!"
Else
Do
r = m Mod n
m = n
n = r
Loop Until n = 0
Text3.Text = CStr(m)
End If
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
冒泡排序
Option Base 1
Dim a(10) As Integer
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 10
a(i) = Int(Rnd * 31) + 20
Text1 = Text1 & Str(a(i))
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer
Dim t As Integer
For i = 1 To 9
For j = 1 To 10 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next j
Next i
For i = 1 To 10
Text2 = Text2 & Str(a(i))
Next i
End Sub
Private Sub Command3_Click()
Text1 = ""
Text2 = ""
End Sub
Private Sub Command4_Click()
End
End Sub
100以内素数
Private Sub Command1_Click()
Dim i As Integer, k As Integer
For i = 2 To 100
If prime(i) Then
Text1 = Text1 & Right(" " & i, 3)
k = k + 1
If k Mod 10 = 0 Then Text1 = Text1 & vbCrLf End If
Next i
End Sub