1、vb课后习题答案第一章 习题参考答案一、填空题1、对象事件驱动2、编译运行模式解释运行模式二、简答题1、简述VB的特点。答:可视化的程序设计方法面向对象的程序设计思想事件驱动的编程机制结构化的程序设计语言高度的可扩充性强大的数据库访问能力支持动态数据交换支持对象链接与嵌入2、简述VB的安装过程。答:向光驱中放入VB安装光盘;计算机将运行自动安装程序,若没有,执行VB目录下的Setup.exe;根据提示逐一回答问题,并点击“下一步”;点击“完成”;2、如何启动VB。答:三种方法:单击“开始”按钮,选择“程序”菜单下的“Microsoft Visual Basic 6.0中 文版”菜单项并单击;双
2、击桌面上的VB6.0快捷图标;在“开始”菜单的运行对话框中输入命令来启动VB;第二章习题参考答案一、选择题:1-10、DCACD BCBBC11-20.BADCA BBAAB21-29、AACBB CDDC二、填空题1.窗体、控件2.属性值3.属性窗口设置、代码窗口设置4.事件过程5.对象名、下划线、事件名6.FormClick第三章习题参考答案一、选择题1、B 2、C 3、A 4、B 5、A 6、C 7、C二、填空题1、11字符型、字节型、整型、长整型、单精度型、双精度型、货币型、逻辑型、日 期型、对象型、变体型。String a Byte、Integer Long Single、Doubl
3、e、Currency Boolean、Date、Object、Variant2、双引号(英文)、#3、ASCIL 1、24、public、通用、Dim、Static Private Static5、&、+、+6、Now()Time、日期型三、解答题1、2、(1)3、(1)常量(关键字)(2)字符型常量(3)逻辑型常量(4)变量(5)字符串常量(6)变量(7)字符串常量(8)变量(9)日期型常量(题目应是#11/16/1999#)(10)数值型常量(单精度)4、315、(1)(l+y/x)/(l-y/x)(2)xA3+3*x*y/(2-y)(3)sqr(abs(a*b-cA3)(4)sqr(s*
4、(s-a)*(s-b)*(s-c)6、7、(1)13(2)17(3)#1999-11-12#(4)zxyl23ABC8、(1)19(2)8.16666666666667(3)5.25(4)29、(1)-4(2)2.82842712474619(3)-3(4)49(5)”hello”(6)He7)16(8)“-459.65(9)510、(4)11、(2)12、(4)第四章习题参考答案一、选择题:17:DAABBCD二、填空题1.500.00%2.将字号扩大为原来的二倍3.B三、编程题(略)1.文本框、标签和命令按钮的名称分别为textl、labell、command 1Private Sub C
5、ommand l_Click()Labell.caption=textl.textCommand 1.caption=text 1.textEnd Sub2.单价、数量、折扣和应付款对应的文本框的名称分别为:textl、text2、text3 和text%计算和清除命令按钮的名称分别为:command 1和command2。Private Sub Command l_Click()Text4=val(textl)*val(text2)*val(text3)End SubPrivate Sub Command2_Click()Textl=:Text2=”:Text3=“:Text4=”End S
6、ub3.文本框、“转大写”命令按钮和“转大写”命令按钮的名称分别为texth command!和 command2Private Sub Command 1 _Click()Text 1=Ucase(text 1)End SubPrivate Sub Command2_Click()Text 1=Lcase(text 1)End Sub第五章习题参考答案一、选择题:111:CAABBABBBCC二、填空题1.(3)2.1 2 3 3.2 2 3 4.160 5.96.78 7.x=5(或 x6 或 x7)8.B9.iSum iSum+j 1250三、编程题1.Private Sub Form_
7、Click()Dim i%,s%S=0For i=101 To 200 step 2If i mod 30 thenPrint iS=s+iEnd IfNext iPrint sEnd Sub2.Private Sub Form_Click()Dim n%,s&S=l:n=0While s400000 Then n=n-lPrint nEnd Sub3.Private Sub Form_Click()Dim imin%,imax%,i%,R%Imin=100:imax=20For i=l to 20R=Int(Rnd*51+50)If Rimax Then imax=RIf Rimin The
8、n imin=RNext iPrint imax,iminEnd Sub第六章习题参考答案一、选择题:113:ABAACCBADADBB二、填空题1.Interval Label 1.caption=time2.List 1.ListCount InterVai三、编程题Hscrolll.Max第七章习题参考答案一、选择题:112:DCADDBBDAC CB二、填空题1.422.Index3.414.A(i)=a(ll-i)a(l l-i)=Tmp5.16.X Mod 7=07.2108.OTo 8i+1 To 99.3 K110.1001Max MaxMax)三、编程题1.Private S
9、ub Cl_click()If OP 1(0).Value=True Then P1.Print”选择 了椭圆形”If OP l(l).Value=True Then P1.Print 选择 了 圆形”If OP l(2).Value=True Then P1.Print 选择 了 正方形”End Sub2.统计按钮名为Command 1,输入框名为text 1,输出框为text2(MultLine设 为 True)Private Sub Command 1 _click()Dim X(1 to 26)AS Integer,!As IntegerDim S As String,L As Int
10、egerL=Len(Text 1.text)For i=l To LS=Mid(Text 1,1,1)S=Ucase(S)If S=A And S=Z Then x(Asc(S)-65+1)=x(Asc(S)-65+1)+1End IfNext iFor i=1 To LText2=text2&Chr(64+i)&x(i)&”“If I Mod 7=0 then Text2=text2&chr(13)+chr(10)Next iEnd Sub3.设学生成绩的平均分已在Mark(l)至Mark(10)中,学生姓名在name(l)至 name(10)中。Private Sub Command 1
11、_click()Dim I%,J%,T%,S$For i=l To 9For j=i+l To 10If Mark(i)Mark(j)ThenT=Mark(i):Mark(i)=Mark(j):Mark(j)=T S=name(i):name(i)=name(j):name(j)=S Next JNext iFor i=l To 10Print Name(i),Mark(i)Next iEnd Sub第八章习题参考答案 一、选择题:110:DCBBDDDBCB二、填空题1.a Ubound(b)n=n-l2.Ke尸a Index=-1三、编程题1.子过程Sub Pl(x As Single,e
12、sp As Single)Dimp!,S!,F!,i%P=l:S=l:i=l:F=xDoP=P*iS=S+F/pLoop Until Abs(F/p)espPrint sEnd Sub子函数Function P2(x As Single,esp As Single)As singleDimp!,S!,F!,i%P=l:S=l:i=l:F=xDoP=P*iS=S+F/pLoop Until Abs(F/p)Y ThenF1=XElseF1=YEnd IfEnd Function7.Function GDBH(ByVal x%)As StringDim i%For i=3 To x-3 Step
13、2If isp(i)And isp(x-i)ThenGDBH=i&+&(x-i)&=&xExit FunctionEnd IfNextEnd FunctionFunction isp(y%)As Booleani=2:k=Int(y/2)Do While i k Thenisp=TrueElseisp=FalseEnd IfEnd FunctionPrivate Sub Form_Click()For i=6 To 40 Step 2Print GDBH(i)Next iEnd Sub8.Function isQO(ByVal x As Integer)As String If x Mod 2
14、=0 ThenisQO=偶数”ElseisQO=奇数”End IfEnd Function9.Function Fl(ByVai x As Integer)As Boolean If x Mod 17=0 and x Mod 37=0ThenFl=TrueElseFl=FalseEnd IfEnd FunctionPrivate Sub Form_Click()For i=1000 To 2000IfFl(i)ThenPrint iEnd ifNext iEnd Sub10.Sub Fl(a()As Integer,By Vai LI As Integer,By Vai L2 AsIntege
15、r)Dim L%,U%,x%,i%L=LBound(a):U=UBound(a)If LI U Or L2 U ThenPrint”位置输入错误!Exit SubEnd IfIfLl L2 Thenx=a(Ll)Fori=Ll To L2+1 Step-1 a(i)=a(i-1)Next ia(L2)=xElseIfL2Ll Thenx=a(Ll)For i=LI To L2-1a(i)=a(i+1)Next ia(L2)=xEnd IfEnd Sub第九章习题参考答案一、选择题:13:ADD二、填空题1.下 拉式 弹出式 2.PopupMenu3.Visible Enabled 4.-三、编
16、程题(略)第十章习题参考答案一、选择题:13:DBD二、填空题1.Load 2.窗体模块 标准模块 类模块3.DoEvents三、编程题1.(略)2.Function F(ByVal x&,ByVal Y&)As IntegerDim t&,r&If xy Then t=x:x=y:y=tR=x Mod yDo While R0X=yY=rR=x Mod yLoopF=yEnd FunctionPrivate Sub Command l_Click()Text3=Val(Textl)/F(Val(Textl),Val(Text2)Text4=Val(Text2)/F(Val(Textl),Va
17、l(Text2)End Sub第十一章习题参考答案一、选择题:13:DDA二、填空题6 2.AB A三、编程题1.Private Sub Form_MouseMove(Button As Integer,Shift As Integer,xAs Single,Y As Single)Textl=x:Text2=YEnd Sub2.Dim startX,StartY As Integer 在通用中申明Private Sub Form_MouseDown(Button As Integer,Shift As Integer,x As Single,Y As Single)If Button=2 T
18、henstartX=x:StartY=YEnd IfEnd SubPrivate Sub Form_MouseMove(Button As Integer,Shift As Integer,x AsSingle,Y As Single)If Button=1 ThenPSet(x,Y),RGB(0,0,0)End IfEnd SubPrivate Sub Form_MouseUp(Button As Integer,Shift As Integer,x AsSingle,Y As Single)If Button=2 ThenDim r%,c%Dim EndX,EndY As IntegerE
19、ndX=x:EndY=Yc=RGB(0,0,0)r=Sqr(EndX-startX)A 2+(EndY-StartY)A 2)Form 1.Circle(startX,StartY),r,cEnd IfEnd Sub第十二章习题参考答案一、选择题:18:BBABBCCA9.I H F C L G EB A J KD二、填空题1.Drive2.PathPrint Dirl.Path3.Path4.Hiddensystem5.顺序文件随机文件二进制文件6.FreeFile7.Close#18.Open u c:samplesreadme.txtFor Output AS#39.Open uc:au
20、toexec.batv For Intput AS#410.Open c:samplesxscj.dat For Random AS#3 Len=5411.Open smtextl.txt For Binary AS#112.For Input Eof(l)13.KeyAscii END(或者 End end eNd enD)Textl.text三、编程题1.在标准模块中定义Type stuID As String*12 name As String*8 zl As String*12 z2 As String*12 sex As String*2 jc As Boolean scool As
21、 String*20 sx As Integer yw As Integer yy As Integer wl As Integer zf As IntegerEnd TypeDim p As stu 在通用中定义Private Sub CmdAppend_Click()p.ID=Textlp.name=Text2p.zl=Text3p.z2=Text4p.scool=Text5If Optionl.Value=True Then p.sex=男If Option2.Value=True Then p.sex=女If Check 1.Value=1 Then p.jc=True Else p.
22、jc=Falsep.sx=Val(Text6):p.yw=Val(Text7)p.yy=Val(Text8):p.wl=Val(Text9)p.zf=Val(TextlO)Put#1,pEnd SubPrivate Sub CmdDisplay_Click()Get#1,Val(txtRecord No),pTextl=p.ID:Text2=p.nameText3=p.zl:Text4=p.z2Text5=p.scoolIf p.sex=男Then Optionl.Value=True Else Option2.Value=TrueIf p.jc=True Then Check 1.Value
23、=1 Else Check 1.Value=0Text6=p.sx:Text7=p.ywText8=p.yy:Text9=p.wlText 10=p.zfEnd SubPrivate Sub Command3_Click()Textl=Text2=Text3=Text4=Text5=Text6=Text7=Text8=Text9=TextlO=End SubPrivate Sub Form_Load()Open App.Path&stu.dat For Random As#1 Len=Len(p)End SubPrivate Sub Form_Unload(Cancel As Integer)
24、Close#1End SubPrivate Sub Textl0_GotFocus()Textl 0=Val(Text6)+Val(Text7)+Val(Text8)+Val(Text9)End Sub2.3.4.Private Type booksid As String*10name As String*8sex As String*2math As Integereng As Integerele As IntegerEnd Type成绩输入Private Sub Command 1 _Click()Dim b As booksOpen c:stu.dat For Append As#1
25、b.id=InputBox(请输入学号)b.name=InputBox(请输入姓名”)b.sex=InputBox(请输入性别)b.math=InputBox(请输入数学成绩”)b.eng=InputBox(请输入英语成绩)b.ele=InputBox(请输入电子成绩”)Write#1,b.id,b.name,b.sex,b.math,b.eng,b.eleClose#1End Sub不及格人员输出Private Sub Command2_Click()Dim a,b,cOpen c:stuo.dat For Input As#1While Not EOF(l)Input#1,a,b,cIf
26、c=数学Then Listl.Additem bIf c=英语Then List2.Additem bIf c=电子Then List3.Additem bWendClose#1End Sub将不及格人选出存入stuo.datPrivate Sub Command3_Click()Dim a,b,c,d,e,fDim k%Open c:stu.dat For Input As#1Open c:stuo.dat For Output As#2While Not EOF(l)Input#1,a,b,c,d,e,fIfd60 Then Write#2,a,b,数学Ife60 Then Write#2,a,b,英语Iff 60 Then Write#2,a,b,电子”WendClose#1Close#2End Sub