vb作业集 - 2014 - 下载本文

End Sub

Private Sub Form_Click()

Dim s1 As String, s2 As String s1 = \ sub1 s1, s2 Print s2 End Sub

程序运行后,单击窗体,则窗体上显示的内容是 。 A.rehcaet B.teacher C.therace 10. 程序运行后,单击命令按钮,输出结果为 。

Private Sub Command1_Click() Call Cop (2) Call Cop (4) End Sub

Sub Cop (a As Integer) Static x As Integer x = x + a Print x; End Sub

D.certhea

A.2 4 B.2 6 C. 3 6 D.2 3

三、阅读题

1. 在窗体上画一个命令按钮,名称为Command1,然后编写如下程序。

Function Func(ByVal x As Integer, y As Integer) y = y * x

结果: If y > 0 Then Func = x Else

Func = y End If

End Function

Private Sub Command1_Click() Dim a As Integer, b As Integer a = 3 b = 4

c = Func(a, b) Print a;b;c End Sub

2. 依次写出下列程序运行时四次单击命令按钮Command1后窗体上的输出结束。

Dim x As Integer, y As Integer

29

VB网络课堂:info.zjfc.edu.cn/vbweb

Private Sub f1(a As Integer) a=a/2 End Sub

Private Sub f2(ByVal b As Integer) b=b/2 End Sub

Private Sub Command1_Click() Call f1(x) Call f2(y) Print x, y End Sub

Private Sub Form_Load() x= 64:y =64 End Sub

结果: 3. 写出运行时两次单击窗体后屏幕上的显示结果。

结果: Dim x As Byte

Private Static Sub Form_Click() Dim y As Byte, z As Byte Call Init(y, z) Call OP(x, y, z) Print x, y, z End Sub

Private Sub Init(a As Byte, b As Byte) a = a + 1: b = b + 2: x = a + b End Sub

Private Sub OP(ByVal u As Byte, v As Byte)

ByRef w As Byte)

u = u + 1: v = v + u: w = u + v + w End Sub

4. 在窗体中画一个名称为command1的命令按钮,并编写如下过程: Private Sub Command1_Click() Dim a as integer Static b as integer a=5

call func(a,b) print a,b End Sub

Private Sub func(byval x As Integer, y as integer) x=x+x y=y+1 End Sub

运行以上程序,二次单击command1得到的结果是

结果: 第一次单击后结果: 第二次单击后结果: 30 VB网络课堂:info.zjfc.edu.cn/vbweb

四、程序填空题

1.[程序说明] 有Summary过程是用于计算1!+2!+...+20!,并打印出计算结果,但不完整,请在横线上填入必要的内容,使其完整。nFactor函数过程用于计算n!。 程序如下:

Public Function nFactor(ByVal n As Integer) As Double Dim i As Integer Dim temp As Double (1)

For i = 1 To n

temp = temp * i Next i

nFactor = (2) End Function

Public Sub summary() Dim sum As Double Dim i As Integer Dim n As Integer n = 20

For i = 1 To n

sum = sum + (3) Next i

Form1.Print \ (4) End Sub

2.【程序说明】编程序求表达式 s=1+x+x/2!+x/3!+?+x/n!+?的值,直至末项小于10-5

为止,并在窗体上输出。

Private Function f( (5) ) As Single Dim i As Integer f = 1

For i = 1 To m f = f * i Next i

End Function

Private Sub Command1_Click() Dim i As Integer, s As Single x = val(InputBox(\输入x\ i = 1: t = x

(6) Do until t < 10 ^ (-5) t= (7) (8) i=i+1 loop Pirnt s End Sub

2

3

n

31 VB网络课堂:info.zjfc.edu.cn/vbweb

(5) A、m As Single B、n As Single

C、m As Integer D、n As Integer

(6)A、s=1 B、s=0 C、s=s+t D、s=t (7)A、x^i/f(i) B、x^i/f C、x^(i+1)/f(i+1) D、x/f

(8)A、s=t B、s=s+t C、s=s+ x^i/f(i) D、s=s+ x^i/f 3.过程f的功能是:返回Single类型数组n个元素的平均值,查找数组中最大值、最小值并通过相应的实参返回到调用处。该过程可以被其他模块中的过程所调用。

(9) f(a() As Single, n As Integer, (10) ) As Single Dim i As Integer

max = a(1): min=a(1) For i = 2 To n

If a(i)>max Then max=a(i) If a(i)

(12) 【供选择的答案】

(9) A、Function B、Sub C、Private Sub D、Private Function (10) A、max As Single,min As Single B、max As Integer,min As Integer C、Byval max As Single (12) A、find = max

D、Byval max As Single,Byval min As Single

D、f=a(i)/n

(11) A、f=f+a(i) B、f=f+a(i)/n C、f=a(i)

B、End Sub C、find = min D、End Function

五、上机调试题:程序设计

1.编制判断是否同时被17与37整除的Function过程。在双击窗体事件中,调用这个Function过程,在窗体上输出100~10000之间所有能同时被17与37整除的数。

2. 编制通用函数过程fsum,用于计算1个三位整数的各位数字之和。(如引用fsum(132)的结果是6等等)

3.按要求编写程序:

(1)定义Function函数:计算一维整型数组所有元素的平均值。

(2)从键盘输入输入10个整数保存在数组中,调用Function函数,计算数组元素的平均值,并在窗体的文本框Text1中输出结果。

代码编写:

1、 2、

32

VB网络课堂:info.zjfc.edu.cn/vbweb