在Visual Basic(VB)考试中,可能会经常考到的程序包括以下几种:
判定素数过程
```vb
Private Function prime(n As Integer) As Boolean
Dim i As Integer
prime = False
For i = 2 To Sqr(n)
If n Mod i = 0 Then Exit Function
Next i
prime = True
End Function
```
求最大公约数过程
```vb
Private Function gcd(ByVal m As Integer, ByVal n As Integer) As Integer
Dim t As Integer, r As Integer
If m < n Then t = m
m = n
n = t
r = m Mod n
Do While r <> 0
m = n
n = r
r = m Mod n
Loop
gcd = n
End Function
```
冒泡排序过程
```vb
Private Sub sort(a() As Integer)
Dim i As Integer, j As Integer, t As Integer
For i = 1 To UBound(a) - 1
For j = 1 To UBound(a) - 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
End Sub
```
求100以内的素数
```vb
Private Sub Form_Click()
Dim i%, j%
For i = 2 To 100
For j = 2 To i - 1
If i Mod j = 0 Then Exit For
Next j
If j = i Then
Print i
End If
Next i
End Sub
```
从键盘输入任意长度的字符串,要求将字符顺序倒置
```vb
Private Sub Command1_Click()
Dim a$, i%, c$, d$, n%
a = InputBox$("输入字符串")
n = Len(a)
For i = 1 To n \ 2
c = Mid(a, i, 1)
Mid(a, i, 1) = Mid(a, n - i + 1, 1)
Mid(a, n - i + 1, 1) = c
Next i
Print a
End Sub
```
计算0~200之间所有能被11或5整除的数之和
```vb
Private Sub Form_Click()
Dim n%, i%
n = 0
For i = 1 To 200
If i Mod 11 = 0 Or i Mod 5 = 0 Then
n = n + i
End If
Next i
Print n
End Sub
```
输入一年份,判断它是否为闰年,并显示有关信息
```vb
Private Sub Command1_Click()
Dim year As Integer
year = Val(InputBox$("输入年份"))
If (year Mod 4 = 0 And year Mod 100 <> 0) Or (year Mod 400 = 0) Then
MsgBox year & "是闰年"
Else
MsgBox year & "不是闰年"
End If
End Sub
```
生成斐波那契数列的前20项
```vb
Dim a As Long, b As Long, c As Long
Dim i As Integer
a = 0
b = 1
For i = 3 To 20
c = a + b
MsgBox c
a = b
b = c
Next i
```