VB第6章课后练习答案
vb6.0第六章 习题解答
习题六一、选择题答案Aadcc dc二、填空题答案1、a(I,j)+b(I,j)2、下标越界3、64、1 2 2 4 3 6第六章选择题部分解析3、sum表示a数组各元素的和X表示a数组元素和的平均值最后打印出a数组中大于平均值的元素Sum=1+2+3+4+5=15,x=3大于3的有4,55、倒着看文本框中显示什么?答:显示的是array1(1,3)+array2(2,3)的和接着往前看程序发现array1(i,j)=i+j所以array1(1,3)=1+3=4Array2(2,3)=2+3=5所以4+5=9二、填空题解析2、x数组最大元素是x(6)退出for循环时,i=7然后print x(i)相当于print x(7)但是没有x(7)这个元素,超出最大下标了。
若把print x(i)改为print x(a)则值是36第六章编程题1、Private Sub Form_Click()Dim a(1 To 20) As Integer, i As IntegerDim j As Integerj = 0 '用来表示小于60的个数Picture1.Print "产生的数组为:"Picture1.PrintPicture2.Print "其中小于60的元素有:"Picture2.PrintFor i = 1 To 20a(i) = Int(Rnd * 100 + 1)Picture1.Print a(i);If i Mod 10 = 0 Then Picture1.PrintIf a(i) < 60 Thenj = j + 1Picture2.Print a(i); " ";If j Mod 10 = 0 Then Picture2.PrintEnd IfNext iEnd SubPrivate Sub Form_Load()RandomizeEnd Sub2、Private Sub Form_Click()Dim a(1 To 10) As IntegerDim max As Integer, min As Integer, i As IntegerFor i = 1 To 10a(i) = Int(Rnd * 90 + 10)Picture1.Print a(i);If i Mod 5 = 0 Then Picture1.PrintNext imax = a(1)min = a(1)For i = 2 To 10If a(i) > max Then max = a(i)If a(i) < min Then min = a(i)Next iLabel1.Caption = "最大数为:" & maxLabel2.Caption = "最小数为:" & minEnd SubPrivate Sub Form_Load()RandomizeEnd Sub3、Dim a(1 To 20) As IntegerPrivate Sub Command1_Click()Dim x As Integer, flag As Integer, i As Integerflag = 0 '标志,表示数组中是否存在xx = InputBox("请输入x的值,为整数", "输入", 1) For i = 1 To 20If x = a(i) Thenflag = 1 'flag改变,说明存在Exit ForEnd IfNext iIf flag = 1 ThenPrint "数组中存在" & xPrint "位置:" & iElsePrint "数组中不存在" & xEnd IfEnd SubPrivate Sub Form_Click()Dim i As IntegerFor i = 1 To 20 '生成数组a(i) = Int(Rnd * 100) '题目没要求,随便的范围Next iFor i = 1 To 19 '冒泡排序For j = 20 To i + 1 Step -1If a(j) > a(j - 1) Thent = a(j)a(j) = a(j - 1)a(j - 1) = tEnd IfNext jNext iFor i = 1 To 20 '打印数组Print a(i);If i Mod 5 = 0 Then PrintNext iEnd SubPrivate Sub Form_Load()RandomizeEnd Sub4、Private Sub Form_Click()Dim a(1 To 15) As SingleDim i As Integer, c1 As Integer, c2 As Integer, c3 As Integer, avg As SingleFor i = 1 To 15a(i) = InputBox("请输入第" & i & "个学生的成绩", "输入成绩", 60)avg = avg + a(i)Next iavg = avg / 15For i = 1 To 15If a(i) > avg Then c1 = c1 + 1If a(i) < 60 Then c2 = c2 + 1If a(i) >= 90 Then c3 = c3 + 1Next iPrint "高于平均分的人数"; c1Print "<60的人数"; c2Print ">=90的人数"; c3End Sub5、Dim a(1 To 4, 1 To 4) As Integer, b(1 To 4, 1 To 4) As Integer, c(1 To 4, 1 To 4) As IntegerPrivate Sub Command1_Click()‘(2)题For i = 1 To 4For j = 1 To it = a(i, j)a(i, j) = a(j, i)a(j, i) = tNext jNext iPicture2.ClsPicture2.Print "a转置后:"Picture2.PrintFor i = 1 To 4For j = 1 To 4Picture2.Print a(i, j); " ";Next jPicture2.PrintNext iEnd SubPrivate Sub Command2_Click()‘(3)题Picture2.ClsMax = c(1, 1)t1 = 1t2 = 1For i = 1 To 4For j = 1 To 4If c(i, j) > Max ThenMax = c(i, j)t1 = it2 = jEnd IfNext jNext iPicture2.Print "max=", MaxPicture2.Print "c("; i; ","; j; ")"End SubPrivate Sub Command3_Click()‘(4)题Picture1.ClsPicture2.ClsFor i = 1 To 4For j = 1 To iPicture1.Print a(i, j); " ";Next jPicture1.PrintNext iFor i = 1 To 4For j = i To 4Picture2.Print Tab(j * 5); b(i, j);Next jPicture2.PrintNext iEnd SubPrivate Sub Command4_Click()‘(5)题Picture1.ClsFor i = 1 To 4t = a(1, i)a(1, i) = a(3, i)a(3, i) = tNext iFor i = 1 To 4For j = 1 To 4Picture1.Print a(i, j); " ";Next jPicture1.PrintNext i End SubPrivate Sub Command5_Click()‘(1)题Picture3.Print "c矩阵:"Picture3.PrintFor i = 1 To 4For j = 1 To 4c(i, j) = a(i, j) + b(i, j)Picture3.Print c(i, j); " ";Next jPicture3.PrintNext iEnd SubPrivate Sub Form_Load()ShowPicture1.Print "a矩阵:"Picture1.PrintPicture2.Print "b矩阵:"Picture2.PrintFor i = 1 To 4For j = 1 To 4a(i, j) = Int(Rnd * 40 + 30)b(i, j) = Int(Rnd * 35 + 101)Picture1.Print a(i, j); " ";Picture2.Print b(i, j); " ";Next jPicture1.PrintPicture2.PrintNext iEnd Sub。
VB第6章课后练习答案
第6章过程与函数一、填空题1.数组名作为过程或函数实参,相应的形参传递方式为____按地址传递____。
2.一维长整型数组a作过程形参写作:a() As Long,二维长整型数组b作过程形参写作:____b() As Long ____。
3.过程形参为整型,对应实参为5.64,传递给形参的值为____6____。
4.调用过程时对形参的改变不会导致相应实参变量的改变,则该形参采用____按值传递____方式。
5.调用过程时对形参的改变就是对相应实参变量的改变,则该形参采用____按地址传递____方式。
6.声明Single类型全局变量x,写作____Public x As Single(或 Publicx!)____;声明Integer类型静态变量x,写作____Static x As Integer (或Static x%)____。
7.在窗体Form1的过程中引用窗体Form2中的全局变量y,写作____Form2.y____。
8.阅读如下代码,单击窗体后,窗体上的显示结果为____s = 2 s = 5 s = 9____。
Dim i As Integer, n As IntegerPrivate Sub Form_Click()Dim i As IntegerFor i = 1 To 3s = sum(i): Print "s ="; s;Next iEnd SubPrivate Function sum(n As Integer)Static j As Integerj = j + n + 1: sum = jEnd Function二、选择题1.VB程序设计语言中,函数过程与子过程必须分别用关键字( C)声明。
(A)Private,Public (B)Public,Private(C)Function, Sub (D)Sub, Function2.要调用一个已经定义好的函数myfunction( ),用语句的方式调用函数,下列写法哪个是正确的( D)(A)Call myfunction (B)myFunction(C)myFunction ( ) (D)CallmyFunction( )3.定义过程时,如果在过程名前加关键字Private,则该过程为( A)。
VB课后练习答案.doc
VB课后练习答案P12第一章课后练习答案:一、选择题二、填空题第二章(P39)选择题答案:填空题答案:1.Cmdl, Click2.Text 1.Text二"Hello"P64第三章课后练习答案一、选择题二、填空题1.(Y Mod 4=0 And Y Mod 10000) Or Y Mod 400=0 2.a<>0 And b*2-4*a*c>=0 3.X Mod 5=0 And X Mod 2=0 或 Tnt(x/5)=X/5 And Int(X/2)=X/2 4. 注意:返回的均是字符型。
第四章课后答案:P79一、选择题答案为:$$41.50010二、填空题三、程序设计题(提示)1、鸡的头数为x,兔的头数为y,则可列方程式:\x-\- y = h (兀 * 2 + y * 4 =于求得x, y 的值再在窗体上输出结果。
2、 利用Format 格式函数,再通过窗体的Print 将格式化后的信息输出到窗体上。
3、 厶员I 的周长=2加" $岡的面积=加25、通过Inputbox 函数或用文本框输入三边和圆的半径a,b,c, r,利用公式:. a + b + cn 二 ---------2 S 三角形面积=Jh (h - - b )(h - c )P97第五章课后练习答案:第9题| D一、选择题答案D 改为:30二、填空题第]题Y Mod 4二0 And Y Mod 100二0 Or Y Mod 400二0 第2题笫3题笫4题Private Sub Command l_Click()X=Val(Textl.Text)Select Case XCase Is>200. Isv-10()Print “xv 100 Or x>200"Case ls<=0Y=l()()-XCase Isv二100Y=100+XCase Is<=200Y=400End SelectText2.Text=yEnd Sub三、程序设计题(提示)1、判断条件:假设一个整数为xX Mod 3=0 Or x Mod 5=0 或x/3=x\3 Or x/5=x\52、选项按钮的知识点在P182,利用该控件的Wlue属性判断是否选中该项。
vb教程答案第六章
1.阅读程序,写出程序结果答: 6 7 92 5 81 3 42.提示:在交换首尾对称位置的数组元素时注意,循环的终值只能到数组的一半,而不能到数组的最大下标,否则会将数组元素值又还原成原来的数值。
程序界面如下图5-2所示:图5-2参考程序如下:Private Sub Command1_Click()Dim a(15) As Integer, i As Integer, j As Integer, t As Integer'给数组赋值,并输出在文本框1中For i = 1 To 15a(i) = Int(Rnd * 100) + 1Text1.Text = Text1.Text & Str(a(i))Next i'交换数组对称位置的数据For i = 1 To 7t = a(i)a(i) = a(16 - i)a(16 - i) = tNext i'输出交换后的数组内容For i = 1 To 15Text2.Text = Text2.Text & Str(a(i))Next iEnd Sub3.提示:可以对数组的每个元素与其后面的所有元素逐个进行比较,如果有相同的数,将后面出现的数值清为0,最后统计所有不为0的数据个数。
参考程序如下:Private Sub Command1_Click()Dim a(20) As Integer, i As Integer, j As Integer, k As Integer'给数组赋值,并输出在窗体上RandomizeFor i = 1 To 20a(i) = Int(Rnd * 100) + 1Print a(i);Next iPrint'统计有多少个不相同的数For i = 1 To 19For j = i + 1 To 20If a(j) = a(i) Then a(j) = 0Next jNext i'统计不相同的数,并输出在窗体上For i = 1 To 20If a(i) <> 0 Then k = k + 1Next iPrint "一共有" & k & "个不相同的数"End Sub4.本题的主要思路是:先给数组的20个数赋值并输出,然后按照求最大值的算法思想,先将前4个数之和赋给存放最大值的变量max,同时记录4个数中第一个数的下标,然后对剩下的每4个数求和,判断是否大于max,如果是,将新的最大值送给max,同时记录其首数据的下标,最后,只要输出四个相邻数之和的最大值,并输出这四个数即可。
VB程序设计课后习题答案
VB课后习题解答第一、二章:1、简述VB6.0的主要特点答:方便、直观的可视化设计工具;面向对象程序设计方法;事件驱动的编程机制;易学易用的应用程序集成开发环境;结构化程序设计语言;完备的联机帮助功能;强大的多媒体、数据库和网络功能。
2、当正常安装VB6.0后,误把Windows子目录删除。
当重新安装Windows后,是否需要再次安装VB6.0?答:要重新安装的。
因为安装VB6.0时,有些VB程序被系统自动安装在Windows目录下,所以一旦删除Windows子目录,就必须重新安装VB6.0。
3、Visual Basic 6.0分为学习版、专业版和企业版,如何知道所安装的是哪个版本?答:在VB6.0的启动界面上,能显示对应的版本。
4、叙述建立一个完整应用程序的过程答:首先建立用户界面的控件对象(简称控件);然后对控件属性进行设置;再对控件事件过程及编程;保存应用程序;程序调试和运行。
5、Visual Basic 6.0有多种类型窗口,若要在设计时看到代码窗口,应怎样操作?答:选择“视图”—>“代码窗口”命令或单击“工程资源管理器”窗口中的“查看代码”按钮。
6、当建立好一个简单的应用程序后,假定该工程仅有一个窗体模块。
试问该工程涉及多少个要保存的文件?若要保存该工程的所有文件,应先保存什么文件?再保存什么文件?若不这样做,系统会出现什么信息?答:涉及两个文件要保存。
先保存窗体文件(.frm),再保存工程文件(.vbp)。
若先保存工程文件,系统也会先弹出“文件另存为”对话框,要求先保存窗体文件。
7、保存文件时,若不改变目录名,则系统默认的目录是什么?答:系统默认的目录是VB988、安装Visual Basic 6.0后,帮助系统是否也已安装?答:没有。
VB6.0的帮助系统在两张光盘上,即MSDN Library,其中还包含了VC、VFP等其他语言的帮助系统。
9、如何使用Visual Basic 6.0的帮助系统?答:最方便的方法是选定要帮助的内容,然后按F1功能键,这时系统打开MSDN Library查阅器,直接显示与选定内容有关的帮助信息。
vb习题答案(1-9章)最全的
第一章VB程序设计概述二、选择题CADAB ACDAB CBDBB第二章VB可视化编程基础二、选择题ABDCA CACBC DADAD BDBBB三、填空题1.可视2.LEFT TOP WIDTH HEIGHT3.按字母序4.查看代码5.工具、选项6.Form1 Font7.Multiline8.只读属性9.工程属性通用Form1.show10.tabindex 0第三章VB语言基础二、选择题BCADB ADBBC DBCBA DAABB三、填空题1.整型、长整型、单精度、双精度2.sin(30*3.14/180)+sqr(x+exp(3))/abs(x-y)-log(3*x)3.160 今天是:3-224.False5.-4 3 -3 3 -4 46.“CDEF”7.(x mod 10)*10+x\108.(35\20)*20=20 ( 35 \ 20 ) * 20 = 209.x mod 3=0 or x mod 5=010.27.6 8.2 8 1 100 397 true false第四章VB程序控制结构二、选择题DBCAD ABCAB D25BAC CBACB DABBC三、填空题1.字符型整型2. 1 2 33.x>74.x<amin5.10246.117.128.6 I “*”;9.0 100 i+1 10.2*I 6 “*”;三、编程题1.Private Sub Form_Click()Dim n As Integer, i As Integer, s As Longs = 1n = Val(InputBox("请输入一个整数:"))For i = 1 To ns = s * iNext iMsgBox n & "!=" & sEnd Sub2.Private Sub Form_Click()Dim i%, k%For j = 3 To 100k = Int(Sqr(j))For i = 2 To kIf j Mod i = 0 Then Exit ForNext iIf i > k Theny$ = y$ & " " & jEnd IfNext jMsgBox y, , "显示质数"End Sub3.Private Sub Form_Click()Dim i%, s%For i = 1 To 100s = s + iNext iMsgBox "1+2+3+……+100=" & sEnd Sub4.Private Sub Form_Click()Dim x%, y%For x = 1 To 50For y = 1 To 33z = 100 - x - yIf 2 * x + 3 * y + 0.5 * z = 100 ThenPrint "小鸡、公鸡、母鸡分别为:"; x; y; z End IfNext yNext xEnd Sub5.Private Sub Command1_Click()Dim a%, b%, c%, p!, s!doa = Val(InputBox("请输入三角形的第一条边"))if a=-1 then exit dob = Val(InputBox("请输入三角形的第二条边"))c = Val(InputBox("请输入三角形的第三条边"))If a + b > c And a + c > b And b + c > a Thenp = (a + b + c) / 2s = Sqr(p * (p - a) * (p - b) * (p - c))Print sElseMsgBox "你输入的三边不能构成三角形,请重新输入"End IfloopEnd Sub6. Private Sub Form_Click()Dim i As Integer, j As Integer,k as integerFor i = 1 To 8For k=1 to 8-iPrint spc(3);Next kFor j = 1 To 2 * i - 1Print i;Next jPrintNext iEnd Sub7. Private Sub Form_Click()Dim i As Integer, j As Integer, k As IntegerFor i = 1 To 30For j = 1 To 30For k = 1 To 30If i ^ 2 + j ^ 2 = k ^ 2 ThenPrint i & " ^ 2 + " & j & "^ 2 =" & k & "^ 2"End IfNext kNext jNext iEnd Sub8.Private Sub Form_Click()Dim x As Integer, a As Integer, b As Integer, c As Integer, ss As String ss = ""For x = 100 To 999a = Int(x / 100)b = Int((x - 100 * a) / 10)c = x - 100 * a - 10 * bIf a ^ 3 + b ^ 3 + c ^ 3 = x Thenss = ss & " " & xEnd IfNext xPrint ssEnd Sub9.Dim sr As Long, r As Singlesr = Val(InputBox("请输入收入:"))Select Case srCase Is < 200r = 0Case 200 To 400r = (sr - 200) * 0.04Case 400 To 5000r = sr * 0.04Case Is >= 5000r = sr * 0.05End SelectPrint "收入为" & sr & "时所缴纳的税为:" & rEnd Sub10.Private Sub Form_Click()Dim t As Single, wf As Singlet = Val(InputBox("请输入上网时间:"))Select Case tCase Is < 10wf = 50Case 10 To 60wf = t * 6Case Is >= 60wf = t * 4End SelectIf wf >= 200 Thenwf = 200End IfPrint "上网时间为" & t & "小时上网的费用为:" & wf End Sub11. Private Sub Form_Click()Dim yf As Integeryf = Val(InputBox("请输入月份"))Select Case yfCase 12, 1, 2MsgBox yf & "月份是冬季"Case 3, 4, 5MsgBox yf & "月份是冬季"Case 6, 7, 8MsgBox yf & "月份是冬季"Case 9, 10, 11MsgBox yf & "月份是冬季"Case ElseMsgBox "输入错误,请重新输入"End SelectEnd Sub12.if的方法Private Sub Form_Click()Dim x As Single, f As Singlex = Val(InputBox("请输入一个数"))If x < 0 Thenf = 2 * x - 1ElseIf x = 0 Thenf = 4 * xElseIf x > 0 Thenf = 7 * x - 5End IfPrint "f(" & x & ")=" & fEnd SubSelect case语句:Private Sub Form_Click()Dim x As Single, f As Singlex = Val(InputBox("请输入一个数"))Select Case xCase Is < 0f = 2 * x - 1Case 0f = 4 * xCase Is > 0f = 7 * x - 5End SelectPrint "f(" & x & ")=" & fEnd Sub第五章数组一、选择题DCACD DBCDC BADAD二、填空题1.名称Index2.preserve3.变体4.235.1 2 3 1 4 16.1 2 3 2 4 67. max max=arr1(i)三、编程题1.Option Base 1Private Sub Form_Click()Dim a(10) As Integer, i%, temp%For i = 1 To 10a(i) = Val(InputBox("请输入第" & i & "个整数")) Next iPrint "原来各元素的值为:"For i = 1 To 10Print a(i);Next iFor i = 1 To 5temp = a(11 - i)a(11 - i) = a(i)a(i) = tempNext iPrintPrint "交换后各元素的值为:"For i = 1 To 10Print a(i);Next iEnd Sub2.Option Base 1Private Sub Form_Click()Dim a As Variant, b As Variant, c As Variant, i%a = Array(2, 8, 7, 6, 4, 28, 70, 25)b = Array(79, 27, 32, 41, 57, 66, 78, 80)ReDim c(8)For i = 1 To UBound(a)c(i) = a(i) + b(i)Next iFor i = 1 To UBound(c)Print c(i);Next iEnd Sub3.Option Base 1Private Sub Form_Click()Dim a() As Variant, i%, j%, n%, m%, max%, hh%, lh%n = Val(InputBox("请输入一个整数:"))m = Val(InputBox("请输入一个整数:"))ReDim a(n, m)For i = 1 To n - 1For j = 1 To m - 1a(i, j) = Int(Rnd * 101 + 50)If max < a(i, j) Then max = a(i, j): hh = i: lh = jNext jNext iPrint "最大值为:"; max; "所在的行号为:"; hh; "所在的列号为:"; lh End Sub4.Option Base 1Private Sub Form_click()Dim a(50) As Integer, b(9) As Integer, i%, j% For i = 1 To 50a(i) = Int(Rnd * 90 + 10)j = Int(a(i) / 10)b(j) = b(j) + 1Next iFor j = 1 To 9Print b(j);Next jEnd Sub5.Option Base 1Private Sub Command1_Click(Index As Integer) Select Case IndexCase 0Text3 = Val(Text1) + Val(Text2)Case 1Text3 = Val(Text1) - Val(Text2)Case 2Text3 = Val(Text1) * Val(Text2)Case 3Text3 = Val(Text1) / Val(Text2)End SelectEnd Sub6.Private Sub Form_click()Dim s!, i&, j&i = 3s = 1j = 0Doj = j + 1s = s + (-1) ^ j / ii = i + 2Loop While 1 / i > 10 ^ (-6)Print 4 * sEnd Sub7.Option Base 1Private Sub Form_click()Dim a(40) As Long, i As Integera(1) = 1a(2) = 1For i = 3 To 39a(i) = a(i - 1) + a(i - 2)Next iFor i = 1 To 40Print a(i);Next iEnd Sub8.冒泡法:Option Base 1Private Sub Form_click()Dim a(10) As Single, ok As Boolean, i%, j%, x!For i = 1 To 10a(i) = Val(InputBox("请输入第" & i & "个数"))Print a(i);Next iFor i = 10 To 2 Step -1ok = TrueFor j = 1 To i - 1If a(j) > a(j + 1) Thenx = a(j)a(j) = a(j + 1)a(j + 1) = xok = FalseEnd IfNext jIf ok Then Exit ForNext iPrintPrint "冒泡排序的结果为:"For i = 1 To 10Print a(i);Next iEnd Sub选择法:Option Base 1Private Sub Form_click()Dim a(10) As Single, p!, i%, j%, min!For i = 1 To 10a(i) = Val(InputBox("请输入第" & i & "个数"))Print a(i);Next iFor i = 1 To 9p = iFor j = i + 1 To 10If a(j) < a(p) Then p = jNext jIf p <> i Thenmin = a(i)a(i) = a(p)a(p) = minEnd IfNext iPrintPrint "排序后的结果为:"For i = 1 To 10Print a(i);Next iEnd Sub第六章过程一、选择题CDBAB DBCCC ABCBD CDB二、填空题1.sub function2.形式实际3.内存的同一地址4.myf(a%,b() as integer ) as Boolean5.值传递地址传递6.lbound ubound7.局部8.通用任何过程9.2 5 9 10.2411.2 12.30 7013.-1三、编程题1. Option Base 1Private Sub Form_Click()Dim m1%, m2%, b() As IntegerFor i = 3 To 7 Step 2ReDim b(i)For j = 1 To ib(j) = Int(Rnd * 90 + 10)Print b(j);Next jm1 = b(1)m2 = b(1)Call mm(m1, m2, b())PrintPrint "最大值为:"; m1Print "最小值为:"; m2Next iEnd SubPublic Sub mm(max%, min%, a() As Integer) Dim i%For i = LBound(a) To UBound(a)If a(i) > max Then max = a(i)If a(i) < min Then min = a(i)Next iEnd Sub2.sub过程:Public Sub jc(n%, s&)Dim i%s = 1For i = 1 To ns = s * iNext iEnd SubPrivate Sub Form_click()Dim s&, a%, b%, c%, s1&a = Val(InputBox("请输入一个整数:"))b = Val(InputBox("请输入一个整数:"))c = Val(InputBox("请输入一个整数:")) Call jc(a, s1)s = s1 + sCall jc(b, s1)s = s1 + sCall jc(c, s1)s = s1 + sPrint a & "!+" & b & "!+" & c & "!=" & s End SubFunction过程:Public Function jc(n%) As LongDim i%, s%s = 1For i = 1 To ns = s * iNext ijc = sEnd FunctionPrivate Sub Form_click()Dim s&, a%, b%, c%, s1&a = Val(InputBox("请输入一个整数:"))b = Val(InputBox("请输入一个整数:"))c = Val(InputBox("请输入一个整数:"))s1 = jc(a) + jc(b) + jc(c)Print a & "!+" & b & "!+" & c & "!=" & s1 End Sub3.Public Sub jo(n As Integer)If n Mod 2 = 0 ThenPrint "false"ElsePrint "true"End IfEnd Sub4.Private Sub Form_click()Dim b(1 To 100) As Integer, i%For i = 1 To 100b(i) = Int(Rnd * 100 + 1)Next iCall sort(b())For i = 1 To 100If i Mod 10 = 0 Then PrintPrint b(i);Next iEnd SubPublic Sub sort(a() As Integer)Dim n%, ok As Boolean, k As Integern = UBound(a)For i = 1 To n - 1ok = TrueFor j = 1 To n - iIf a(j) > a(j + 1) Thenk = a(j): a(j) = a(j + 1): a(j + 1) = k: ok = False End IfNext jIf ok Then Exit ForNext iEnd Sub5.Private Sub Form_click()Dim b(1 To 10) As Integer, i%For i = 1 To 10b(i) = Val(InputBox("请输入第" & i & "个整数")) Next iCall sort(b())Print "排序后的结果"For i = 1 To 10Print b(i);Next iEnd SubPublic Sub sort(a() As Integer)Dim n%, p%, k As Integern = UBound(a)For i = 1 To n - 1p = iFor j = i + 1 To nIf a(j) < a(p) Then p = jNext jIf p <> i ThenMin = a(i)a(i) = a(p)a(p) = MinEnd IfNext iEnd Sub6.Public Sub gys(m%, n%)Dim r%, t%If m <> 0 And n <> 0 ThenIf m < n Thent = m: m = n: n = tEnd Ifr = m Mod nDo While r <> 0m = nn = rr = m Mod nLoopEnd IfEnd Sub7.Public Sub ws(n%, s%)Dim k%, i%For i = 1 To n - 1If n Mod i = 0 Then s = s + iNext iEnd SubPrivate Sub Form_click()Dim m%, sum%, c$For m = 1 To 999sum = 0Call ws(m, sum)If sum = m Then c = c & sum & " " Next mMsgBox cEnd Sub8.Public Function ef(a(), k%, wz%) As Integer Dim i%, low%, mid%, top%low = LBound(a)top = UBound(a)Do While low <= topmid = (low + top) / 2If k > a(mid) Thenlow = mid + 1ElseIf k = a(mid) Thenwz = midExit FunctionElsetop = mid - 1End IfLoopwz = -1End Function9.Function MaxGY(x As Integer, y As Integer) Dim Temp As IntegerTemp = x Mod yIf Temp = 0 ThenMaxGY = yElseMaxGY = MaxGY(y, Temp)End IfEnd FunctionPrivate Sub Form_click()Dim m%, n%m = Val(InputBox("请输入第一个整数:"))n = Val(InputBox("请输入第二个整数:")) Print "最大公约数是:"; MaxGY(m, n)End Sub10. Function Ss(n%)If n = 1 ThenSs = 10ElseSs = Ss(n - 1) + 2End IfEnd FunctionPrivate Sub Form_click()Dim r%, x%r = 5x = Ss(5)Print "第5个小孩的岁数为:"; xEnd Sub第七章应用程序界面设计一、选择题BCDCA CDBBD CADAA CADBD DAAAB CAA二、填空题1.form1.show unload form12.form2.show form1.hide3.属性方法事件4、工程属性工程属性5.子窗体主窗体6、keypress list(i) additem7.0 list1.listindex list1.listcount-18、1000 true time() 9、AA10、下拉式组合框、简单组合框和下拉式列表框第八章VB图形操作一、选择题DCCDA CDAAA BABBC CAAA二、填空题1、不会也不会 2.、Scaleheight Scalewidth3、(300,-150)4、右上5、twip6、0~157、B 8、-2π~09、逆10、drawwidth11、右上12、move13、cls 14、坐标原点、坐标度量单位、坐标轴的长度与方向15、VB程序设计vbprogramming三、编程题1、Private Sub Command1_Click()Const pi = 3.14159P1.Circle (Val(Text1), Val(Text2)), Val(Text3)End SubPrivate Sub Command2_Click()P1.ClsText1 = ""Text2 = ""Text3 = ""End Sub2、Private Sub Form_Load()Pic1.Picture = LoadPicture("F:\VB程序设计\上课课件\tea.jpg")End SubPrivate Sub Pic1_Click()Pic2.Picture = Pic1.PicturePic1.Picture = LoadPicture("")End Sub3、Dim a1!, b1!Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) a1 = Xb1 = YEnd SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Circle (a1, b1), Sqr((a1 - X) ^ 2 + (b1 - Y) ^ 2)End Sub4、Dim a1!, b1!Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Thena1 = Xb1 = YEnd IfEnd SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 1 ThenLine (a1, b1)-(X, Y), vbGreen, BFEnd IfEnd Sub5、Private Sub Form_Click()Scale (-200, 200)-(200, -200)Dim i!For i = 0 To 140 Step 20Line (-i - 10, i + 10)-(i + 10, -i - 10), , BNext iEnd Sub第9章vb文件操作一、选择题BDDCC BBAAB二、填空题1、驱动器列表框、目录列表框和文件列表框2、change 3\file1.path=dir1.path4、name “D:\old.doc” as “c:\new.doc”5、dir1.path=drive1.drive file1.path=dir1.path6、filecopy “d:\text.doc”,”e:\text.doc”7、eof 8、input not eof(1)9、for inputFor outputnot eof(1)str1Close #110、pattern 11、空12、字节。
VB6.0程序设计实践教程 习题及答案
Vb6.0
第一章 一、判断题 1.属性是 Visual Basic 对象性质的描述,对象的数据就保存在属性中。 2.控件的属性值不可以在程序运行时动态地修改。 3.许多属性可以直接在属性表上设置、修改,并立即在屏幕上看到效果。 4.所谓保存工程,是指保存正在编辑的工程的窗体。 5.面向对象的程序设计是一种以对象为基础,由事件驱动对象执行的设计方法。 6.为了使一个控件在运行时不可见,应该将该控件的 Enabled 属性设置为 False。 7. 保存 Visual Basic 文件时, 若一个工程包含多个窗体或模块, 则系统先保存工程文件, 再分别保存各窗体或模块文件。 8.事件是由 Visual Basic 预先定义的对象能够识别的动作。 9.方法是 Visual Basic 对象可以相应的用户操作。 10.窗体中的控件,是使用工具箱中的工具在窗体上画出的各图形对象。 11.由 Visual Basic 语言编写的应用程序有解释和编译两种执行方式。 12.在打开一个 Visual Basic 工程进行修改后,要另存为一个版本,只需单击“工程另 存为…”就行,因为系统将同时保存其他文件。 13.当用 Load 命令将窗体装入内存时,一定触发窗体的 load 事件和 Activate 事件. 二、选择题 1.要使某个控件不可操作,要对 属性进行设置。 A、Enabled B、Visibale C、Caption D、Name 2.下列不属于对象的基本特征的是 。 A、属性 B、方法 C、事件 D、函数 3.当运行程序时,系统自动执行启动窗体的 事件过程。 A、Click B、Load C、DblClick D、Unload 4.Visual Basic 中“程序运行”允许使用的快捷键是 。 A、F2 B、F5 C、Alt+F3 D、F8 5.改变控件在窗体中的左右位置应修改该控件的 属性。 A、Top B、Left C、Width D、Right 6.工程文件的扩展名为 。 A、.vbp B、.bas C、.frx D、.frm 7.在 Visual Basic 中,称对象的数据为 。 A、属性 B、方法 C、事件 D、封装 8.下列选项中不属于属性的是 。 A、Name B、Caption C、Show D、Text 9.将 Visual Basic 编制的程序保存在磁盘上,至少会产生何种文件 。 A、.doc 与.txt B、.com 与.exe C、.bat 与.frm D、.vbp 与.frm 10.下列关于属性设置的叙述错误的是 。 A.一个控件具有什么属性是 VB 预先设计好的,用户不能改变它 B.一个控件具有什么属性值是 VB 预先设计好的,用户不能改变它 C.一个控件的大多数属性既可以在属性窗口中设置,也可以用程序代码设置 D.一个控件的属性在属性窗口中设置后,还可以再利用程序代码为其设置新值
第6-10章 VB课后参考答案
第6章习题参考答案一、判断题题号 1 2 3 4 5 6答案√√×√√×二、选择题题号 1 2 3 4 5答案 D C A D B三、程序填空题1.(1)nsp=true(2) m mod i=0(3) p1 and p2(4) p1=nsp(i)2.(1)number(i,7)(2) (int n, int m)(3) number=number*10+m四、程序阅读1. a=9 b=42.(1) 362516941(2)1499413.64. 1 4 6五、编写程序1.Public Function fact(x As Integer) As Single '求x!Dim f As Single, i As Integerf = 1For i = 1 To xf = f * i '累积Next ifact = fEnd FunctionPrivate Sub Form_Click()Dim n%, m%, y!Dom% = Val(InputBox("m="))n% = Val(InputBox("n="))Loop Until m >= n And n >= 0y = fact(m) / (fact(n) * fact(m - n))Print yEnd Sub2 private sub Insertfun(a%(), y%)Dim i as integer,n as integern=ubound(a)Redim preserve a(n+1)For i=ubound(a)-1 to 0 step -1If y <a(i) thena(i+1)=a(i)If i=0 then a(i)=yElsea(i+1)=yexit forend ifnext i3. '判断是否为回文函数Private Function Judge(str As String) As BooleanDim i As IntegerJudge = True '假设为回文For i = 1 To Len(str) \ 2 '依次判断对应位置的两个字符,只要有一组不等即不是回文If Mid(str, i, 1) <> Mid(str, Len(str) + 1 - i, 1) ThenJudge = FalseExit FunctionEnd IfNext iEnd Function4.Private Sub Form_Click()Dim n%,x!n = Val(InputBox("n="))x = Val(InputBox("x="))Print fe(n, x) '调用求值函数过程End Sub'求值函数过程Public Function fe(n%, x!) As SingleDim i As Integer, t As Singlefe = 1: t = 1For i = 1 To nt = t * x / i 't为通项fe = fe + tNext iEnd Function5.。
VB程序设计第6章对话框的设计
• MsgBox函数返回值为1~6之间的 一个整数,表示用户按下了“确定”、 “取消”、“终止”、“重试”、“忽略”或 “是”按钮。
VB程序设计第6章对话框的设计
2、通用对话框 (1)通用对话框的使用 把“通用对话框(Common
VB程序设计第6章对话框的设计
(3)“保存”对话框 • “保存”对话框也是在Windows应用
程序中经常用到的。用ShowSave 方法显示对话框,它同样能指定文件 的驱动器、目录、文件扩展名和文件 名,其使用方法和“打开”对话框的使 用方法基本相同。
VB程序设计第6章对话框的设计
(4)“颜色”对话框 • “颜色”对话框用来在调色板中选择颜
对出现,指出对话框左上角的屏幕坐标。 • InputBox函数返回文本框中输入的内容,
为字符串类型。
VB程序设计第6章对话框的设计
(2)消息框 • 创建消息框的标准函数是MsgBox
函数,该函数在对话框中显示消息, 等待用户按下按钮,并返回一个整数 来表示用户按下了哪一个按钮。 • MsgBox函数的语法格式为:
VB程序设计第6章对话框的设计
其中: • Prompt为必需的字符串表达式参数,提
供显示在对话框中的提示文本。 • Title为可选字符串表达式参数,提供标题
显示在对话框的标题栏中。 • Default为可选字符串表达式参数,提供
输入框的缺省输入文本。 • Xpos和Ypos都是可选的数值表达式,成
VB程序设计第6章对话框的设计
• ⑧过滤器索引(FilterIndex属性): 用索引值来指定对话框使用哪一个过 滤器。
• ⑨取消引发错误(CancelError属 性):决定当用户单击对话框上的 “取消”按钮时,是否会显示一个报错 信息的消息框。
VB课后答案 2
第1章一、选择题1-5 CCDBA 6-10ABCDB二、填空题1.bas2.工具栏3属性窗口4.工具栏5.设计状态,运行状态,中断状态三、简答题略四、编程题控件属性属性值FormNameForm1TextBoxNameText1Text1空白CommandButtonNameCommand1Caption确定Private Sub Command1_Click()Command1.Caption = Text1.TextEnd Sub第2章一、选择题1-5 CBDDC 6-10 AAAAC11-14 BBBD二、填空题1. Text1.Text = "Hello!"或Text1= "Hello!" 2.Width 、Height3.Ctrl4. ABCD、Visual Basic5. 123321第3章一、选择题1-5ABDCA 6-10CBCB C 11-15BBBCC 二、根据要求写出表达式1.Int(100*Rnd+1)2.x>10 And x<503.x mod 4=0 Or x mod 13=04.Chr(Int(25*Rnd+65))三、写出下列函数的值1.Int(–3.14159)=-42.Sqr(Sqr(16))=23.Fix(–3.1415926)=-34.Int(Abs(99–100)/2)=05.Sgn(7*3+2)=16.Format(2.3, "##.000")=2.3007.Left("Hello",2)=”He”8.Val("16 Year")=169.Str(–459.65)=”-459.65”四、将下列数学表达式改写为等价的VB表达式1.(b^2-sqr(4*a*c-b^2))/(4*a)2.sqr(sin(3.14/18)^2+cos(3.14/9)^2)+15 3.abs((a+b)^2-4*a*c)4.a^2+exp(4)+sqr(abs(a+exp(1)))五、编程题1.界面设计如图:文本框text1的change事件代码如下:Private Sub Text1_Change()Text2.Text = LCase(Text1.Text)End Sub2.界面设计如图:设置计时器的interval属性值为1000,Timer1的Timer 事件代码如下:Private Sub Timer1_Timer()Text1.Text = TimeEnd Sub第4章一、选择题1-5 DBCCA 6 C二、填空题1.输入字符型val2.Msgbox “是否显示数据”,vbOkCancle+vbQuestion,”提示”3.3445******************三、编程题略第5章一、选择题1-5 DADCDB二、程序填空1.Inputbox 、is<1 、x^3+10*x+2 、-x y=x3+7x2+10 1≤x≤6x+10 x>6-x-5 x<1y=x3+7x2+10 1≤x≤6x+10 x>6-x-5 x<12.3.Rnd 、1 、x mod 5=04.Val(text1.text) 、x mod 2=0 、"奇数" 、End If 三、编程题1.代码如下:Private Sub form_Click()Dim x As Singlex = Val(InputBox("请输入任意一个实数:"))If x < 0 ThenPrint "概数是负数"ElseIf x = 0 ThenPrint "概数是零"ElsePrint "概数是正数"End IfEnd Sub2.代码如下:Private Sub form_Click()Dim x As Single, y As Singlex = val(InputBox("请输入x的值:"))If x < 0 Theny = x ^ 4 + 10ElseIf x < 10 Theny = x ^ 2 - 10ElseIf x < 20 Theny = 5 * x + 8Elsey = x / 2 + 1End IfMsgBox "当x的值为:"& x &"时,函数值y为:"& y End Sub3.运行界面如下:代码如下:Private Sub Command1_Click()Dim x As Single, y As Singlex = Val(Text1.Text)If x < 100 Theny = xElseIf x < 200 Theny = x * 0.95ElseIf x < 300 Theny = x * 0.9Elsey = x * 0.8End IfText2.Text = yEnd Sub4.运行界面如下:代码如下:Private Sub Command1_Click()Dim x As Single, y As Singlex = Val(Text1.Text)If x <= 3 Theny = 8Elsey = (x - 3) * 1.6 + 8End IfText2.Text = yEnd Sub第6章习题答案一、选择题1-5 ABDAB 6-7 CB 二、填空题1.Do……Loop 、While……Wend 、For……Next2.7 153.54.For……Next While……Wend 5.5.6 56.15 4 07.Next j print三、编程题1.(1)代码如下:Private Sub Form_Click()For i = 1 To 5For j = 1 To iPrint "*";Next jPrintNext iEnd Sub(2)代码如下:Private Sub Form_Click()For i = 1 To 5Print Tab(6 - i);For j = 1 To iPrint "*";Next jPrintNext iEnd Sub(3)代码如下:Private Sub Form_Click()For i = 1 To 5For j = 1 To 6 - iPrint "*";Next jPrintNext iEnd Sub(4)代码如下:Private Sub Form_Click()For i = 1 To 5Print Tab(i);For j = 1 To 6 - iPrint "*";Next jPrintNext iEnd Sub2. 输入若干个字符,分别统计其中英文字母、数字、其他字符各有多少个。
最新VB程序设计课后习题答案资料
VB课后习题解答第一、二章:1、简述VB6.0的主要特点答:方便、直观的可视化设计工具;面向对象程序设计方法;事件驱动的编程机制;易学易用的应用程序集成开发环境;结构化程序设计语言;完备的联机帮助功能;强大的多媒体、数据库和网络功能。
2、当正常安装VB6.0后,误把Windows子目录删除。
当重新安装Windows后,是否需要再次安装VB6.0 ?答:要重新安装的。
因为安装VB6.0时,有些VB程序被系统自动安装在Windows目录下,所以一旦删除Windows子目录,就必须重新安装VB6.0。
3、Visual Basic 6.0分为学习版、专业版和企业版,如何知道所安装的是哪个版本?答:在VB6.0的启动界面上,能显示对应的版本。
4、叙述建立一个完整应用程序的过程答:首先建立用户界面的控件对象(简称控件);然后对控件属性进行设置;再对控件事件过程及编程;保存应用程序;程序调试和运行。
5、Visual Basic 6.0有多种类型窗口,若要在设计时看到代码窗口,应怎样操作?答:选择“视图”一>“代码窗口”命令或单击“工程资源管理器”窗口中的“查看代码”按钮。
6、当建立好一个简单的应用程序后,假定该工程仅有一个窗体模块。
试问该工程涉及多少个要保存的文件?若要保存该工程的所有文件,应先保存什么文件?再保存什么文件?若不这样做,系统会出现什么信息?答:涉及两个文件要保存。
先保存窗体文件(.frm),再保存工程文件(.vbp)。
若先保存工程文件,系统也会先弹出“文件另存为”对话框,要求先保存窗体文件。
7、保存文件时,若不改变目录名,则系统默认的目录是什么?答:系统默认的目录是VB98& 安装Visual Basic 6.0后,帮助系统是否也已安装?答:没有。
VB6.0的帮助系统在两张光盘上,即MSDN Library,其中还包含了VC、VFP等其他语言的帮助系统。
9、如何使用Visual Basic 6.0的帮助系统?答:最方便的方法是选定要帮助的内容,然后按F1功能键,这时系统打开MSDN Library查阅器,直接显示与选定内容有关的帮助信息。
VB教程课后习题的答案及解析
=第二章标准控件和属性1.下列可以激活属性窗口的操作是( D )A)用鼠标双击窗体的任何部位 B)执行‘工程’菜单中的‘属性窗口’命令 C)按Ctrl+F4键 D)按F4键2.确定一个控件在窗体上的位置的属性是( D )A)Width或Height B)Width和Height C)Top或Left D)Top和Left3.确定一个窗体或控件的大小的属性是( B )A)Width或Height B)Width和Height C)Top或Left D)Top和Left4.在Visual Basic中,所有标准控件都具有的属性是( B )。
【2013年9月考试真题】A) Caption B) Name C) Text D) Value参考答案:B【解析】VB标准控件中不同控件具有不同属性,有的相同有的不同,但都具有Name属性。
故选B。
5.为了让计时器控件每隔10秒产生了一个Timer事件,应把Interval属性设置为( B )A) 1000 B) 10000 C) 10 D) 1006.下面关于单选按钮和复选框的叙述中正确的是( D )A) 单选按钮被选中则它的Value属性为True;复选框被选中则它的Value属性为2B) 单选按钮被选中则它的Value属性为True;复选框被选中则它的Value属性为TrueC) 单选按钮被选中则它的Value属性为1;复选框被选中则它的Value属性为1D) 单选按钮被选中则它的Value属性为True;复选框被选中则它的Value属性为17.将文本框的ScrollBar设置为2,文本框中却没有垂直滚动条,其原因为( B )A) 文本框的Locked属性值为True B) 文本框的MultiLine属性值为FalseC) 文本框的MultiLine属性值为True D) 文本框中没有内容第三章简单程序设计语句1.在面向对象的程序设计中,可被对象识别的动作称为( B )。
VB课后习题答案汇总
课后习题参考答案第1章Visual Basic 6.0一、判断题1--5:√×√×√ 6――10:××√×√ 11――13:√××二、选择题1――5:ADBBB 6――10:.CACDB三、填空题1、图片框和框架2、对象3、控件屏幕4、Load5、Load Activate四、程序设计题1、Private Sub Command1_Click()Print Text1.TextEnd SubPrivate Sub Command2_Click()EndEnd Sub2、Private Sub Command1_Click()Form1.FontSize = Form1.FontSize + 3Print "青春无悔"End SubPrivate Sub Command2_Click()FontSize = FontSize - 3Print "青春无悔"End SubPrivate Sub Command3_Click()Form1.FontBold = TruePrint "青春无悔"End SubPrivate Sub Command4_Click()FontBold = FalsePrint "青春无悔"End Sub3、Private Sub Command1_Click()Text1.Visible = FalseEnd SubPrivate Sub Command2_Click()Text1.Visible = TrueText1.FontName = "楷体_gb2312"Text1.FontSize = 15Text1.Text = "我学会了创建一个应用程序了!"End Sub第二章判断题:1~5错错错错错第3章顺序结构程序设计一、思考题略二、程序阅读题1.A=2 B=1 C=22.123.BABCBACBC4. 1 2 False5.1-12-123-1234-三、选择题1.B2.D3.B4.B5.D6.C四、程序设计题1.Private sub form_click()Dim r!,s!R=val(inputbox(“r=”))S=3.14159*r*rPrint “s=”;sEnd sub2.Private sub form_click()Dim x as singleX=val(inputbox(“x=”))Print fix(x*x*1000)/1000,fix(sqr(x)*1000)/1000End sub第4章选择结构程序设计一、判断题1.错2.错3.错4.错5.对二、选择题1.A 2. D 3.A 4.B 5.A 6.A三、程序阅读题1.X=3 y=72.w=4w=13w=135w=313.X_`a12 Uvw&4.12 9四、程序填空题1.(1) chr(keyascii) (2) chr(keyascii)=”)”(3)count1=count1-1 (4)count1=0 (5)count1>02.(1) max (2)min (3)c>max (4)c<min3.(1)x mod 3=2 and x mod 5=3 and x mod 7=4 then(2)text1.setfocus (3) text1.selstrat=0 (4)text1.sellength=len(text1.text)五、1.Private Sub Command1_Click()Dim y As Integery = Val(InputBox("y="))If (y Mod 4 = 0 And y Mod 100 <> 0) Or (y Mod 400 = 0) Then Print "是闰年"ElsePrint "不是闰年"End IfEnd SubPrivate Sub Command1_Click()Dim y As Integer,y1 as stringy = Val(InputBox("y="))If (y Mod 4 = 0 And y Mod 100 <> 0) Or (y Mod 400 = 0) Then Y1= "是闰年"ElseY1= "不是闰年"End If? y,y1End SubPrivate Sub Command1_Click()Dim y As Integer,y1 as stringy = Val(InputBox("y="))Y1= "不是闰年"If (y Mod 4 = 0 And y Mod 100 <> 0) Or (y Mod 400 = 0) Then Y1= "是闰年"End If? y,y1End Sub2.Private Sub Command1_Click()Dim a%, b%, c%, x!,s!a = Val(InputBox("a="))b = Val(InputBox("b="))c = Val(InputBox("c="))If a + b > c And a + c > b And b + c > a Thenx = (a + b + c) / 2s = Sqr(x * (x - a) * (x - b) * (x - c))Print "s="; sElsePrint "不是三角形,重输a,b,c"End IfEnd SubPrivate Sub Command1_Click()Dim a%, b%, c%, x!,s!,t%a = Val(InputBox("a="))b = Val(InputBox("b="))c = Val(InputBox("c=")) ‘a,b,c=3,4,5t=0If a + b > c And a + c > b And b + c > a Thenx = (a + b + c) / 2s = Sqr(x * (x - a) * (x - b) * (x - c))t=1End IfIf t=0 then? “不是“endifEnd Sub3.Private Sub Command1_Click()Dim x!, y!x = Val(InputBox("x="))y = x * x - x – xIf x < 0 And x <> -3 Theny = x * x + x - 6ElseIf 0 <= x And x < 10 And x <> 2 And x <> 3 Then y = x * x - 5 * x + 6End IfPrint "x="; x, "y="; yEnd SubPrivate Sub Command1_Click()Dim x!, y!x = Val(InputBox("x="))If x < 0 And x <> -3 Theny = x * x + x – 6elseIf 0 <= x And x < 10 And x <> 2 And x <> 3 Theny = x * x - 5 * x + 6elsey=x^2-x-1End IfPrint "x="; x, "y="; yEnd SubPrivate sub form_click()Dim y%,z%,dj%,p!,t!Y=val(inputbox(“月”))Z= val(inputbox(“订票数”))Dj= val(inputbox(“票价”))Select case ycase 7,8,9If z>=20 then p=0.15 else p=0.05case 1 to 5,10,11If z>20 then p=0.3 else p=0.2Case elseP=0.2End selectT=z*dj*p? tend sub5.private sub form_click()dim m1%,m2%,m3%,dj$m1=val(inputbox(“m1=”)): m2=val(inputbox(“m2=”)): m3=val(inputbox(“m3=”))if (m1+m2+m3)/3>=95 or ( a=100 and b=100 and c>=80) or ( a=100 and c=100 and b>=80) or ( c=100and b=100 and a>=80) thendj=”一等”elseif (m1+m2+m3)/3>=90 or ( a=100 and b>=75 and c>=75) or ( b=100 and a>=75 and c>=75) or ( c=100 and b>=75 and a>=75) thendj=”二等”elseif a>=75 and b>=75 and c>=75 thendj=”三等”elsedj=”没有”endif? “dj=”;djend sub第5章 循环结构程序设计1.判断题×√√√××√×2.选择题BBBBCC3.填空题(1)7(2)Do/Loop For/Next While/Wend(3)-364.程序阅读题(1) (2)(3)4 (4)5.程序填空题(1)①sign=1 ②For i=2 to 19(2)①Len(str1) ②length-1 ③Mid(str1,i,2) ④Sum=0(3)①I mod 7=5 and i Mod 5 = 3 And i Mod 3=2 ②i<1000(4)①last_one = last_two ②last_two = this_one ③i=i+1 ④i-2(5)①I>int(sqr(m)) ②Int(Sqr(n-m))6.编程题(1)Private Sub Form_Click()Dim i As Integer, j As IntegerDim s As Double, Sum As Doubles = 188 68861 2 3 4 Sum=10# # # # # ## # # # ## # # ## # ## ##For i = 1 To 11 Step 2For j = 1 To is = s * jNext jSum = Sum + sNext iPrint SumEnd Sub(2)Private Sub Form_Click()Dim m%, n%, mn%, r%, t%m = Val(InputBox("m="))n = Val(InputBox("n="))If n <= 0 Or m <= 0 ThenMsgBox "数据有误"EndEnd Ifmn = m * nIf m < n Thent = m: m = n: n = tEnd IfDo While (n <> 0)r = m Mod n: m = n: n = rLoopForm1.Print "最大公约数="; mForm1.Print "最小公倍数="; mn / m End Sub(3)Private Sub Form_Click()Dim i%, j%For i = 1 To 5Print Spc(20 - i);For j = 1 To 2 * i - 1Print Trim(Str(i));Next jPrintNext iFor i = 4 To 1 Step -1Print Spc(20 - i);For j = 1 To 2 * i - 1Print Trim(Str(10 - i));PrintNext iEnd Sub(4)Private Sub Form_Click()Dim x As IntegerDim y As IntegerDim z As IntegerFor x = 0 To 100For y = 0 To 100 - xz = 100 - x - yIf 5 * x + 3 * y + z / 3 = 100 Then Print x, y, zNext yNext xEnd Sub(5)Private Sub Form_Click()Dim x As Single, y As Single, a As Single, i As Integerx = Val(InputBox("输入x:"))a = 1: y = a: i = 0While a >= 0.00001i = i + 1: a = a * x / i: y = y + aWendPrint "y="; yEnd Sub第六章答案一、判断题1.错2.错3.错4.错(可以为小数,不过仍然是当作整数来处理)5.错6.错二、选择题1.C2.C3.A4.D三、程序填空1.(1)N-1 (2)T = A(J): A(J) = A(J + 1): A(J + 1) = T(3)Print A(I)2.(1)a(i, j) = 1 (2)Print四、程序阅读1.52.1 2 3 41 2 3 41 2 3 41 2 3 41 1 1 12 2 2 23 3 3 34 4 4 43.(前面空4个空格)1 12 11 102 13 16 93 14 15 84 5 6 7第7章过程判断题:1、对2、错3、错4、错5、对6、对7、错选择题:1.B 2、D 3、D 4、C 5、A程序阅读题:1、y=5 a=52、n=1 z=4y=15 a=10 n=2 z=7y=30 a=15 n=3 z=103、32 644、2 2 216 648 644 645、A1=10 B1=20A2=20 B2=10程序填空题:1、(1)hwstring(str) (2)"" (3)As Boolean(4)n=len(strtxt) (5) True (6)False2.(1)Xsch(Nman,Nkcen) (2)n(3)sum=sum+x(i,j) (4)tt/n (5)x(i,m+1)<1.2*ver and x(i,m+1)>=1.1*ver 3.(1)number(i)(2)Byval n as Integer (3)number= number+7*10^ (i-1)4.(1)temp=1 (2)temp (3)nFactor( i ) (4)sum 程序设计题:1、具体程序为:Function Maxnum(ByRef a() As Integer, ByVal n As Integer) As IntegerDim i As IntegerMaxnum = a(1)For i = 2 To nIf a(i) > Maxnum Then Maxnum = a(i)Next iEnd FunctionFunction Avenu(ByRef a() As Integer, ByVal n As Integer) As SingleDim i As Integer, sum As IntegerFor i = 1 To nsum = sum + a(i)Next iAvenu = sum / nEnd FunctionSub Sortrnum(ByRef a() As Integer, ByVal n As Integer)Dim i As Integer, j As Integer, k As IntegerDim temp As IntegerFor i = 1 To nk = iFor j = i + 1 To nIf a(k) > a(j) Then k = jNext jIf i <> k Thentemp = a(i)a(i) = a(k)a(k) = tempEnd IfNext iEnd SubPrivate Sub Form_Click()Dim b(10) As Integer, i As IntegerFor i = 1 To 10b(i) = InputBox("请输入整数", "输入")Next iCall Sortrnum(b(), 10)Print "从小到大排序为:"For i = 1 To 10Print b(i);NextPrintPrint "最大数为:"; Maxnum(b(), 10)Print "平均数为:"; Avenu(b(), 10)End Sub2、具体程序为:Function fe(n%, x!) As SingleDim temp As Single, i As Integerfe = 1temp = 1For i = 1 To ntemp = temp * x / ife = fe + tempNext iEnd FunctionPrivate Sub Form_Click()Dim n As Integer, x As Singlen = Val(InputBox("请输入n", "输入"))x = Val(InputBox("请输入x", "输入"))Print "e的值为:"; fe(n, x)End Sub3、具体程序为:Sub sort(a() As Single, n As Integer)Dim i As Integer, j As Integer, k As Integer Dim temp As SingleFor i = 1 To nk = iFor j = i + 1 To nIf a(k) < a(j) Then k = jNext jIf i <> k Thentemp = a(i)a(i) = a(k)a(k) = tempEnd IfNext iEnd SubPrivate Sub Form_Click()Dim a(10) As Single, i As IntegerFor i = 1 To 10a(i) = Val(InputBox("请输入数据", "输入")) Next iCall sort(a(), 10)Print "从大到小为:"For i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext iEnd Sub第8章 常用控件参考答案:一、判断题1、×2、×3、√4、√5、×6、×7、×8、×9、× 10、×11、× 12、√ 13、× 14、√ 15、√二、单选题1、A2、B3、A4、C5、B6、D7、A8、B9、C 10、C11、A 12、A 13、C 14、B 15、A三、填空题1、Autosize WordWrap2、Enabled Visible3、文本框和列表框4、拖动滚动框5、Change6、Command1.Setfocus7、Timer8、MaxLength 9、AddItem 10、定时器屏蔽123 45.五、程序填空题1、(1) Label1.left (2) –Label1.Width2、(1) List1.ListIndex<0 (或者List1.ListIndex = -1) (2) List1.RemoveItem List1.ListIndex(3) Text1.Text=”” (4) List1.AddItem Text1.Text3、(1) 1 to 2*I-1 (2) Command2.Enabled=True(3) Command2.Enabled=False4、(1) ndec<>0 (2) ndec mod 16 (3) ai=ndec(4) end (5) ai & shex (6)shex5、(1) Timer1.Enabled=True (2) x \ 3600 (3) (x-h*3600)\60(4) x=x+16、(1) m<len(str1)/2 (2) mid (str1,m+1,1) (3)mid (str1,len(str1)-m,1)六、程序设计题1、 Private Sub Command1_Click()If Text1.Text = "" ThenMsgBox "请先输入添加项"ElseList1.AddItem Text1.TextEnd IfEnd SubPrivate Sub Command2_Click()If List1.ListIndex < 0 Then ‘或者if list1.listindex=-1 then MsgBox "请选择一删除项"ElseList1.RemoveItem List1.ListIndexEnd IfEnd SubPrivate Sub Command3_Click()If List1.ListIndex < 0 ThenMsgBox "请选择一修改项"ElseText1.Text = List1.TextEnd IfCommand4.Enabled = TrueEnd SubPrivate Sub Command4_Click()Dim a As IntegerIf List1.Text = Text1.Text ThenMsgBox "请先修改此项"Elsea = List1.ListIndexList1.RemoveItem List1.ListIndexList1.AddItem Text1.Text, aEnd IfEnd SubPrivate Sub Form_Load()Command4.Enabled = FalseEnd Sub2、 Dim a As IntegerPrivate Sub Command1_Click()a = Int(Rnd * 100) + 1End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)Static i As IntegerIf KeyAscii = 13 ThenIf Val(Text1.Text) > a ThenLabel1.Caption = "太大了,请重输"i = i + 1ElseIf Val(Text1.Text) = a ThenLabel1.Caption = "恭喜你,你答对了!"i = 0Command1_ClickElseLabel1.Caption = "太小了,请重输!"i = i + 1End IfIf i > 10 ThenMsgBox "你失败了!"EndEnd IfEnd IfEnd Sub3、Private Sub Command1_Click()Dim n As Integer: n = Len(Text1.Text)Dim s(1 To 1000) As String, i As Integer, j As Integer, t As String,a As StringFor i = 1 To ns(i) = Mid(Trim(Text1.Text), i, 1)Next iFor i = 1 To n - 1For j = i + 1 To nIf s(i) > s(j) Thent = s(i): s(i) = s(j): s(j) = tEnd IfNext j, iFor i = 1 To n: a = a + s(i): Next iPrint aCommand1.Enabled = False: Command2.Enabled = TrueEnd SubPrivate Sub Command2_Click(): End: End SubPrivate Sub Form_Load()Text1.MaxLength = 200Command2.Enabled = False: Command1.Enabled = TrueEnd Sub4、Dim a(1 To 1000) As String, b(1 To 1000) As String, c(1 To 1000) As Double, d(1 To 1000) As DoublePrivate Sub Combo1_click()Text1.Text = a(Combo1.ListIndex + 1)Text2.Text = b(Combo1.ListIndex + 1)Text3.Text = c(Combo1.ListIndex + 1)Text4.Text = d(Combo1.ListIndex + 1)End SubPrivate Sub Command1_Click()Combo1.AddItem Text1.Texta(Combo1.ListCount) = Text1.Textb(Combo1.ListCount) = Text2.Textc(Combo1.ListCount) = Val(Text3.Text)d(Combo1.ListCount) = Val(Text4.Text)End SubPrivate Sub Command2_Click()For i = Combo1.ListIndex + 1 To Combo1.ListCount - 1a(i) = a(i + 1)b(i) = b(i + 1)c(i) = c(i + 1)d(i) = d(i + 1)Next ia(i) = ""b(i) = ""c(i) = 0d(i) = 0Combo1.RemoveItem Combo1.ListIndexText1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""End SubPrivate Sub Form_Load()Dim i As IntegerCombo1.AddItem "张三"Combo1.AddItem "李四"Combo1.AddItem "王五"a(1) = "张三": a(2) = "李四": a(3) = "王五"b(1) = "浙江林学院": b(2) = "浙江工业大学": b(3) = "浙江大学"c(1) = : c(2) = : c(3) =d(1) = : d(2) = : d(3) =End Sub5、Private Sub Check1_Click()If Check1.Value = 1 ThenLabel1.FontBold = TrueElseLabel1.FontBold = FalseEnd IfEnd SubPrivate Sub Check2_Click()If Check1.Value = 1 ThenLabel1.FontItalic = TrueElseLabel1.FontItalic = FalseEnd IfEnd SubPrivate Sub Option1_Click()Label1.FontName = "宋体"End SubPrivate Sub Option2_Click()Label1.FontName = "楷体_gb2312"End Sub6、Private Sub Command1_Click()Dim a As String, i As Integer, n As IntegerDim s As Integer, q As Integer, l As Integern = Len(Text1.Text)For i = 1 To na = Mid(Text1.Text, i, 1)If a >= "a" And a <= "z" Thens = s + 1 '大写个数ElseIf a >= "A" And a <= "Z" Thenq = q + 1 '小写个数ElseIf a >= "0" And a <= "9" Thenl = l + 1End IfNext iText2.Text = sText3.Text = qText4.Text = lEnd SubPrivate Sub Form_Load()Text1.SetFocusEnd Sub7、Private Sub Form_Click()Dim i As Integer, j As Integer, t As IntegerFor i = 0 To List1.ListCount - 1List3.AddItem List1.List(i)Next iList1.ClearDo While List2.ListCountList3.AddItem List2.List(0)List2.RemoveItem 0LoopFor i = 0 To List3.ListCount - 2For j = i + 1 To List3.ListCount - 1If List3.List(i) > List3.List(j) Thent = List3.List(i): List3.List(i) = List3.List(j): List3.List(j) = tEnd IfNext j, iEnd SubPrivate Sub Form_Load()Dim i As Integer, n As Integern = InputBox("")For i = 1 To nList1.AddItem Int(Rnd * 90) + 10List2.AddItem Int(Rnd * 90) + 10Next iEnd Sub第9章对话框和菜单1.判断题:×√√(题中应把flag改为flags)√√××√√×××√××2.选择题:DDCCD BDCCB3.填空题1.-2.一级子菜单3.将顶级菜单设置为不可见4.popupmenu pmenu,0,x,y5.showfont6.工程、部件7.click8.代码窗口,click9.下拉式,弹出式打开/另存为、颜色,字体、打印、帮助4. 程序阅读题打开对话框2、*.txt3、allfiles*.exe*.txt*.doc4、Checking,No File Selectd,重试(R),取消5. 程序填空题“c:\winnt”commondialog1.showopen,loadpicture(commondialog1.filename)6. 程序设计题1、界面如图:代码如下:Private Sub Command1_Click()CommonDialog1.FileName = ""CommonDialog1.Flags = 4096CommonDialog1.Action = 1If CommonDialog1.FileName = "" ThenMsgBox "No File Selectd", 5 + vbExclamation, "Checking"ElseList1.AddItem CommonDialog1.FileNameEnd IfEnd Sub2、代码如下:Private Sub a_Click()CommonDialog1.ShowColorText1.ForeColor = CommonDialog1.ColorEnd SubPrivate Sub b_Click()CommonDialog1.Flags = 1CommonDialog1.ShowFontText1.FontSize = CommonDialog1.FontSizeEnd SubPrivate Sub c_Click()CommonDialog1.Flags = 1CommonDialog1.ShowFontText1.FontName = CommonDialog1.FontNameEnd SubPrivate Sub d_Click()EndEnd Sub第10章图形操作和图形控件一、判断题×√×××√×√××二、选择题BAADACAA三、填空题(1)pic1.Circle (pic1.ScaleLeft + pic1.ScaleWidth / 2, pic1.ScaleTop + pic1.ScaleHeight / 2), 700(2)在图片框picture1中,以(800,1000)为圆心画一个半径为500的圆(3)Form1.Scale (-200, 250)-(300, -100)(4)Autosize stretch四、程序阅读题1、窗体宽和高的1/4为起点,画一个边长为1000的正方形,并画出其正向对角线2、在图片框中交替画出实心和空心的矩形,空心矩形是黄色边框。
VB教材习题_参考答案1-6章
Visual Basic程序设计教程(李杰主编清华大学出版社 2011.2)习题答案习题2参考答案2.2 单选题1、B2、C3、A4、D5、A6、B7、D8、C9、B 10、D2.3 填空题1、事件驱动2、Cancel3、Name4、Private Sub Form1_Click()5、运行程序6、代码窗口7、Text1.Text = "我是一个学生"8、LostFocus()事件9、Label1.Caption = "请输入用户名"Label1.ForeColor = vbGreen10、(Text1.Text)*22.4 事件练习题1、①该事件的对象是标签Label1,单击标签触发该事件。
②缺省Label1对象的代码是针对窗体,其余有Label1对象的代码是针对标签的。
③Label1.AutoSize = True语句使标签的大小自动随文字的多少而改变。
2、程序运行后,单击命令按钮,在Text1文本框显示"知音海外版",在Text2文本框显示"计算机世界"。
3、单击“显示文本框”命令按钮触发的事件过程Private Sub Command1_Click()Text1.Visible = TrueText1.ForeColor = vbRedText1.Text = "VB程序设计"End Sub单击“隐藏文本框”命令按钮触发的事件过程Private Sub Command2_Click()Text1.Visible = FalseEnd Sub习题3参考答案3.2 单选题1 C2 C3 D4 B5 B6 B7 B8 C9 D3.3填空题1.REM(或‘) _ :2 双引号(“”)#3 $ %4 UCASE()5.(log(1+d*d)-exp(2))^(5/2) [ 还有一种理解方式:(log(1+d*d)-e*e)^(5/2)]6. &O113 &H4B习题4参考答案4.2 单选题BDCBD4.3 填空题1.MsgBox("是否删除? ", vbOkCancel + vbQuestion, "删除")2.The Length of 12345=53. 变量或表达式4. 15. loop4.4 程序设计题1.Private Sub Command1_Click()If Text1.Text = "asdf" ThenMsgBox "密码正确!"ElseMsgBox "密码错误", vbRetryCancel, "错误"End IfEnd Sub2. < 题目中的“连续产生n*n个字母”应该改为“连续产生n个字母”> Private Sub Command1_Click()Dim n%, y%, i%n = Val(Text1.Text)If n > 10 Or n<=0 ThenMsgBox "数据超界!"Text1.Text=””Text1.SetFocusElsey = Int((90 - 65 + 1) * Rnd) + 65For i = 1 To nPrint Chr(y + i)Next iEnd IfEnd Sub3.Private Sub Form_Click()Dim a!, b!, c!, x1!, x21, d!a = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)d = b * b - 4 * a * cIf d >= 0 Thenx1 = (-b + Sqr(d)) / (2 * a)X2 = (-b - Sqr(d)) / (2 * a)Print x1; X2ElsePrint "没有实根"End IfEnd Sub4.Private Sub Form_Click()Dim i%, j%, s%, n%,q%n = InputBox("enter n:")q = 0For i = 1 To ns = 0For j = 1 To is = s + jNext jq = q + sNext iPrint qEnd Sub5.Private Sub Form_Click()Dim i%, j%, n%, k%n = 0For i = 1 To 3For j = 1 To 3For k = 1 To 3Print i & j & k,n = n + 1If n Mod 5 = 0 Then PrintNext kNext jNext iPrintPrint "n="; nEnd Sub6.Private Sub Form_Click()Dim i%, j%, n%, k%For i = 1 To 9For j = 0 To 9For k = 0 To 9n = i * 100 + j * 10 + kIf n = i ^ 3 + j ^ 3 + k ^ 3 Then Print n,Next kNext jNext iEnd Sub习题5参考答案5.2 单选题1 A2 B3 C4 C5 D6 D7 A8 C9 D 10 A 11 A 12 B 5.3 填空题1.False2.Caption3.0 ListCount-14.Print List1.List(3)5.Combo1.ReMoveItem 36.LargeChange7.Timer8.5009.Shape10.Borderstyle5.4 程序设计题1.Dim op1, op2, result As SingleDim operator As IntegerPrivate Sub Command1_Click(Index As Integer)If Index >= 0 And Index <= 10 ThenIf txt1.Text = "" Thentxt1.Text = CStr(Index + 1)Elsetxt1.Text = txt1.Text + CStr(Index + 1) End IfEnd IfIf Index >= 12 And Index <= 15 Thenop1 = CDbl(txt1.Text)operator = Indextxt1.Text = ""End IfIf Index = 11 Thenop2 = CDbl(txt1.Text)Select Case operatorCase 12result = op1 + op2Case 13result = op1 - op2Case 14result = op1 * op2Case 15result = op1 / op2End Selecttxt1.Text = CStr(result)End IfEnd SubPrivate Sub Command2_Click()op1 = 0op2 = 0result = 0txt1.Text = ""End SubPrivate Sub Command3_Click()EndEnd Sub2.Private Sub chkBold_Click()If chkBold.Value = vbChecked ThenTxt1.FontBold = TrueElseTxt1.FontBold = FalseEnd IfEnd SubPrivate Sub chkUnderline_Click()If chkUnderline.Value = vbChecked Then Txt1.FontUnderline = TrueElseTxt1.FontUnderline = FalseEnd IfEnd SubPrivate Sub Opt18_Click()Txt1.FontSize = 18End SubPrivate Sub Opt22_Click()Txt1.FontSize = 22End Sub3.Private Sub Form_Load()List1.AddItem "上海"List1.AddItem "北京"List1.AddItem "天津"List1.AddItem "广东"List1.AddItem "辽宁"List1.AddItem "陕西"List1.AddItem "河南"List1.AddItem "深圳"List1.AddItem "四川"List1.AddItem "重庆"End SubPrivate Sub List1_DblClick()For i = 0 To 9If List1.ListIndex = i ThenList2.AddItem List1.List(i)List1.RemoveItem iEnd IfNext iEnd Sub4.Private Sub Command1_Click()Timer1.Enabled=TrueTimer1.Tag=Text1.Text*60Frame1.Caption=“现在开始倒计时”End SubPrivate Sub Timer1_ Timer()Timer1.Tag=Timer1.Tag-1If m<0 ThenTimer1.Enabler=FalseMsgBox“预定的时间到了!”,0,“倒计时”Frame1.Caption=“请输入计时的分钟数:”Text1.Text=0Exit SubEnd Ifn1=Format(m Mod 60,“00”)n2=Format(m \ 60)Mod 60,“00:”)n3=Format(m \ 3600),“00:”)Text1.Text=n3&n2&n1End Sub习题6参考答案6.1单选题1.C2.C3.B4.B5.B6.D7.B8.B9.A 10.A6.2 填空题1.Option Base 12.变体类型3.preserve (注意:教材上面写成perserve, 教材错误)4.Variant变体类型5.96.相同,索引号7.1).通过复制粘贴的方式创建控件数组2).在设计时候添加多个同类型的类型控件,然后通过“属性”窗口将这些控件的名称改为相同,并把Index属性设为不同的下标值。
计算机VB第6章课后参考答案思考题与习题
第6章思考题与习题一.思考题(解略)1.在Windows程序中,用户和计算机程序的交互过程主要表现在哪些方面?在VB中是通过什么途径提供用户交互的?2.如何实现弹出式菜单的设计和执行?3.在这本章我们介绍了剪切板的一些概念,如何在VB程序中使用剪切板?如果不是文本文件,那么应该如何使用剪切板?4.在键盘事件中,KeyPress和KeyDown及KeyUp之间有什么区别?在什么情况下使用KeyPress事件?5.如何确定鼠标是哪个按钮被按下?如果多个程序都使用鼠标,系统应该如何设计?6.如果需要编写一个程序,判断输入的单词是否是VB的关键字,那么应当如何设计这个程序?二.填空题1.mouseup*2.产生对象.Dragover事件*3.一个对象的DragMode属性要么是1,要么是0,它在属性窗口或程序代码中被设置。
4.MouseMove5.指针类型,17 996,Shift*7.拖放8.move9.KeyPress,对应按键的ASCII码。
KeyDown或KeyUp,Shift10.Click,当前窗体的输出位置11.右键,菜单12.AL T13.Visible,Enabled14.Click,115.Enabled False16.连接符”-”17.518.Clipboard.Clear19.文本,Clipboard.GetText20.PopupMenu,Call PopupMenu 菜单名称上机实验一.在一个窗体上放置一个文本框,向该文本框输入0~15(其他数提示为输入错误,要求重新输入),根据在文本框中的数字显示不同的鼠标指针。
Private Sub Text1_Change()If V al(Text1.Text) >= 0 And V al(Text1.Text) <= 15 ThenMe.MousePointer = V al(Text1.Text)ElseMsgBox "数据输入错误,请输入0-15的数"Text1.SelStart = 0Text1.SelLength = Len(Text1.Text)End IfEnd Sub二.设计一个程序,从键盘上读入一个字符,如果是在0~9,a~z之间,直接存入该字符到窗口中的文本框中,并给出它们对应的ASCII码,如果是A~Z,则将其转换为小写后在文本框中显示原来的字符和转换后小写的字符及其ASCII码,如果是其他字符,直接在文本框中显示提示信息“您输入亚的是字母和数字以外的符号”。
VB书课后题答案 第2、3、6章习题
8lsb2.6 习题p411. 下列都属于Visual 数据类型的是___________。
A.Short、Int、Long、Single、DoubleB.Short、Integer、Long、Float、DoubleC.Integer、Long、Single、Double、DecimalD.Boolean、Byte、Bit、Decimal、Date2. 以下定义常量不正确的语句是____________。
A.Const Num as Integer=10B.Const Num as Long=23.4,Str$=""C.Const Str$=""D.Const Str$=# #3. 使用数值类型数据时,系统有时为什么会提示“溢出”错误?4. 什么是变量的隐式声明和显示声明?5. 根据条件写出相应的表达式。
(1)产生“A”~“Z”范围内的一个字符。
Int(rnd()*26+65)(2)产生150~268(包括150和268)范围内的一个正整数。
Int(rnd()*119+150)(3)产生100~999范围内的一个正整数,并判断是否能整除5和7。
X=int(rnd()*900+100)If x mod 5 And x mod 7 thenMsgBox( x & “能被5 和7 整除”)ElseMsgBox( x & “不能被5 和7 整除”)Endif(4)将任意一个2位数的个位与十位分离。
P30(5)产生“a”~“n”范围内的一个小写字母,并转换为大写字母。
Ucase(Chr(Int(Rnd()*14+97)))(6)将一个字符串中去掉左右的空格,再将首字母和末尾字母一起构成一个新的字符串。
(7)表示年龄age在15~30岁之间的女生。
Age >=15 And age<=30 And 性别=”女”6. 编写程序,将系统当前的时间和日期通过Label控件显示。
VB程序设计课后习题答案
VB课后习题解答第一、二章:1、简述VB6.0的主要特点答:方便、直观的可视化设计工具;面向对象程序设计方法;事件驱动的编程机制;易学易用的应用程序集成开发环境;结构化程序设计语言;完备的联机帮助功能;强大的多媒体、数据库和网络功能。
2、当正常安装VB6。
0后,误把Windows子目录删除。
当重新安装Windows后,是否需要再次安装VB6。
0?答:要重新安装的。
因为安装VB6。
0时,有些VB程序被系统自动安装在Windows目录下,所以一旦删除Windows子目录,就必须重新安装VB6。
0。
3、Visual Basic 6。
0分为学习版、专业版和企业版,如何知道所安装的是哪个版本?答:在VB6.0的启动界面上,能显示对应的版本.4、叙述建立一个完整应用程序的过程答:首先建立用户界面的控件对象(简称控件);然后对控件属性进行设置;再对控件事件过程及编程;保存应用程序;程序调试和运行。
5、Visual Basic 6。
0有多种类型窗口,若要在设计时看到代码窗口,应怎样操作?答:选择“视图"—>“代码窗口”命令或单击“工程资源管理器”窗口中的“查看代码"按钮。
6、当建立好一个简单的应用程序后,假定该工程仅有一个窗体模块。
试问该工程涉及多少个要保存的文件?若要保存该工程的所有文件,应先保存什么文件?再保存什么文件?若不这样做,系统会出现什么信息?答:涉及两个文件要保存.先保存窗体文件(.frm),再保存工程文件(.vbp)。
若先保存工程文件,系统也会先弹出“文件另存为”对话框,要求先保存窗体文件。
7、保存文件时,若不改变目录名,则系统默认的目录是什么?答:系统默认的目录是VB988、安装Visual Basic 6。
0后,帮助系统是否也已安装?答:没有。
VB6.0的帮助系统在两张光盘上,即MSDN Library,其中还包含了VC、VFP等其他语言的帮助系统。
9、如何使用Visual Basic 6.0的帮助系统?答:最方便的方法是选定要帮助的内容,然后按F1功能键,这时系统打开MSDN Library查阅器,直接显示与选定内容有关的帮助信息。
vb课后习题答案(红色参考书)
第一章VB开发环境与VB概述习题答案习题一、选择题1. B2. A3. A4. B5. D6. B7 A.8. A9. C10.B11. C12. D13. A14. D第二章常用控件与界面设计习题答案习题一、选择题1. C2.B3.C4.A5.A6.B7.B8.B9.C 10.C 11.B 12.B 13.D 14.A 15.B 16.B 17.B 18.C 19.C 20.D21.B 22.B 23.B 24.C 25.C26.C27. A28. B29.B 30.C 31.B 32.C 33.B 34.B35.D 36.B 37.D 38.D 39.B40.A 41.B 42.A 43.B44.B 45.D 46.A 47.A 48.B49.D 50.C 51.D 52.D 53.C54.D 55.D 56.C 57.A 58.C 59.C 60.B 61.B第三章实验与习题实验实验3_1‘算术运算Private Sub Command1_Click()Const a As Single = 3, b As Single = 2, c As Single = -1, d As Single = 6Dim x As Single, y As Single, z As Single, m As Singlex = 2: y = 1: z = 1: m = 1List1.ClearList1.AddItem a / (b + c / b)List1.AddItem (x + Sqr(x ^ 2 + 1)) ^ (1 / 3)List1.AddItem ((2 * y) / (a * x + b * y) * (a * x - b * y))List1.AddItem 1 + 1 / (1 + 1 / m)List1.AddItem 1 / 2 * (d / 3) ^ (2 * x)List1.AddItem Log(y + Cos(x) ^ 2)List1.AddItem Abs((Exp(x) + Sin(x) ^ 3) / (x + y))List1.AddItem Log((Exp(x * y) + Abs(1 / Tan(z) + Cos(x) ^ 3)) / (x + y - z))List1.AddItem Sin(45 * 3.1415 / 180) + (Exp(10) + Log(10)) / Sqr(x + y + 1)List1.AddItem Log(a) / Log(b)End Sub‘关系运算Private Sub Command2_Click()List1.ClearList1.AddItem Mid("Visual Basic", 1, 12) = Right("Language Visual Basic", 12)List1.AddItem "ABCRG" >= "abcde"List1.AddItem Int(134.69) >= CInt(134.9)List1.AddItem 78.9 / 32.77 <= 97.5 / 43.87 And -45.4 > -4.98List1.AddItem Str(32.345) = CStr(32.345)End Sub‘逻辑运算Private Sub Command3_Click()Dim x As Integer, y As IntegerDim C1 As Integer, C2 As Integer, C3 As IntegerC1 = 70: C2 = 80: C3 = 90x = 9: y = -2List1.ClearList1.AddItem x + y < 10 And x - y > 0List1.AddItem (x% >= 0 And y% >= 0) Or (x% < 0 And y% < 0)List1.AddItem x = 0 Xor y = 0List1.AddItem C1 + C2 + C3 >= 255 Or C1 > 90 And C2 > 90 And C3 > 80End Sub实验3_2略实验3_3略实验3_4‘零售结算Private Sub Command1_Click()Dim dj As Single, sl As Single, je As Single, n As IntegerLabel1.Caption = "金额:"dj = InputBox("请输入商品的单价:", "单价框")sl = InputBox("请输入商品的数量:", "数量框", 1)je = dj * sln = MsgBox("共计金额=" & je, vbOKCancel + vbInformation, "结果")If n = vbOK Then Label1.Caption = "金额:" & Str(je) & "元"End Sub‘产生随机数Private Sub Command2_Click()Dim m As Integer, n As IntegerDim x1 As Integer, x2 As Integer, x3 As Integerm = Val(Text1.Text): n = Text2.TextRandomizex1 = Int(Rnd * (n - m + 1) + m)x2 = Int(Rnd * (n - m + 1) + m)x3 = Int(Rnd * (n - m + 1) + m)Label4.Caption = "结果为:" & Str(x1) & Str(x2) & Str(x3)End Sub‘求逆序数Private Sub Command3_Click()Dim a As Integer, h As Integer, t As Integer, n As IntegerRandomizea = Int(Rnd * (999 - 100 + 1) + 100)h = Mid(CStr(a), 1, 1)t = Mid(CStr(a), 2, 1)n = Right(CStr(a), 1)Label5.Caption = a & "-->" & Format(n * 100 + t * 10 + h, "000")End Sub实验3_5略习题选择题1.D2.B3.B4.D5.B6.D7.D8.B9.C 10.A11.D 12.D 13.D 14.C15.D16.D17B18.A19.D20.B21.D 22.C 23.D 24.A 25.A 26.B 27.A 28.A 29.C 30.A31.B 32.C 33.A 34.D 35.A 36.B 37.B38.B39.C40.D41.B42.C43.B44.D 45.C 46.C 47.A 48.D 49.B 50.A51.C 52.B 53.D 54.D 55.D56.DA7.B58.A59.A60.A61.B62.B 63.C 64.A 65.B 66.B 67.D 68.A 69.A 70.A 71.C72.B 73.B 74.D75.D 76.C 77.B 78.D 79.A 80.D 81.C 82.B 83.D 84.B 85.B 86.B 87.A 88.D 89.C填空题1、2 -32768~327672、整型长整型单精度双精度3、0 False “”4、Chr(Int(Rnd*(Asc(“L”)-Asc(“C”))+Asc(“C”)))x>0 And y>0 Or x<0 And y<0c>=”a” And c<=”z” Or c>=”A” And c<=”Z”x>=10 And x<20x<z Xor y<z5、False 2 True False True True True True6、188 200100 68.56 8765547、a=1 b=1a=2 b=1a=3 b=1a=4 b=18、Y Mod 4=0 And Y Mod 100<>0 Or Y Mod 400 =09、He Said, “Good morning”. 10、EDEF11、(A>0 And A<>Int(A)) And (B<0 And B<>Int(B))12、a+b+c+d (a+b+c+d)/4 sum aver13、(f-2*h)/2 (4*h-f)/2简答题1、略2、Private Sub Form_Click()Const g = 9.8Dim V0 As Single, t As SingleDim S As SingleV0 = InputBox("请输入V0的值:")t = InputBox("请输入时间t的值:")S = (1 / 2) * g * t ^ 2 + V0 * tMsgBox ("位移量为:" & S)End Sub3、Private Sub Command1_Click()Dim l As Single, w As Single, h As SingleDim s As Singlel = Text1.Textw = Text2.Texth = Text3.Texts = (l * w + l * h + w * h) * 2Label1.Caption = sEnd Sub第四章选择分支与循环实验答案实验4-1 方法一:x<=5 ; y 方法二:Is<=2 ;Case Else实验4-2 Shape1.BackColor=vbRed; Inter=15; Inter=Inter-1; Inter 实验4-3 b<c b=a a=k实验4-4 方法二:s*n ; t/s<10^-5实验4-5 方法一:m>=10^-4 ; (-1)^(n+1)*m实验4-6 x2=x3 ; <10^-6实验4-7实验4-8 i+2*j+5*k=10 ; n实验4-9 题目一:a) 5 b)i=16 p=12 n=5 c)i=9 p=8 n=1 题目二:120 10实验4-10 N=1 ;S=N& "*3 " & "+1 "实验4-11 Len(st); Ch=Lcase(ch) ; Case Else ; Mid(Char,I,1)&St实验4-12 i\100 ; (i Mod 100)\10习题一、选择题1.C2.C3.A4.D5.A6.B7.B8.D9.B二、填空题1.Is=0 ; 1 To 10 ; Is >10 2. 9 3. 3 ; 4 15 4. this is a book ;This Is A Book 5. 3 ; 3 ; 3 5 6. 32 0 7. 4 8. 15 19. 1 ; 401 ;104 10. 1 2 3 ;1 3; 1 11. (3)12. 2 0 24 13. 10 14 14. BAABC CBAABC15. X Mod P=0 ; X=X+1 ;Find=False 16. abs(t)<10^(-7) ; s+t17. Mid(n,1,1)=Mid(n,2,1) And Mid(n,3,1)=Mid(n,4,1) S=S+118. Mid(n,i,1)>Mid(n,i+1,1) 19. "输入n";N/I=0 ;Print I第五章数组实验5_1 Int(90 * Rnd) + 10 a(i) = a(21 - i) a(21 - i) = t5_2 k = x Mod 10 i = i + 15_3 s5_4 max < a(i, j) row = I col = j5_5 1 ReDim Preserve prime(m) prime(j)5_6 d Mod 25_7 Exit For a(j + 1) = a(j)5_8 N – I A(j) = A(j + 1)5_9 A(i, 1) J Min = Max Not Flag5_10 ch >= "0" And ch <= "9" P = P & ch ReDim Preserve C(k)5_11 Private Sub Form_Click()Dim x As Integer, i As Integer, k As Integer, j As IntegerDim a() As Integerx = InputBox("输入正整数x=")i = 2Print x; "的质因子为:";Do While x >= iIf x Mod i = 0 Thenk = k + 1ReDim Preserve a(k)a(k) = ix = x \ iElsei = i + 1End IfLoopFor j = 1 To UBound(a)Print a(j);Next jEnd Sub习题一选择题1.A2.C3.D4.C5.B6.D7.D8.B9.D 10.B 11.B 12.D二填空题1. Variant2. ReDim A(N) A(1) If M < A(I) Then M = A(I)3. Int(26 * Rnd) + 65 Chr(t) i + 1 To 10 A(i) > A(j)4. InStr(s, ",") Right(s, Len(s) - n) s5. 4 5 6 6 5 4 3 2 16. T – 1 I + 17. k = 6 - i – j max = m 8. j = a j = j - 39. i + 1 i = i + 1 10. 2 3 7三编程题1.Option ExplicitOption Base 1Private Sub Command1_Click()Dim a(10) As IntegerDim i As Integer, max As IntegerFor i = 1 To 10a(i) = Int(90 * Rnd) + 10Print a(i);Next iPrinti = 1max = 0Do While i <= 10If a(i) Mod 3 = 0 ThenIf a(i) > max Thenmax = a(i)End IfEnd Ifi = i + 1LoopIf max = 0 ThenPrint "无要找的数"ElsePrint max & "是最大的能被3整除的数"End IfEnd Sub2.Option ExplicitOption Base 1Private Sub Command1_Click()Dim A() As IntegerDim I As Integer, m As IntegerDim K As IntegerReDim A(10)For I = 1 To 10A(I) = IPrint A(I);Next IPrintm = InputBox("ê?è?m")K = 9 + mReDim Preserve A(K)For I = 11 To KA(I) = A(I - 10)Next IFor I = 1 To 10A(I) = A(I + m - 1)Next IFor I = 1 To 10Print A(I);Next IPrintEnd Sub3.Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, J As Integer, K As IntegerDim a() As Integer, n As Integer, p As Integer Dim s As String, Flag As Booleanp = 1For K = 10 To 100I = 1Do While I <= 10 And Not FlagJ = 1Do While J <= 10 And Not Flagn = I ^ 2 + J ^ 2If n = K ThenFlag = TrueReDim Preserve a(p)a(p) = Kp = p + 1s = I & Str(J) & Str(K)List1.AddItem sEnd IfJ = J + 1LoopLoopFlag = FalseNext KEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim A(10) As IntegerDim B(10) As Integer, S As StringDim I As Integer, J As Integer, X As Integer A(1) = Int(90 * Rnd) + 10S = A(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = A(J) Then Exit ForNext JIf J = I ThenA(I) = XS = S & " " & A(I)Exit DoEnd IfLoopNext IText1.Text = SB(1) = Int(90 * Rnd) + 10S = B(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = B(J) Then Exit ForNext JIf J = I ThenB(I) = XS = S & " " & B(I)Exit DoEnd IfLoopNext IText2.Text = SS = ""For I = 1 To 10If A(I) = B(J) ThenS = S & " " & A(I)End IfNext JNext IText3.Text = SEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, J As IntegerDim a() As Integer, K As Integer, Sum As Integer K = Val(Text1.Text)I = 1DoJ = K Mod 10ReDim Preserve a(I)a(I) = JSum = Sum + JK = K \ 10I = I + 1Loop Until K = 0Text2.Text = SumEnd Sub6.Option ExplicitPrivate Sub Command1_Click()Dim a(100, 2) As Integer, K As IntegerDim I As Integer, J As IntegerDim M As Integer, N As IntegerK = 1For I = 1 To 99For J = 1 To 99If I > J ThenM = I - JN = I + JIf Sqr(M) = Int(Sqr(M)) And Sqr(N) = Int(Sqr(N)) ThenList1.AddItem I & " " & Ja(K, 1) = Ia(K, 2) = JK = K + 1End IfEnd IfNext JNext IEnd Sub第六章参考答案实验实验6-11、Exit Do2、Y(X)3、X >= -1实验6-2 【题目二】42 、26、8实验6-3 1、s1, key 2、s As String 3、ch >= "a" And ch <= "z"4、Mid(s, i, 1) = Chr(Asc(ch) + k - 26)思考:解密函数Private Sub dec(s As String, k As Integer) '解密过程Dim i As Integer, ch As String * 1For i = 1 To Len(s)ch = Mid(s, i, 1)If "A" <= ch And ch <= "Z" ThenIf Asc(ch) - k < Asc("A") ThenMid(s, i, 1) = Chr(Asc(ch) - k + 26) '小于A的ASCII值则加上26ElseMid(s, i, 1) = Chr(Asc(ch) - k)End IfElseIf ch >= "a" And ch <= "z" Then '多分支格式,ch介于小写a与z之间If Asc(ch) - k < Asc("a") ThenMid(s, i, 1) = Chr(Asc(ch) - k + 26) '小于a的ASCII值则加上26ElseMid(s, i, 1) = Chr(Asc(ch) - k)End IfEnd IfNext iEnd Sub实验6-41、b() As Integer, st As String2、L <= R3、ReDim Preserve b(R)4、L = L + 1辗转相除法求最大公约数函数Private Function Gcd(ByVal p As Integer, ByVal q As Integer) As IntegerDim r As Integerr = p Mod qDo While r <> 0p = qq = rr = p Mod qLoopGcd = qEnd Function实验6-5 【题目一】age = age(n - 1) - 3【题目二】1、(Left + Right) / 2 2、Left = Mid + 1实验6-6 1、3 -1 2、7 4 3、3 -1 4、7 0 实验6-7 【题目一】1、Sqr(2 + t) 2、Abs(a - 1) < eps【题目二】1、Dim X As Integer 2、"S(" & I & ")=" & S(I)3、(j * i + 1)习题参考答案一选择题1.B2.D3.C4.C5.C6.B7.B8.B9.A 10.D 11.D 12.A 13.A 14.A 15.B 16.B 17.D18.C19.D20.B21.C 22.C 23.B 24.A 25.D 26.D 27.A 28.B二、填空1(1)形式、(2)实在2(1)按地址传递、(2)ByVal3(1)窗体模块、(2)标准模块、(3)类模块4(1) 6 5(1)4 20 1016(1) 6 26 、(2)-2 987(1)10 6 、(2) 5 -58(1) 6 5 、(2)8 79(1)11 、(2)710(1)Fun(i) 、(2)Fun=p11(1) 4 、(2)20.712(1)29 、(2)28.713(1)k = j 、(2)b() As Integer14(1)gec 、(2)ge 、(3) 315(1)p1 = True And p2 = True 或p1 And p2 、(2)p1 = p2 –2 、(3)Isp = True 、(4)m Mod i = 016(1)Convert(st) 、(2)Len(s) 、(3)"A" To "F" 、(4)k = k + p * h17(1)isualVi 、(2)sualVisu18(1)s + a 、(2)n + 1 、(3)x As Single, n As Integer 、(4)p * (x + i) / ((2 * i - 1) * i * x)三、改错题1、Option Base 1Private Sub Command1_Click()Dim A() As Integer, I As Integer, J As Integer, Logic As Boolean Dim K As IntegerReDim A(1)A(1) = 50———————'K=1For I = 51 To 149K = 1 '位置错Logic = FalseCall Sub1(A, I, Logic)If Logic ThenK = K + 1ReDim A(K) 'ReDim Preserve A(K)A(K) = IEnd IfNext IFor I = 1 To UBound(A)Text1 = Text1 & Str$(A(I))If I Mod 5 = 0 Then Text1 = Text1 & vbCrLfNext IEnd SubPrivate Sub Sub1(A() As Integer, N As Integer, F As Boolean) Dim I As Integer, J As Integer, Ub As IntegerUb = UBound(A)For I = 1 To UbFor J = 2 To A(I)If A(I) Mod J = 0 And N Mod J = 0 ThenExit For 'Exit SubEnd IfNext JF = TrueNext IEnd Sub2、Option ExplicitOption Base 1Private Sub Command1_Click()Dim I As Integer, Flg As Boolean, S1 As Integer, S2 As Integer Dim J As Integer, K As Integer, P() As IntegerReDim P(1)P(1) = 2For I = 3 To 150 Step 2For J = 2 To Sqr(I)If I Mod J = 0 Then Exit ForNext JIf J > Sqr(I) ThenReDim Preserve P(UBound(P) + 1)P(UBound(P) + 1) = I 'P(UBound(P)) = IEnd IfNext IFor I = 80 To 125If fun(I, P, S1, S2) Then 'If fun(I, P, S1, S2)Text1 = Text1 & Str$(I) & "=" & Str(S1) & "*" & Str$(S2) & vbCrLf End IfNext IEnd SubPrivate Function fun(N As Integer, P() As Integer, S1 As Integer, S2 As Integer) As Boolean Dim I As Integer, J As IntegerFor I = 1 To UBound(P)For J = 1 To UBound(P) - 1If N = P(I) * P(J) ThenS1 = P(I)S2 = P(J)'fun = TrueExit Functionfun = True '位置错End IfNext JNext IEnd Function3、Option ExplicitPrivate Sub Get_Chess(Chess() As Integer, idx As Integer, Over As Integer) 'ByVal idx As Integer Dim Ub As Integer, Counter As Integer, K As IntegerUb = UBound(Chess)Chess(idx) = 0Counter = 1K = 0 '位置错Do Until Counter = Ub - 1Do Until K = 2idx = idx + 1If idx > Ub Then idx = 1K = K + Chess(idx)LoopChess(idx) = 0Counter = Counter + 1LoopFor Over = 1 To UbIf Chess(Over) <> 0 Then Exit ForNext OverEnd SubPrivate Sub Command1_Click()Dim Chess(32) As Integer, I As Integer, J As IntegerDim K As Integer, idx As IntegerFor I = 1 To 32For J = 1 To 32Chess(J) = 1Next JCall Get_Chess(Chess, I, K)If K = 23 ThenText1 = "从第" & Str$(I) & "号棋子开始取"Exit ForEnd IfNext IEnd Sub5、Option ExplicitPrivate Sub Form_Click()Dim S As String, Flg As BooleanDoS = InputBox("输入一个自然数")If S = "" Or Not IsNumeric(S) ThenMsgBox "请重新输入", 48 + vbOKOnlyElseExit DoEnd IfLoopjudge(S, Flg) ' Call judge(S, Flg) 或judge S, Flg If Flg ThenPrint S; "是回文数"ElsePrint S; "不是回文数"End IfEnd SubPrivate Sub judge(Ch As String, F As Boolean)Dim L As Integer, I As IntegerL = Len(Ch)F = True ' F = FalseFor I = 1 To LIf Mid$(Ch, I, 1) <> Mid$(Ch, L + 1 - I, 1) Then Exit Sub Next IF = False ' F = TrueEnd Sub四、编程题1、编程验证任一大于2的偶数可以表示成两个素数之和。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第6章过程与函数
一、填空题
1.数组名作为过程或函数实参,相应的形参传递方式为____按地址传递____。
2.一维长整型数组a作过程形参写作:a() As Long,二维长整型数组b作过程形
参写作:____b() As Long ____。
3.过程形参为整型,对应实参为5.64,传递给形参的值为____6____。
4.调用过程时对形参的改变不会导致相应实参变量的改变,则该形参采用____按
值传递____方式。
5.调用过程时对形参的改变就是对相应实参变量的改变,则该形参采用____按地
址传递____方式。
6.声明Single类型全局变量x,写作____Public x As Single(或 Public
x!)____;声明Integer类型静态变量
x,写作____Static x As Integer (或
Static x%)____。
7.在窗体Form1的过程中引用窗体Form2中的全局变量y,写作
____Form2.y____。
8.阅读如下代码,单击窗体后,窗体上的显示结果为____s = 2 s = 5 s = 9____。
Dim i As Integer, n As Integer
Private Sub Form_Click()
Dim i As Integer
For i = 1 To 3
s = sum(i): Print "s ="; s;
Next i
End Sub
Private Function sum(n As Integer)
Static j As Integer
j = j + n + 1: sum = j
End Function
二、选择题
1.VB程序设计语言中,函数过程与子过程必
须分别用关键字( C)声明。
(A)Private,Public (B)Public,
Private
(C)Function, Sub (D)Sub, Function
2.要调用一个已经定义好的函数
myfunction( ),用语句的方式调用函
数,下列写法哪个是正确的( D)
(A)Call myfunction (B)myFunction
(C)myFunction ( ) (D)Call
myFunction( )
3.定义过程时,如果在过程名前加关键字
Private,则该过程为( A)。
(A)它是私有过程,仅在定义该过程的
模块内有效,模块内的过程可以调用该
过程
(B)它是公有过程,在整个工程内有效,
模块外的过程可以调用该过程
(C)它是私有过程,在整个工程内有效,
模块外的过程可以调用该过程
(D)它是公有过程,仅在定义该过程的
模块内有效,模块内的过程可以调用该
过程
4.在过程中用Dim保留字定义的变量是
( B)
(A)全局变量 (B)局部变量 (C)静态变
量 (D)以上都不是
5.防止递归函数进入死锁的机制是
( A)
(A)在适当的条件下返回 (B)通过Stop
语句终止递归调用
(C)通过End语句终止递归函数 (D)不
需要返回
6.关于函数与子程序的关系,下面说法正确
的是( A)
(A) 函数执行完后将得到一个返回值,
而子程序只是执行一系列动作
(B) 函数可以不带参数,而子程序必须
带参数
(C) 在函数中只用到传地址的方式,而
子程序只能用到传值方式
(D)子程序可以被其他子程序调用,而
函数不能被其他函数调用
7.Visual Basic 6.0 中缺省的参数传递机
制是( B)
(A) 传值 (B) 传址 (C) 传值和传地
址 (D)从实参到形参
8.使用( C)语句可以实现过程的特殊
出口。
(A) Public Sub/Function (B) Call 过
程名
(C) Exit Sub/Function (D)Private
Sub/Function
三、思考题
1.静态变量和动态变量的区别是什么?
2.函数过程与子程序过程有什么区别?
3.子过程调用有哪几种形式?
4.什么是形参?什么是实参?什么是值引
用?什么是地址引用?对应的实参有
没有限制?
5.怎样引用其他模块的过程和变量?
6.编制通用函数过程,计算Double类型一
维数组所有元素的平均值。
7.编制通用Sub过程,将Single类型一维
数组反序排放(如实参数组元素依次为
6、5、9、7,调用后为
7、9、5、6;若
为-3.2、4、2.6、31、7.3,调用后为
7.3、31、2.6、4、-3.2)。
8.编制通用Sub过程,在一个m行n列二维数组中查找绝对值最大的元素,以及
该元素的行号、列号(提示:Sub过程
的形参列表如x() As Single, m As
byte, n As Byte, xmax As Single,ki
As Byte,kj As Byte)。
9.输出100—300间的素数,要求:
(1)用函数判断素数。
(2)每行输出10个数。
10.设计一个将一维数组按降序排列的子过程(假设数据个数不知道),然后编程:(1)自动产生并输出100个[100,999]间的正整数,并要求每行输出10个数
据。
(2)调用该过程对数据按降序排列后再按每行10个输出。
6-3-7解答:
Private Sub Form_Click()
Dim a() As Single
ReDim a(10)
Print "反序前:"
For i = 1 To 10
a(i) = Rnd * 201 - 100
Print Format(a(i), "###.## "); Next i
Print
Call inver(a(), 10)
Print "反序后:"
For i = 1 To 10
Print Format(a(i), "###.## ");
Next i
Print
End Sub
Private Sub inver(a() As Single, n As Integer)
Dim i As Integer, t As Single
For i = 1 To n \ 2
t = a(n - i + 1)
a(n - i + 1) = a(i)
a(i) = t
Next i
End Sub
6-3-8解答:
6-3-9解答:
Private Sub Form_Click()
s = 0
For i = 100 To 300
If prime(i) Then
Print i;: s = s + 1
If s Mod 10 = 0 Then Print
End If
Next i
End Sub
Private Function prime(x) As Boolean If x < 2 Then
prime = False
Exit Function
End If
k = Sqr(x)
For i = 2 To k
If x Mod i = 0 Then
prime = False
Exit Function
End If
Next
prime = True
End Function
6-3-10解答:
Private Sub Form_Click()
Dim i As Integer, n As Integer Dim a(100) As Integer
Print "排序前:"
For i = 1 To 100
a(i) = Int(900 * Rnd() + 100) Print a(i);
If i Mod 10 = 0 Then Print
Next i
Print
Print "排序后:"
Call ss(a(), 100)
For i = 1 To 100
Print a(i);
If i Mod 10 = 0 Then Print
Next i
End Sub
Private Sub ss(a() As Integer, n As Integer)
For i = 1 To n - 1
For j = i + 1 To n
If a(i) < a(j) Then
t = a(i): a(i) = a(j): a(j) = t
End If
Next j
Next i
End Sub。