第二部分 VB程序设计
一.填空题
21.在VB中可以作为容器的是
B
。
A.Form、TextBox、PictureBox
B.Form、PictureBox、Frame
C.Form、TextBoX、Label D.PictureBox、TextBox、ListBox
22.VB中除窗体能显示图片外,下面列出的控件中可以显示图片的控件有
C
。
1.PictureBox 2.Image 3.TextBox
4.CommandButton 5.OptionButton 6.Label
A.1、2、3、4、 B.1、2、5、6、
C.1、2、4、5
D.1、2、4、6、
23.针对语句If I=1 Then J=1,下列说法正确的是
C
。
A.I=1和J=1均为赋值语句
B.I=1和J=1均为关系表达式
C.I=1为关系表达式,J=1为赋值语句
D.I=1为赋值语句,J=1为关系表达式
24.在某过程中已说明变量a为Integer类型、变量s为String类型,过程中的以下四组语句中,不能正常执行的是
C
。
A.s=2*a+1 B.s="237"&".11":a=s
C.s=2*a>3
D.a=2:s=16400*a
25.下面所列四组数据中,全部是正确的VB常数是
A
。
A.32768,1.34D2,"ABCDE",&O1767
B.3276,123.56,1.2E-2,#True#
C.&HABCE,02-03-2002,False,D-3
D.ABCDE,#02-02-2002#,E-2
26.下面有关数组处理的叙述中,不正确的是
D
。
1、在过程中用ReDim语句定义的动态数组,其下标的上下界可为赋了值的变量。
2、在过程中,可以使用Dim、Private和Static语句定义数组。
3、用ReDim语句重新定义动态数组时,不得改变该数组的数据类型。
4、可用Public语句在窗体模块的通用说明处定义一个全局数组。
A.1、2、3、4、 B.1、3、4、 C.1、2、3、
D.2、4
、
27.第一次显示某窗体时,将引发一系列事件,正确的事件是
B
。
A.Load - Initialize - Activate
B.Initialize - Load - Activate
C.Load - Activate- Initialize D.Initialize - Activate - Load
28.定义两个过程Private Sub1(St() As String)和Private Sub2(Ch() As String*6),在调用过程中用Dim S(3) As String*6,A(3) As String定义了两个字符串数组。下面调用语句中正确的有
D
。
1、Call Sub1(S) 2、Call Sub(A) 3、Call Sub2(A) 4、Call Sub2(S)
A.1、2 B.1、3 C.2、3
D.2、4
29.运行下面程序,单击命令按钮Command1,则立即窗口上显示的结果是
A
。
Private Sub Command1_Click()
Dim A As Integer,B As Boolean,C As Integer,D As Integer
A=20/3 : B=True : C=B : D=A+C
Debug. Print A, D, A=A + C
End Sub
A. 7 6 False
B. 6.6 5.6 False
C. 7 6 A=6 D. 7 8 A=8
30.以下控件中,不能作为数据绑定控件的是
C
。
A. Label(标签) B. TextBox(文本框)
C. OptionButton(单选按钮)
D.ListBox(列表框)
二、填空题
(请将答案填写在答题纸的相应答题号内,每个答案只占一行)
1
、
正确表示命题“
A
是一个带小数的正数,且
B
是一个带小数的负数”的逻辑表达式是
Csng(A)>0 And Csng(B)<0
或者
A>O And A<>Int(A) And B<0 And B<>Int(B)
。
2
、
运行下面的程序,当单击窗体时,文本框
Text1
中的内容是
AFT
,文本框
Text2
中的内容是
AatF
。
Private Sub Form_Click()
Dim St As String
Dim St1 As String, St2 As String
St = "ASFRSDCFRSKX"
Call Change_String(St, St1, St2)
Text1.Text = St1
Text2.Text = St2
End Sub
Private Sub Change_String(S As String, St1 As String, St2 As String)
Dim tem As String, i As Integer
For i = 1 To Len(S)
tem = Mid(S, i, 1)
If tem = "S" Then
St1 = St1
St2 = St2 & "at"
ElseIf tem = "R" Then
St1 = St1 & "T"
Exit For
Else
St1 = St1 & tem
St2 = St2 & tem
End If
Next i
End Sub
3
、
执行下面程序,单击命令按钮
Command1
,则在窗体上显示的第一行是
5
,第二行是
45
,第三行是
54
。
Private Sub Command1_Click( )
Dim s_data As Integer, d_data As String, k As Integer, p As String
s_data = 29
Do Until s_data <= 5
p = s_data Mod 5
d_data = d_data & p
s_data = s_data \ 5
Loop
d_data = d_data & s_data
Print s_data
Print d_data
p = ""
For k = Len(d_data) To 1 Step -1
p = p & Mid(d_data, k, 1)
Next k
Print p
End Sub
4
、
执行下面程序,从键盘输入
3
,程序运行结束后,
A( 1, 1)
的值是
17
,
A( 2, 2)
的值是
15
;
A( 2, 3)
的值是
12
。
Private Sub Form_Click()
Dim I As Integer, J As Integer, K As Integer
Dim A() As Integer, N As Integer
N = InputBox("
输入
N")
ReDim A(N, N)
For I = 1 To N
For J = 1 To N
K = K + 1
A(I, J) = K + 10
Next J
Next I
Call Sub1(A, N)
End Sub
Private Sub Sub1(A() As Integer, N As Integer)
Dim I As Integer, J As Integer
Dim T As Integer, K As Integer
K = N + 1
For I = 1 To Int(N / 2)
For J = I To N - I
T = A(I, J)
A(I, J) = A(K - J, I)
A(K - J, I) = A(K - I, K - J)
A(K – I, N + 1 - J) = A(J, K - I)
A(J, K - I) = T
Next J
Next I
End Sub
5
、
执行下面程序,单击命令按钮
Command1
,则在窗体上第一行显示是
0 1 2 3 4
, 第二行显示是
4 5 6 7 8
,第三行显示是
8
。
Private Sub Command1_Click()
Output_Count 4
Output_Count 8
Output_Count 4
End Sub
Private Sub Output_Count(UpperLimit As Integer)
Static LowerLimit As Integer
Do While LowerLimit < UpperLimit
Print LowerLimit;
LowerLimit = LowerLimit + 1
Loop
Print LowerLimit
End Sub
6
、
运行下面程序,单击命令按钮
Command1
,则在窗体上显示的第一行内容是
VLAUSI
,第二行内容是
VILAUS
,文本框
Text1
中显示的内容是
VISUAL
。
Private Sub Command1_Click()
Dim S As String
S = "LAUSIV"
Text1.Text = Display(S, Len(S))
End Sub
Private Function Display(S As String, L As Integer) As String
If L > 1 Then Display = Display(S, L - 1)
S = Left(S, L - 1) & Right(S, 1) & Mid(S, L, Len(S) - L)
Display = S
Print Display
End Function
7
、
由键盘输入一个正整数,找出大于或等于该数的第一个素数。
Private Sub Form_Click()
Dim p As Integer , x As Integer, flag As Boolean
flag = False
x = InputBox("
请输入一个正整数
")
Do While Not flag
p = 2: flag= True
Do While flag And (p <= (x / 2))
If x Mod p = 0 Then
flag = False
Else
p = p + 1
End If
Loop
If Not flag Then x = x + 1
Loop
Print x
End Sub
8
、
下面的程序可从一个由字母与数字相混的字符串中选出数字串,并把数字串构成的数写入一个名为
List1
的列表框中(图
1
是程序执行的画面)。
Option Explicit
Private Sub Cmd1_Click()
Dim s As String, k As Integer, c() As String
Dim p As String, i As Integer
s = Text1.Text
k = 1
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
p= p & Mid(s, i, 1)
ElseIf Mid(s, i + 1, 1) >= "0" And Mid(s, i + 1, 1) <= "9" And i <> 1 Then
If p <> "" Then
ReDim Preserve c(k)
c(k) = p
k = k + 1
p = ""
End If
End If
Next i
ReDim Preserve c(k)
c(k) = p
For i = 1 To k
List1.AddItem c(i)
Next i
End Sub
9
、
给定一个十进制正整数,找出小于它并与其互质的所有正整数(所谓互质数是指最大公约数为
1
的两个正整数,图
2
是程序执行画面)。
Option Explicit
Private Function gcd( ByVal m As Integer, ByVal n As Integer ) As Integer
Dim r As Integer
r = m Mod n
If r = 0 Then
gcd = n
Else
m = n: n = r
gcd = gcd(m, n)
End If
End Function
Private Sub Command1_Click()
Dim n As Integer, p As Integer
n = Val(Text1)
For p = n - 1 To 2 Step -1
If gcd(n, p) = 1 Then List1.AddItem p
Next p
End Sub
10.用σ(m)表示数m的所有因子(包括1和m在内)之和。如果m<n,且σ(m)=σ(n)=m+n+1,则数对(m,n)是拟互满数。下列程序的功能是找出40——2000之间的所有拟互满数,并将运算结果按图3给出的格式显示在文本框Text1中。
Option Explicit
Private Sub Command1_Click()
Dim N As Integer, I As Integer, j As Integer
Dim Flg As Boolean, M As Integer
For N = 40 To 2000
Flg = False
Call Garcia(N, M, Flg)
If Flg Then
Text1.Text = Text1.Text & "(" & M & "," & N & ")" & Chr(13) & Chr(10)
End If
Next N
End Sub
Private Sub Garcia(N As Integer, M As Integer, Bool As Boolean)
Dim G1 As Integer, G2 As Integer, K As Integer
G1 = Factor(N)
For K = 4 To N - 1
G2 = Factor(K)
If
G1 = G2 And G2 = N + K + 1
Then
Bool = True
M =
K
Exit For
End If
Next K
End Sub
Private Function Factor(N As Integer) As Long
Dim I As Integer
For I = 1 To N
If N Mod I = 0 Then
Factor = Factor + I
End If
Next I
End Function