1、vb大一程序设计第八章习题答案1第八章1Sub过程与Function过程的主要区别是 BASub过程可以通过Call语句调用,而Function过程不可以BSub过程不能通过过程名返回值,而Function过程可以CSub过程与Function过程的参数传递方式不一样DFunction过程只能返回1个值,而Sub过程可以返回多个值2以下叙述中错误的是 C 。A在Sub过程中可以调用Function过程B可以在程序的任何地方调用以Public声明的过程C在Sub过程中可以嵌套定义Function过程D用Static声明的过程中的局部变量都是Stati变量3以下关于函数过程的叙述中,正确的是A 。
2、A函数过程虚参的类型与函数返回值的类型没有关系B在函数过程中,过程的返回值可以有多个C当数组作为函数过程的参数时,既能以按值方式传递,也能以按址方式传递D若不指明函数过程参数的类型,则该参数没有数据类型4以下叙述中错误的是B 。在VB应用程序中,A过程的定义不可以嵌套,但过程的调用可以嵌套B过程的定义可以嵌套,但过程的调用不能嵌套C程序设计人员不能任意指定事件过程的名称D在Function过程中可以Sub过程5. 下列描述中正确的是(A )(A) VB只能通过调用Sub过程(B)Sub过程可以嵌套定义也可以嵌套调(C)可以在Sub过程中定义1个Function过程,但不能定义Sub过程(D)S
3、ub过程和Function过程都必须带返回值6.下列叙述中正确的是(B) )(A)一个Sub过程至少要有一个Exit Sub语句(B)一个Sub过程必须有一个End Sub语句(C)可以在Sub过程中定义一个Function过程,但不能定义Sub过程(D)调用一个Function过程可以获得多个返回值7以下语句用来定义过程subP,其中正确的是 D 。ADim Sub subP(x , y) BPublic subP(x , y)CPrivate Sub subP(x , y) As Integer DSub subP(x , y)8.在窗体上放置2个标签和一个命令按钮,其名称分别为Label
4、1. 、Label2、command1Private Sub Command1_Click()a = Val(Label2.Caption)Call func(Label1, a)Label2.Caption = aEnd SubPrivate Sub Form_Load()Label1.Caption = ABCDELabel2.Caption = 10End SubPrivate Sub func(L As Label, ByVal X As Integer)L.Caption = 1234a = a * aEnd Sub程序运行后,单击命令按钮在2个标签中显示的内容分别是 1234 10
5、9. Option Base 1Sub subp(b() As Integer)For i = 1 To 3b(i) = 3 * iNext iEnd SubPrivate Sub Command1_Click()Dim a(3) As Integerarr = Array(8, 4, 3)For i = 1 To 3a(i) = arr(i)Next isubp a()For i = 1 To 3Print a(i)Next iEnd Sub运行程序后,在窗体上输出的内容为(A)A)3 6 9 B)843 C)963 D)34810. Function func(a As Integer,
6、b As Integer) As IntegerStatic x As Integer, y As Integerx = 0: y = 2y = y + x + 1: x = y + a + bfunc = xEnd FunctionPrivate Sub Command1_Click()Static k As Integer, m As IntegerDim p As Integerk = 5: m = 2p = func(k, m)Print pp = func(k, m)Print pEnd Sub输出结果是 10 1011. Sub s1(ByVal x As Integer, ByV
7、al y As Integer)Dim t As Integert = x: x = y: y = tEnd SubPrivate Sub Command1_Click()Dim a As Integer, b As Integera = 10: b = 30s1 a, bPrint a=; a; b=; bEnd Sub运行程序后,在窗体上输出的内容为(C)A)a=30b=10B)a=30b=30C)a=10b=30D)a=10b=1012. Sub s(x As Single, y As Single)t = x: x = t / y: y = t Mod yEnd Sub Private
8、 Sub Command1_Click()Dim a As Single, b As Singlea = 5: b = 4s a, bPrint a; bEnd Sub输出结果1.25 113. Function subl(a As Integer, ByVal b As Integer) As Integera = Val(b)subl = aPrint sublEnd FunctionPrivate Sub Command1_Click()Call subl(-15.1, 2.54)End Sub运行程序后,在窗体上输出的内容为(A)A)3 B)2 C)-15 D)提示错误运行结果314.
9、 假定有以下两个过程: Sub S1(ByVal x As Integer, ByVal y As Integer) Dim t As Integer t=x: x=y: y=t End Sub Sub S2(x As Integer, y As Integer) Dim t As Integer t=x: x=y: y=t End Sub 则以下说法中正确的是 (B )A用过程S1可以实现交换两个变量值的操作,S2不能实现B用过程S2可以实现交换两个变量值的操作,S1不能实现C用过程S1和S2都可以实现交换两个变量值的操作D用过程S1和S2都不能实现交换两个变量值的操作15.下列关于过程及过
10、程参数的描述中,错误的是(C )A过程的参数可以是控件名称B用数组作为过程的参数时,使用的是“传地址”方式C只有函数过程能够将过程中处理的信息传回调用的程序中D窗体可以作为过程的参数16.以下说法正确的是(D)A)事件过程也是过程,与通用过程完全一样B)事件过程是程序员编写的各种子过程C)事件过程通常放在标准模块中D)事件过程是用来处理由用户操作或系统激发的事件的代码17. 执行“工程”菜单中的(D )命令可以添加一个标准模块。A) 添加过程 B) 通用过程 C) 添加窗体 D) 添加模块 18.通用过程可以通过执行“工具”菜单中的(A)命令来建立。A) 添加过程 B) 通用过程 C) 添加窗
11、体 D) 添加模块 二填空题1.Function fun(s As Integer)For i = 1 To sSum = Sum + iNext ifun = Sum End FunctionPrivate Sub Form_Click()Text1.Text = Str(fun(10)End SubPrivate Sub text1_change()Label1.Caption = vb programmingEnd Sub文本框中显示55,而标签框中显示 vb programming2.阅读程序 Function fun(x As Long) As IntegerIf x Mod 2 =
12、 0 Thenfun = True (1)Elsefun = iffun = False (2)End If End FunctionPrivate Sub Command1_Click()Dim num As Longnum = Val(Text1.Text)p = IIf(fun(num), 奇数, 偶数) (3)Print Str(num) & 是1个“ & p; End Sub3. 在命令按钮事件过程中定义1个数组,将这个数组作为参数转送到通用过程FindMax,并返回该数组的最大值Option Base 1Dim arr2() As IntegerPrivate Function f
13、indmax(a() As Integer) As IntegerDim start As Integer, finish As Integer, i As Integerstart = LBound ( a)finish = UBound (a)Max = a(Start)For i = start + 1 To finishIf a(i) Max Then Max =a(i) Next ifindmax = MaxEnd FunctionPrivate Sub Command1_Click()Dim arr1arr1 = Array(12, 435, 76, 24, 78, 54, 866
14、, 43)b = UBound(arr1)ReDim arr2(8) As IntegerFor i = 1 To barr2(i) = CInt(arr1(i)Next im = findmax(arr2)Print 最大值:; mEnd Sub命令按钮的单击事件过程中定义1个数组,把这个数组作为参数传送到通用过程FindMax,并返回该数组的最大值Private Sub Command1_Click()Dim a(1 to 10) As Integer, I As Integer, Result As IntegerFor I=1 to 10a(i)=inputbox(“enter num
15、 please”)Next IResult=FindMax( a )Print “数组a中的最大值为:” ; resultEnd SubPrivate Function FindMax( b () As Integer)Dim s1 as Integer, s2 AS Integer, max As IntegerS1=Lbound(b)S2=Ubound(b)max= b(s1) For I= s1 to s2 If maxb(i) thenMax=b(i)End ifNext IFindMax= max End Function三.程序设计1用递归过程计算两个整数的最大公约数Private
16、 Sub Command1_Click()Dim m As Long, n As Longm = Val(Text1.Text)n = Val(Text2.Text)Label3.Caption = m & 与 & n & 最大公约数是If m 0ls1 = Len(s1)s1 = Left(s1, i - 1) + Mid(s1, i + ls2)i = InStr(s1, s2)LoopDeleStr = s1End FunctionPrivate Sub Command1_Click()Dim ss1 As Stringss1 = Text1.Textss1 = DeleStr(ss1,
17、 Text2.Text)Text3.Text = ss1End Sub5. 对若干个整数按升序排列 Private Sub form_click()Dim nums()Dim i As Integer, j As Integer, n As Integer, t As Integern = Val(InputBox(请输入参加排序的数:)ReDim nums(1 To n)For i = 1 To nnums(i) = Val(InputBox(请输入第 & Str(i) & 个参加排序的数)Next iFor i = 1 To n - 1For j = 1 To n - iIf nums(j
18、) nums(j + 1) Thent = nums(j)nums(j) = nums(j + 1)nums(j + 1) = tEnd IfNext jNext iFor i = 1 To nPrint nums(i);Next iEnd Sub6.编写子程序验证哥德巴赫猜想方法1Private Sub Command1_Click()Dim N As Long, I As Long, J As LongIf IsNumeric(Text1.Text) ThenN = CLng(Text1.Text)If N Mod 2 = 0 And N = 6 ThenFor I = 2 To N 2I
19、f I = 2 Or Pd(I) ThenJ = N - IIf Pd(J) ThenLabel1.Caption = 结果: & N & = & I & + & J & 符合!Exit SubEnd IfEnd IfNextLabel1.Caption = 结果: 不符合!ElseText1.Text = 请输入大于或等于6的偶数!End IfElseText1.Text = 请输入数字!End IfEnd SubPrivate Sub Text1_GotFocus()Text1.Text = Text1.ForeColor = &H0Command1.Visible = TrueEnd S
20、ubFunction Pd(a As Long) As BooleanDim b As LongFor b = 2 To a - 1DoEventsIf a Mod b = 0 ThenPd = FalseExit ForElsePd = TrueEnd IfNextEnd Function方法1: Private Sub Form_Click() Dim n%, n1%, n2% n = Val(InputBox(输入大于6的正整数) For n1 = 3 To n 2 Step 2 n2 = n - n1 If Prime(n1) And Prime(n2) Then Print n &
21、= & n1 & + & n2 Exit For 结束循环 End If Next n1 End Sub Private Function Prime(n%) As Boolean Dim k%, Yes As Boolean Yes = True For k = 2 To n 2 If n Mod k = 0 Then Yes = False: Exit For Next k Prime = Yes End Function7.已知函数sum(k,n)=1k+2k+3k+nk。已知k=2,n=5求值Private Function Power(a As Integer, b As Integ
22、er) As Long Dim i As Integer, t As Long t = 1 For i = 1 To a t = t * b Next i Power = tEnd FunctionPrivate Function Sum(k As Integer, n As Integer) As Long Dim i As Integer, s As Integer For i = 1 To n s = s + Power(k, i) Next i Sum = sEnd FunctionPrivate Sub Form_Click() Dim k As Integer, n As Inte
23、ger, s As Long k = 2 n = 5 s = Sum(k, n) Print sEnd Sub8.求12+22+32+42+k2Private Sub Form_Click()k = Val(InputBox(请输入k)s = 0For i = 1 To ks = s + i 2NextMsgBox 结果为 & sEnd Sub9.当单击按钮时,从键盘上输入一个正整数,调用numdecl过程判断输入的数是否为降序数Private Sub Command1_Click()Dim n As Long, flag As Booleann = InputBox(请输入一个正整数)Cal
24、l numDec1(n, flag)If flag ThenPrint n; 是降序数ElsePrint n; 不是降序数End IfEnd SubPrivate Sub numDec1(n As Long, flag As Boolean)Dim x As String, i As Integerx = Trim(Str(n)For i = 1 To Len(x)If Mid(x, i, 1) Mid(x, i + 1, 1) Then Exit ForNext iIf i = Len(x) + 1 Then flag = True Else flag = FalseEnd Sub10.编写程序,计算s!=a!+b!+c!Private Sub Command1_Click() Dim S1 As Long, S2 As Long, S3 As Long S1 = Text
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1