1、Visual Basic 程序设计参考答案第 1 章 程序设计及 VB 概述 一、选择题 1、C 2、A 3、A 4、C 5、B 6、C 7、D 8、A 9、D 10、C 二、填空题 1、 对象 Windows(不确定) 事件驱动 2、窗体 工程 3、查看对象 查看代码 4、CmdExam Click lbldisp 5、 “启动”按钮 “运行”菜单“启动”命令 F5 6、Click 事件过程 7、Cancel 8、Label1第 2 章 Visual Basic 程序设计基础 一、选择题 1、A 2、C 3、D 4、B 5、C 6、A 7、B 8、A 9、B 10、D 二、写出下列表达式的值
2、 1、 6 2、 0 3、 8 4、False 5、False 6、 True 7、 1 8、16.7 9、“C“ 10、“Basic“ 11、“VISUAL “ 12、 5 三、将代数式改写成 VB 表达式 1、(a + b) / (c * d - Sqr(1 - a * a) + 5 (1 / 3) * Cos(3.14159 / 3) 2 2、Sin(45 * 3.14159 / 180) + Abs(x - y) 3、2 * Exp(x) + Log(x * y) 4、 (Log(50) / Log(10) - Log(20) / (9 * Exp(3) 5、Abs(x) = Y An
3、d Abs(x) 0) Or (N Mod 400 = 0)第 3 章 程序的基本控制结构 一、选择题 1、 C D 2、B 3、B 4、B 5、C 6、A 7、D 8、BD 9、C 10、D 11、A 12、A 13、D 14、A 15、A 16、D 二、填空题 1、 15 15 2、 x= 3 y=10 3、 1 2 3 4 5 6 7 8 9 0 * * * * * * * * * * * * * * * 4、 12345 54321 5、 ABCDE 6、 32 7、 k If x 0 8、窗体、图片框、框架 9、MultiLine ScrollBars 10、AutoSize Str
4、etch 11、Image1.Picture = LoadPicture(“c:pic1.jpg“) 12、Change 13、另存为 14、顶级 15、 ListIndex Text 或 List(List1.ListIndex) List(2) x , Lst1.ListIndex +1 ListCount 1 Selected RemoveItem i第 6 章 过程 一、选择题 1、C 2、C 3、C 4、A 5、D 二、填空题 1、内存空间 或者:地址 2、传值 传址 3、Sub TestAB(a As Single, b!, c As Boolean) 4、过程名 5、局部变量 6
5、、Static 7、本过程 模块 整个工程 8、模块级 工程级 9、过程调用自身 递归结束 递归描述 10、 6 8 4 6 4 3 11、 0.6 1.8 12、 2 5 8 11 13、 3 1 3 0 1 3 4 1 3 0 1 3 1 1 3第 7 章 鼠标和键盘 填空题 1、 11 2、 MouseDown MouseUp MouseMove 3、 Shift Ctrl Alt 4、 MouseDown MouseUp MouseUp 5、 A a 6、 Ctrl + Alt + 单击鼠标右键 7、 Form-66 Text-66第 8 章 文件 一、选择题 1、C 2、B 3、A
6、4、D 5、D 二、填空题 1、程序文件和数据文件 顺序文件和随机文件 ASCII 文件和二进制文件 2、顺序 随机 二进制 3、 Output Append Input Random Binary Random 4、打开(建立) 读/ 写 关闭 5、Input# LineInput# Input() Get Put 6、Input As #10 Output As #20 7、Dir1.Path Drive1_Change()第 9 章 多窗体和多文档界面 一、选择题 1、C 2、C 3、D 4、C 5、C 二、填空题 1、Load UnLoad 2、Show Hide 3、无效 4、图片框
7、 5、图片框、计时器 第 10 章 文本和图形 选择题 1、A 2、C 3、A 4、B 5、C第 3 章 编程题参考答案 1、Private Sub Form_Click() Dim a%, b%, c% Dim d!, x1!, x2% a = InputBox(“a=“) b = InputBox(“b=“) c = InputBox(“c=“) d = b * b - 4 * a * c If d 0 And n Mod 3 0 Then s1 = s1 + x: n1 = n1 + 1 If x = i If x Mod i = 0 Then If x = i Then Print i
8、 Else Print i; “*“; x = x i Else i = i + 1 End If Loop End Sub5、Private Sub Form_Click() Dim x%, i%, n% For i = 1 To 20 x = Int(100 * Rnd) If x Mod 2 = 0 Then Print x; n = n + 1 End If Next Print Print “偶数个数为:“; n End Sub6、Private Sub Form_Click() Dim x!, y! x = InputBox(“x=“) If x 200 Print “sum=“;
9、 Sum 9、Private Sub Form_Click() For m = 100 To 999 tem = m gw = tem Mod 10 tem = tem 10 sw = tem Mod 10 bw = tem 10 If (gw + sw) Mod 10 = bw Then 满足第一个条件 flag = True 判断其是否为素数 For i = 2 To Sqr(m) If m Mod i = 0 Then flag = False: Exit For Next i If flag Then Print m; n = n + 1 按每行 5 个输出 If n Mod 5 =
10、0 Then Print End If End If Next m End Sub第 4 章 编程题参考答案 1. Option Base 1 Private Sub Form_Click() Dim A, B Dim C(10) As Integer A = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) B = Array(2, 4, 6, 8, 10, 1, 3, 5, 7, 9) For i = 1 To 10 C(i) = A(i) * B(i) Print C(i); Next i End Sub 2. Option Base 1 Private Sub
11、Form_Click() Dim a(10) As Integer For i = 1 To 10 a(i) = InputBox(“输入整数“) Print a(i); Next Print For i = 1 To 10 2 temp = a(i) a(i) = a(11 - i) a(11 - i) = temp Next i For i = 1 To 10 Print a(i); Next End Sub 3. Option Base 1 Dim a() As Integer Dim n As Integer Private Sub Form_Click() Randomize 10:
12、 n = InputBox(“n=“) If n a(i) Then temp = a(i): a(i) = a(j): a(j) = temp End If Next j Next i For i = 1 To n Print a(i); Next Print End Sub 4. Private Sub Command1_Click() Dim x As Integer, y As Integer ReDim Preserve a(n + 1) 插入操作 排序 x = InputBox(“输入要插入的数“) For k = 1 To n If x a(k) Then Exit For Ne
13、xt k For i = n To k Step -1 a(i + 1) = a(i) Next i a(k) = x For i = 1 To n + 1 Print a(i); Next i Print y = InputBox(“输入要删除的数“) For k = 1 To n + 1 If y = a(k) Then Exit For Next For i = k + 1 To n + 1 a(i - 1) = a(i) Next For i = 1 To n Print a(i); Next Print End Sub 删除操作5. Option Base 1 Private Sub
14、 Form_Click() 第(1) 小题 Dim a(5, 5) As Integer Dim Max As Integer, timax As Integer, tjmax As Integer For i = 1 To 5 For j = 1 To 5 a(i, j) = Int(90 * Rnd + 10) Print a(i, j); Next j Print Next i 第(2)小题 Max = a(1, 1): timax = 1: tjmax = 1 For i = 1 To 5 For j = 1 To 5 If a(i, j) Max Then Max = a(i, j)
15、 timax = i tjmax = j End If Next j Next i Print “最大元素为:“; Max; “其位置在第“; timax; “行“; tjmax; “列“ 第(3) 小题 s = 0 For i = 1 To 5 For j = 1 To i Print a(i, j); s = s + a(i, j) Next j Print Next i Print “下三角元素和为“; s 第(4) 小题 For i = 1 To 5 temp = a(i, 2): a(i, 2) = a(i, 4): a(i, 4) = temp Next i For i = 1 T
16、o 5 For j = 1 To 5 Print a(i, j); Next j Print Next i End Sub 6. Option Base 1 Private Sub Form_Click() Dim a() As Integer n = Val(InputBox(“n=“) ReDim a(n, n) For i = 1 To n a(i, i) = 1: a(i, 1) = 1 Next For i = 3 To n For j = 2 To i - 1 a(i, j) = a(i - 1, j - 1) + a(i - 1, j) Next j Next i For i =
17、 1 To n For j = 1 To i Print a(i, j); Next j Print Next i End Sub第 8 章 编程题参考答案 1、Option Base 1 Private Sub Form_Click() Dim a(5, 5) As Integer Open “d:a1.txt“ For Output As #1 For i = 1 To 5 For j = 1 To 5 a(i, j) = Int(90 * Rnd + 10) Print #1, a(i, j); Next j Print #1, Next i Max = a(1, 1) For i =
18、1 To 5 For j = 1 To 5 If a(i, j) Max Then Max = a(i, j) End If Next j Next i Print #1, “Max=“; Max Close #1 End Sub2、Private Sub Command1_Click() Open “d:file1.txt“ For Input As #1 Do While Not EOF(1) Line Input #1, str1 Text1 = Text1 + str1 + Chr(13) + Chr(10) Loop Close #1 End Sub Private Sub Comm
19、and2_Click() 修改文本框内容后保存文件 读出文件内容并在文本框中显示 Open “d:file2.txt“ For Output As #2 Print #2, Text1 Close #2 End Sub 3、标准模块: Type TelBook Num As String * 5 Name As String * 4 TelNum As String * 8 Addr As String * 30 End Type 窗体模块: Dim Tel As TelBook Private Sub cmdSearch_Click() Open “d:Telbook.txt“ For Random As #1 Len = Len(Tel) i = 1 flag = 0 Do While Not EOF(1) Get #1, i, Tel If Tel.Num = Trim(txtNum.Text) Then txtName.Text = Tel.Name txtTelNum.Text = Tel.TelNum txtAddr.Text = Tel.Addr flag = 1 Exit Do End If i = i + 1 Loop Close #1 If flag = 0 Then MsgBox “没有找到所输入的编号!“ End If End Sub