VB考试复习题 下载本文

方法二:

Dim a%, m%, b%, c%, d% Private Sub Form_Click() a = Int(Rnd * 900 + 100) Print a

d = (a Mod 10) * 100

b = (a Mod 100) - (a Mod 10) c = a \\ 100 m = c + b + d Print m End Sub

16、从键盘输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的“ABCDEFG”变换成“GFEDCBA”。 Private Sub Form_Click() Dim i, a, b, n As String

n = InputBox(\输入字符\i = Len(n)

For a = 1 Toi / 2 b = Mid(n, a, 1)

Mid(n, a, 1) = Mid(n, i + 1 - a, 1) Mid(n, i + 1 - a, 1) = b Next a Print n End Sub

17、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,输出该矩阵所有数据之和

Private Sub Form_Click() Dim a(3, 3), sum% For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 31 + 20) Next j Next i

For i = 0 To 3 For j = 0 To 3 Print a(i, j); Next j Print Next i

For i = 0 To 3 For j = 0 To 3 sum = sum + a(i, j)

Next j Next i

Print \End Sub

18.编一子过程Procmin(a(),amin),求一维数组中a的最小值amin.

(随机产生10个200-400之间的数,并显示数组各元素;调用子函数,显示最小值) Private Sub Form_Click() Dim a(1 To 10), amin, i% For i = 1 To 10

a(i) = Int(Rnd * 200 + 200) Print a(i); Next i

Call s(a(), amin) Print

Print \End Sub

Sub s(b(), min) Dim i%

min = b(LBound(b))

For i = LBound(b) + 1 To UBound(b) If b(i) < min Then min = b(i) Next i End Sub

19,20.编一个选择排序法子过程,对已知的若干整数按递增次序排列。 提示:子过程的形式为sub sort1(a%())。 Private Sub Command1_Click() Dim b%(10), i% Print \排序前\For i = 1 To 10

b(i) = Int(Rnd * 100) Print b(i); Next i Print

Print \排序后\Call sort(b()) End Sub

Sub sort(a%())

Dim i%, m%, n%, min%, t% n = UBound(a) For i = 0 To n - 1 min = i

For j = i + 1 To n

If a(j) < a(min) Then min = j Next j

m = a(i): a(i) = a(min): a(min) = m Next i

For i = LBound(a) To UBound(a) Print a(i); Next i End Sub

递减次序排列

Private Sub Command1_Click() Dim b%(10), i%

Print \排序前\For i = 0 To 9

b(i) = Int(Rnd * 100) Print b(i); Next i Print

Print \排序后\Call sort(b()) End Sub

Sub sort(a%())

Dim i%, m%, n%, min%, j% n = UBound(a) For i = 0 To n - 1 For j = 0 To n - 1 - i

If a(j) < a(j + 1) Then m = a(j): a(j) = a(j + 1): a(j + 1) = m Next j Next i

For i = LBound(a) To UBound(a) - 1 Print a(i); Next i End Sub

21,22.已知有序数组a(),编程插入x(x的值为14)。数组a中的元素分别 为{12,6,4,89,75,63,100,20,31}。

Ip = Val(InputBox(\请输入数字x\For i = 0 To UBound(A) If Ip

ReDim Preserve A(UBound(A) + 1) t = A(i) A(i) = Ip

ins = i

Exit For End If Next i

For i = ins + 1 To UBound(A) - 1 n = A(i) A(i) = t t = n Next i

Text2.Text = \

For i = 0 To UBound(A) - 1

Text2.Text = Text2.Text & \

Next i End Sub

Private Sub Form_Load() ReDimA(9)

A(0) = 4: A(1) = 6: A(2) = 12: A(3) = 20 A(4) = 31: A(5) = 63: A(6) = 75: A(7) = 89 A(8) = 100 End Sub

23、声明一个整型的二维数组a(1 to 4,1 to 4),用随机函数产生各元素,范围介于[1,20]之间,编程将第2行和第4行对应元素交换。 Private Sub Form_Click() Dim a(0 To 3, 0 To 3), i%, j%, t For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 21 + 1) Print Tab(j * 5); a(i, j); Next j Next i Print Print

For j = 0 To 3

t = a(1, j): a(1, j) = a(3, j): a(3, j) = t Next j

For i = 0 To 3 For j = 0 To 3

Print Tab(j * 5); a(i, j); Next j

Next i End Sub 24、定义三个4×4的二维数组A,B,C,A和B中的元素均随机生成,数组A的范围是1~20,数组B的范围是100~200,数组C是A和B相加得到的。请编程生成并输出A,B,C。 Private Sub Command1_Click() Dim a(3, 3), b(3, 3), c(3, 3), t%, m% For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 20 + 1) Picture1.Print a(i, j); Next j Next i

Picture1.Print For i = 0 To 3 For j = 0 To 3

b(i, j) = Int(Rnd * 101 + 100) Picture1.Print b(i, j); Next j Next i

Picture1.Print For i = 0 To 3 For j = 0 To 3 c(i, j) = a(i, j) + b(i, j)

Picture1.Print c(i, j); Next j Next i 25、随机产生10个1~100的正整数放入数组,显示产生的数,求最大值、最小值、平均值。 Private Sub Form_Click()

Dim a(1 To 10) As Integer, i%, maxa%, mina%, suma% For i = 1 To 10

a(i) = Int(Rnd * 71 + 30) Next i

mina = a(1) maxa = a(1) suma = a(1) For i = 2 To 10

If a(i) >maxa Then maxa = a(i) If a(i) < mina Then mina = a(i) suma = suma + a(i) Next i

For i = 1 To 10 Print a(i);