《VB课后习题参考答案.pdf》由会员分享,可在线阅读,更多相关《VB课后习题参考答案.pdf(21页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、第 1 章 习题参考答案一、思考题略二、选择题1.C2,B第 2 章习题参考答案一、判断题题号1234567891011答案XXVXXVVV二、填空题1.窗体font2.不能在代码中设置3.seistart_4.Locked5.T a b i n d e x、06.vbModal 或 17.Ac t i v a t e、Deactivate8.PictureBox 和 Frame三、选择题题号12345678910答案DCDADBBBAB第三章习题参考答案一、思考题1、double2、xyCabc,15eyd,Is,#END X8P3、变 量 有(1)(4)(6)(8),常 量 有(2)字符串
2、型(3)逻 辑 型(5)字符串型(7)字符串型(9)日期型(10)实型4、ab+d+r-5、(1)26(2)3.75(3)36、写Hi下列Visual Basic表达式的值:(1)False(2)True(3)True(4)False(5)True(6)True(7)False(8)False7 将下列命题用Visual Basic布尔表达式表示:(1)zx and zy(2)abs(a)b+2(3)p mod q=0(4)x-2 and x5(5)xz or xz and yz(6)a0 and y0 o xvO and yvO 或 x*y 0。10.s=A and sv=Z or s=a
3、and sv=z 三、选择题题号12345答案cBBBD四、编程题Private Sub Form_Click()Dim x%,Ten%,hundred%x=Val(Textl.Text)Ten=(x 10)Mod 10hundred=x 100Print”百位数是:&hundredPrint 十位数是:“&TenEnd Sub2Private Sub Form_Click()Dim char As String*1Forml.CurrentX=Rnd*Form 1 .ScaleWidthForml.CurrentY=Rnd*Form 1 .ScaleHeightchar=Chr(Int(Rn
4、d*16)+65)Print charEnd Sub第 4 章 习 题 参 考 答 案一、判断题题号12345678答案XXXVVJJX二、选择题题号12345答案ADBBB三、程序阅读写出下列各段程序执行后的输出结果。1.785786357864135786422.w=4、w=13、w=135、w=313.此题有点错误,修改如下:是将语句 If k Asc(uAM)Then k=k+26w 改为:Ifk Asc(nZn)And k Asc(uan)Then k=k+26改后的答案:xyzal2 uvw&4.ABBBCCCCCDDDDDDDEEEEEEEEE5.此题的“HF KeyAsci=1
5、3 Then”改为 HF KeyAscii=13 Then”答案是:129四、程序填空题1.(1)Sing=-1(2)For i=2 to 19(3)Sing=一 SingX17x)zX)/17a(2(3(42.(1)L e n(ol d se n)(2)M i d(ol d se n,i,1)(3)Uc a se(c h a r)(4)c h a r3.L e n(strl)L e ng th-1M i d(strl,i,2)S um=04.(1)Tri m(S tr(i)(2)Tri m(S tr(i*i)(3)R i g h t(x2,L e n(xl)=xl5.(1)last_one=
6、last_two(2)last_two=this_one(3)i=i+1(4)i-1五、编程题1.Private Sub Form_Click()Dim RI As Double,R2 As Double R I,R2 表示两电阻值Dim Rp As Double,Rs As DoubleRI=Val(InputBox(输入电阻 RI=?)R2 二 Val(InputBox(输入电阻 R2=?”)Rp=Rl*R2/(R1+R2)Rs=Rl+R2Rp=Fix(Rp*1000+0.5)/1000,保留小数点后 3 位Rs=Fix(Rs*1000+0.5)/1000,保留小数点后 3 位MsgBox
7、”并联电阻是:“&Rp&vbCrLf&”串联电阻是:&RsEnd Sub2.Private Sub Form_Click()Dim N As LongN=Val(InputBox(”请输入年份“,“提示框”)If(N Mod 400=0)Or(N Mod 4=0 And N Mod 100 0)Then,判断条件Print N&”年是闰年”ElsePrim N&年不是闰年End IfEnd Sub3.Private Sub Form_Click()Dim a!,b!,c!,s!,x!a=Val(InputBox(HEnter A=?M)b=V al(Inpu tBox(Enter B=?)c=
8、Val(InputBox(Enter C=?H)If a+b c And b+c a And a+c b Then,如果能构成三角形x=(a+b+c)/2s=Sqr(x*(x-a)*(x-b)*(x-c)Print S=u;sElsePrint”数据有错,不能构成三角形”End IfEnd Sub4.(此题不适合使用Select语句)Private Sub Command 1 _Click()Dim x As Double,fx As Doublex=Val(InputBox(输入 x=?)If x 0 And x -3 Thenfx=x*x+x-6Elself x=0 And x 10 An
9、d x 2 And x 3 Thenfx=x*x-5*x +6Elsefx=x*x-x-lEnd IfPrint nx=x;”f(M&x&)=H;fxEnd Sub5.Dim i As IntegerDim s As Long,t As Longt=1For i=1 To 10t=t*is=s+tNext iPrintn S=n&sDim i%,s!,n!,t!,f%i=1 项数s=0 累加和n=1 ,阶乘t=1 通项f=1 嗦数Do While t 0.000001s=s+f*ti=i+1n=n*it=1 /nf=-f 表示符号LoopPrint S=;s方法一Private Sub For
10、m_Click()Dim i As IntegerDim Hundred As Integer,Ten As Integer,One As Integer 定义百位,I,位,个位数For i=100 To 999Hundred=i 100,求百位数Ten=(i 10)-Hundred*10,求十位数One=i Mod 10,求个位数Ifi=Hundred A 3+Ten A 3+One A 3 Then 判断是否为素数Prinli&”是水仙花数”End IfNext iEnd Sub 方法二Private Sub Form_Click()Dim i As IntegerDim Hundred
11、As Integer,Ten As Integer,One As Integer 定义百位,十位,个位数For Hundred=1 To 9,百位数ForTen=0To9 十位数For One=0 To 9i=Hundred*100+Ten*10+OneIf i=Hundred A 3+Ten A 3+One A 3 Then 判断是否为素数Print i&”是水仙花数”End IfNext OneNext TenNext HundredEnd SubDim a As Double,xO As Double,xl As Doublea=Val(InputBox(“输入 a=巧xO=axl=2/
12、3*x0+a/(3*x 0*xO)Do While Abs(xl-xO)0,000001xO=xl初值第一次迭代后的值控制精度迭代x l=2/3*x 0 +a/(3*x 0*xO)LoopPrint 立方根:;xl第 5 章习题参考答案一、判断题题号123456答案JVXXVJ二、选择题题号123答案DAC三、程序填空1.Preserve a(n+l)a(i+l)=a a(i+l)=m n+1 或 Ubourid(a)2.Print_K-lt=a(k,j):a(k,j)=k):k)=t3.Rnd*100P=10p=JP=0四、编写程序1.Private Sub Command 1 _Click
13、()Dim A()As Integer,N As IntegerDim i As Integer,M As Integer,t As Integer,k As IntegerN=Val(InpulBox(请输入数组的大小N=?n)M=Val(InputBox(”请输入移位次数M?)输入循环次数的大小IfN=0 Then Exit SubReDimA(N-1)Print”原始数组值”Fori=0ToN-1 ,给数组赋值A(i)=Int(Rnd*100)Print A(i);Next iPrint 移位For i=1 To M,循环体完成右移一位,循环完成右移j位t=A(N-1)For k=N-1
14、 To I Step-1 循环右移A(k)=A(k-1)Next kA(0)=tNext i 打印右移后的数组值Print”循环右移“&M&”位后的数组值”For i=0 To N-1Print A(i);Next iPrintEnd Sub2.Const NM=100Private Sub Command 1 _Click()Dim a(l To NM)As Single,b(l To NM)As Integer,max As IntegerDim i As Integer,j As Integer,k As Integer,t As IntegerFor i=1 To NM 输 入 100
15、个数值a(i)=Val(InputBox(请输入 a(H&i&)的值)Iflnt(a(i)=a(i)Then 判断是否为整数k=k+1 整数个数加1b(k)=a(i)将这个整数存放到数组BEnd IfNext i 选择排择For i=1 To k-1max=iFor j=i+1 To kIf b(max)b(j)Then max=jNext jIf max i Thent=b(max):b(max)=b(i):b(i)=tEnd IfNext i 打印输出For i=1 To kPrint b(i);IfiM od5=0Then PrintNext iEnd Sub3.Private Sub
16、Command 1 _Click()Dim a()As IntegerDim n%,i%,j%n=Val(InputBox(输入一个整数 n?)ReDim a(n)Forml.ClsRandomizePrint”产生的30个随机整数:“For i=1 To na(i)=Int(Rnd*21)-10Print a(i);If i Mod 10=0 Then PrintNext iPrint 删除数组的相同数据j=2Do While j nF o ri=lT o j-l 查找相同的元素If a(j)=a(i)Then Exit ForNext iIfijThen 如果第i 个数与前面的数据相同,删
17、除第i 个元素Fori=jTon-1a(i)=a(i+1)Next in=n-1ReDim Preserve a(n)Elsej=j+1End IfLoopPrint”删除后的数据:“For i=1 To nPrint a(i);If i Mod 10=0 Then PrintNext iEnd Sub4.Private Sub Commandl_Click()Dim a(l To 50)As Integer,i%,j%,t%,p%,n%,x%n=0 已经产生的元素个数RandomizeDo While n n Thenn=n+1a(n)=xEnd IfLoopFor i=1 To 49 使用
18、选择法排序P=iFor j=i+1 To 50If a(p)a(j)Then p=jNext jt=a(p):a(p)=a(i):a(i)=tNext iFor i=1 To 50 打印输出Print a(i);If i Mod 10=0 Then PrintNext iEnd Sub第 6 章 习题参考答案一、判断题题号123456答案VVXJJX二、选择题题号12345答案DCADB三、程序阅读1.s=CDEsum=32.(1)362516941(2)149941y=ly=2y=6y=24y=120四、程序填空题1.(1)Call Convert(st)(2)Ucase(Mid(s,i,l
19、)(3)Asc(subC)-65+10(4)Convert=P2.(1)L e ft,i-l)&mid(sl,i+ls2)(2)InStr(si,s2)(3)Call DeleStr(ssl.Text2)(4)ssl3.(1)number(i,7)(2)(int n,int m)(3)number=number*10+m五、编写程序1.Public Function fact(x As Integer)As Single 求 x!Dim f As Single,i As Integerf=1For i=I To xf=f*i,累积Next ifact=fEnd FunctionPrivate
20、Sub Form_Click()Dim n%,m%,y!Dom%=Val(InputBox(,m=,)n%=Val(InputBox(,n=n)Loop Until m=n And n=0y=fact(m)/(fact(n)*fact(m-n)Print yEnd Sub2.判断是否为回文录函数Private Function Judge(str As String)As BooleanDim i As IntegerJ udge=True 假设为回文F ori=l ToLen(str)2 依次判断对应位置的两个字符,只要有一组不等即不是回文If Mid(str,i,1)Mid(str,Len
21、(str)4-1 -i,1)ThenJudge=FalseExit FunctionEnd IfNext iEnd Function3.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 Singlef e =1 :t=1F or i =1 To nt=t *x/if e =f e +tN e xt iE n d Fu n c
22、t i o n t为通项第 7 章习题参考答案一、判断题题号1234567891 01 1答案JXJXVJXVXXJ三、选择题题号12345678答案BDDCBDCB四、程序阅读1.a=l b=la=3 b=2a=7 b=4a=1 5 b=82.2 2 23.s=5 s=1 0 s=404.5,1 0,2第 8 章习题参考答案一、判断题题号1234567891 01 11 2答案XXJVXXXJJVXX二、填空题1.M u l t i l i n e _ _ _ _ _ _ _2.V a l u e _ _ _ _ _ _ _ _3.Selected、L i s t、0、L i s t 1.L
23、 i s t Co u n t-14.下拉式列表框5.LargeC hanee、V a l u e三、程序填空L i s t l.Ad d l t e m L i s t 2.L i s t(L i s t 2.L i s t i n d e x)L i s l 2.R e m o v e i t e m1.Label 1.Font=Combo l.TextCombo2.TextLabel 1 .FontBold=TrueLabel l.Fontltalic=TrueLabel 1.FontSize=Val(Combo3.Text)2.L i s t l.L i s t(l i s t l.L
24、 i s t i n d e x)l i s t l.R e m o v e i t e m L i s t l.L i s t i n d e x四、选择题题号123456答案ADDBDD五、编程题1 ,添加功能Private Sub cmdAdd_Click()Dim i%If Combo 1.Text=,H Then Exit SubFor i=0 To Combo 1 .ListCount-逐个判断是否有与输入内容相同的项目If Combo 1 .Text=Combo l.List(i)Then Exit Sub 找到相同项目则退出Next iCombo 1.Addltem Combo
25、 1.Text 1 未找到则添加End Sub删除功能Private Sub cmdDelete_Click()If Combo 1 .Listindex=0 Then,是否选中某一 项目Combo 1 .Removeitem Combol.Listindex 删除选中项目Else删除与输入内容相同的项目For i=0 To Combo 1 .ListCount-1If Combo l.List(i)=Combol.Text ThenCombo 1 .Removeitem iEnd IfNext iEnd IfEnd Sub 清除Private Sub Command3_Click()Comb
26、ol.ClearEnd Sub2.Private Sub Commandl_Click()产生随机数Dim i%RandomizeFor i=1 To 10Listl.Addltem Str(Int(Rnd*90)+10)Next iList2,ClearEnd SubPrivate Sub Command2_Click()迁移偶数Dim i%i=0Do While i ave Then Print num(k);Next kClose#1End Sub3.Private Sub Form_Click()Dim strin As String*1,k%,n%Dim Nm(l To 26)As
27、Integer 打开文件进行统计Open D:Lcs.txt1 For Input As#1Open MD:Res.dat For Output As#2Do While Not EOF(l)strin=Input(l,#1),一次读出一个字符strin=UCase(strln),转换为大写字母If strin=A”And strin 57 OR KeyAsciiv48 then KeyAscii=O三、选择题题号12答案CD注:第 2 小题:最后一个选项(C)应 改 为(D)第13章习题参考答案一、填空题1_2.3.4.5.6.7.8.9.10.11.12.13.Database层次.一 个
28、或多Database Management System一 网 状 一一 关 系jy表 类 型、动态集类型_、-快照类型_、-快照类型 从“学生基本信息”表中查询所有“性别”为“男”的记录.Select姓名,应发工资From工资Where性别二“女”Insert into 学生成绩(学号,姓名,成绩)Values(0204016,张颖,88)Delete*from 学生成绩 Where 成绩 60D a ta b a s e N a m e、AbsolutionPosilion、RecordCountData 1 .Recordset.Edit:ConnectDatal.Rccordsel.M
29、ovcFirsl:Dalal.Recordset.Move 5Dalal.RecordsetC姓名)二王军:Dalal.Recordset.Update一Datal.Recordset.Index=XM14.Dalal.Recordset.Move-615.Datal.Recordset.FindFirst”姓名=,王颖16.DataSource、DataField二、选择题题号12345678答案cBCBDB、DBC第14章习题参考答案一、判断题题号123456答案JXJXJJ二、选择题题号12答案cC第15章习题参考答案判断题题号12345答案VXXJJ二、填空题1.Len(strl)length-1sum=O2.i100(i10)mod 10i mod 10