当前位置:文档之家› VB程序设计教程第四版课后答案

VB程序设计教程第四版课后答案

vb课后习题答案第一章习题参考答案一、填空题1、对象事件驱动2、编译运行模式解释运行模式二、简答题1、简述VB的特点。

答:①可视化的程序设计方法②面向对象的程序设计思想③事件驱动的编程机制④结构化的程序设计语言⑤高度的可扩充性⑥强大的数据库访问能力⑦支持动态数据交换⑧支持对象链接与嵌入2、简述VB的安装过程。

答:①向光驱中放入VB安装光盘;②计算机将运行自动安装程序,若没有,执行VB目录下的Setup.exe;③根据提示逐一回答问题,并点击“下一步”;④点击“完成”;2、如何启动VB。

答:三种方法:①单击“开始”按钮,选择“程序”菜单下的“Microsoft Visual Basic 6.0 中文版”菜单项并单击;②双击桌面上的VB6.0快捷图标;③在“开始”菜单的运行对话框中输入命令来启动VB;第二章习题参考答案一、选择题:1-10、 DCACD BCBBC11-20、BADCA BBAAB21-29、AACBB CDDC二、填空题1.窗体、控件2.属性值3.属性窗口设置、代码窗口设置4.事件过程5.对象名、下划线、事件名6. Form_Click第三章习题参考答案一、选择题1、B2、C3、A4、B5、A6、C7、C二、填空题1、 11字符型、字节型、整型、长整型、单精度型、双精度型、货币型、逻辑型、日期型、对象型、变体型。

String、Byte、Integer、Long、Single、Double、Currency、Boolean、Date、Object、Variant2、双引号(英文)、#3、 ASCII、1、24、 public、通用、Dim、Static、Private、Static5、 &、+、+6、 Now()、Time、日期型三、解答题1、(3)2、(1 )3、(1)常量(关键字)(2)字符型常量(3)逻辑型常量(4)变量(5)字符串常量(6)变量(7)字符串常量(8)变量(9)日期型常量(题目应是#11/16/1999#)(10)数值型常量(单精度)4、315、(1)(1+y/x)/(1-y/x) (2)x^3+3*x*y/(2-y)(3)sqr(abs(a*b-c^3)) (4)sqr(s*(s-a)*(s-b)*(s-c))6、7、(1)13 (2)17(3)#1999-11-12# (4)zxy123ABC(3)5.25 (4)27)16 (8)“-459.65” (9)510、(4)11、(2)12、(4)第四章习题参考答案一、选择题:1—7: DAABBCD二、填空题1. 500.00%2.将字号扩大为原来的二倍3. B三、编程题(略)1.文本框、标签和命令按钮的名称分别为text1、label1、command1Private Sub Command1_Click()Label1.caption=text1.textCommand1.caption=text1.textEnd Sub2.单价、数量、折扣和应付款对应的文本框的名称分别为:text1、text2、text3和text4,计算和清除命令按钮的名称分别为:command1和command2。

Private Sub Command1_Click()Text4=val(text1)*val(text2)*val(text3)End SubPrivate Sub Command2_Click()Text1=”” : Text2=””: Text3=””: Text4=””End Sub3.文本框、“转大写”命令按钮和“转大写”命令按钮的名称分别为text1、command1和command2Private Sub Command1_Click()Text1=Ucase(text1)End SubPrivate Sub Command2_Click()Text1=Lcase(text1)End Sub第五章习题参考答案一、选择题:1—11: CAABBABBBCC二、填空题1.(3) 2. 1 2 3 3.2 2 3 4.160 5.9 6.78 7.x<=5 (或x<6 或 x<7) 8.B 9.iSum iSum+j 1250三、编程题1.Private Sub Form_Click()Dim i%,s%S=0For i = 101 To 200 step 2If i mod 3<>0 thenPrint iS=s+iEnd IfNext iPrint sEnd Sub2.Private Sub Form_Click() Dim n%,s&S=1:n=0While s<=400000N=n+1S=s*nWendIf s>400000 Then n=n-1Print nEnd Sub3.Private Sub Form_Click() Dim imin%,imax%,i%,R%Imin=100:imax=20For i=1 to 20R=Int(Rnd*51+50)If R>imax Then imax=RIf R<IMIN Then imin="R Next i Print imax,iminEnd Sub第六章习题参考答案一、选择题:1—13: ABAACCBADADBB二、填空题1.Interval Label1.caption=time2.List1.ListCount InterVal Hscroll1.Max 三、编程题第七章习题参考答案一、选择题:1—12: DCADD BBDAC CB二、填空题1.42 2.Index 3.414.A(i)=a(11-i) a(11-i)=Tmp5.1 6.X Mod 7=0 7.2108.0 To 8 i+1 To 99.3 K 110. 1001 Max MaxMax)三、编程题1.Private Sub C1_click()If OP1(0).Value=True Then P1.Print “选择了椭圆形”If OP1(1).Value=True Then P1.Print “选择了圆形”If OP1(2).Value=True Then P1.Print “选择了正方形”End Sub2.统计按钮名为Command1,输入框名为text1,输出框为text2(MultLine设为True)Private Sub Command1_click()Dim X(1 to 26) AS Integer,I As IntegerDim S As String,L As IntegerL=Len(Text1.text)For i=1 To LS=Mid(Text1,I,1)S=Ucase(S)If S >= "A" And S <= "Z" Thenx(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(1)至Mark(10)中,学生姓名在name(1)至name(10)中。

Private Sub Command1_click()Dim I%,J%,T%,S$For i=1 To 9For j=i+1 To 10If Mark(i)<MARK(J) Then T=Mark(i):Mark(i)=Mark(j):Mark(j)=TS=name(i):name(i)=name(j):name(j)=SNext JNext iFor i=1 To 10Print Name(i),Mark(i)Next iEnd Sub第八章习题参考答案一、选择题:1—10: DCBBDDDBCB二、填空题1.a Ubound(b) n=n-12.Key=a(i) Index=-1三、编程题1.子过程Sub P1(x As Single, esp As Single)Dim p!,S!,F!,i%P=1:S=1:i=1:F=xDoP=P*iS=S+F/pLoop Until Abs(F/p)<ESP Print sEnd Sub子函数Function P2(x As Single, esp As Single) As single Dim p!,S!,F!,i%P=1:S=1:i=1:F=xDoS=S+F/pLoop Until Abs(F/p)<ESP P2=sEnd Function2.Swap2能实现数据交换。

因为地址传递方式形参和实参有共同的地址,那么形参的变化,会引起实参的变化(即形参将值回传给实参),故可实现两数交换。

3.Function LF( ByVal x As Single) As SingleLF=X*X*XEnd FunctionSub Command1_Click()Dim I%For i=1 To 20Print I, LF(i)Next iEnd Sub4.Function WeekDay(ByVal x As Integer) As StringSelect Case xWeekDay=”星期日”Case 1WeekDay=”星期一”Case 2WeekDay=”星期二”Case 3WeekDay=”星期三”Case 4WeekDay=”星期四”Case 5WeekDay=”星期五”Case 6WeekDay=”星期六”End SelectEnd Function5.Function Rand30(ByVal X As`Integer) As Integer RandomizeRand30=Int(Rnd*X+1)End FunctionSub Command1_Click()For i=1 To 30Print Rand30(100)Next iEnd Sub6.Function F1(ByVal X As`Single, ByVal Y As`Single) As IntegerIf X>Y ThenF1=XElseF1=YEnd IfEnd Function7.Function GDBH(ByVal x%) As StringDim i%For i = 3 To x - 3 Step 2If isp(i) And isp(x - i) ThenGDBH = i & "+" & (x - i) & "=" & xExit FunctionEnd IfNextEnd FunctionFunction isp(y%) As Boolean i = 2: k = Int(y / 2)Do While i <= kIf y Mod i = 0 Then Exit Do i = i + 1LoopIf 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 =0 ThenisQO=”偶数”ElseisQO=”奇数”End IfEnd Function9.Function F1(ByVal x As Integer) As Boolean If x Mod 17 =0 and x Mod 37 =0ThenF1=TrueElseF1=FalseEnd IfEnd FunctionPrivate Sub Form_Click()For i = 1000 To 2000If F1(i) ThenPrint iEnd ifNext i10.Sub F1(a() As Integer, ByVal L1 As Integer, ByVal L2 AsInteger)Dim L%, U%, x%, i%L = LBound(a): U = UBound(a)If L1 > U Or L2 > U ThenPrint "位置输入错误!"Exit SubEnd IfIf L1 > L2 Thenx = a(L1)For i = L1 To L2 + 1 Step -1a(i) = a(i - 1)Next ia(L2) = xElseIf L2 > L1 Thenx = a(L1)For i = L1 To L2 - 1a(i) = a(i + 1)a(L2) = xEnd IfEnd Sub第九章习题参考答案一、选择题:1—3: ADD二、填空题1.下拉式弹出式 2.PopupMenu 3.Visible Enabled 4.-三、编程题(略)第十章习题参考答案一、选择题:1—3: DBD二、填空题1.Load 2.窗体模块标准模块类模块3.DoEvents三、编程题2.Function F(ByVal x&,ByVal Y&) As Integer Dim t&,r&If x<Y Then t="x:x=y:y=t R= x Mod yDo While R<>0X=yY=rR= x Mod yLoopF=yEnd FunctionPrivate Sub Command1_Click()Text3 = Val(Text1) / F(Val(Text1), Val(Text2)) Text4 = Val(Text2) / F(Val(Text1), Val(Text2)) End Sub第十一章习题参考答案一、选择题:1—3: DDA二、填空题6 2.AB A三、编程题1.Private Sub Form_MouseMove(Button As Integer, Shift As Integer, xAs Single, Y As Single)Text1 = x: Text2 = YEnd Sub2.Dim startX, StartY As Integer ‘在通用中申明Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x AsSingle, Y As Single)If Button = 2 ThenstartX = 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 IntegerEndX = x: EndY = Yc = RGB(0, 0, 0)r = Sqr((EndX - startX) ^ 2 + (EndY - StartY) ^ 2)Form1.Circle (startX, StartY), r, cEnd IfEnd Sub第十二章习题参考答案一、选择题:1—8: BBABBCCA9.I H F C L G E B A J K D二、填空题1.Drive 2.Path Print Dir1.Path3.Path 4.Hidden system5.顺序文件随机文件二进制文件6.FreeFile7.Close #18.Open “c:\samples\readme.txt” For Output AS #3 9.Open “c:\autoexec.bat” For Intpu t AS #4 10.Open “c:\samples\xscj.dat” For Random AS #3 Len=54 11.Open “smtext1.txt” For Binary AS #112.For Input Eof(1)13.KeyAscii END(或者End end eNd enD) Text1.text 三、编程题1.‘在标准模块中定义Type stuID As String * 12name As String * 8z1 As String * 12z2 As String * 12sex As String * 2jc As Booleanscool As String * 20sx As Integeryw As Integeryy As Integerwl As Integerzf As IntegerEnd TypeDim p As stu ‘在通用中定义Private Sub CmdAppend_Click()p.ID = Text1 = Text2p.z1 = Text3p.z2 = Text4p.scool = Text5If Option1.Value = True Then p.sex = "男"If Option2.Value = True Then p.sex = "女"If Check1.Value = 1 Then p.jc = True Else p.jc = False p.sx = Val(Text6) : p.yw = Val(Text7)p.yy = Val(Text8) : p.wl = Val(Text9)p.zf = Val(Text10)Put #1, , pEnd SubPrivate Sub CmdDisplay_Click()Get #1, Val(txtRecord_No), pText1 = p.ID : Text2 = Text3 = p.z1 : Text4 = p.z2Text5 = p.scoolIf p.sex = "男" Then Option1.Value = True Else Option2.Value = TrueIf p.jc = True Then Check1.Value = 1 Else Check1.Value = 0Text6 = p.sx : Text7 = p.ywText8 = p.yy : Text9 = p.wlText10 = p.zfEnd SubPrivate Sub Command3_Click()Text1 = "" : Text2 = ""Text3 = "" : Text4 = ""Text5 = "" : Text6 = ""Text7 = "" : Text8 = ""Text9 = "" : Text10 = ""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)Close #1End SubPrivate Sub Text10_GotFocus()Text10 = Val(Text6) + Val(Text7) + Val(Text8) + Val(Text9)End SubPrivate Type booksid As String * 10name As String * 8sex As String * 2math As Integereng As Integerele As IntegerEnd Type‘成绩输入Private Sub Command1_Click()Dim b As booksOpen "c:\stu.dat" For Append As #1b.id = InputBox("请输入学号") = InputBox("请输入姓名")b.sex = InputBox("请输入性别")b.math = InputBox("请输入数学成绩")b.eng = InputBox("请输入英语成绩")b.ele = InputBox("请输入电子成绩")Write #1, b.id, , b.sex, b.math, b.eng, b.ele Close #1End Sub‘不及格人员输出Private Sub Command2_Click()Dim a, b, cOpen "c:\stuo.dat" For Input As #1While Not EOF(1)Input #1, a, b, cIf c = "数学" Then List1.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 #1 Open "c:\stuo.dat" For Output As #2While Not EOF(1)Input #1, a, b, c, d, e, fIf d < 60 Then Write #2, a, b, "数学" If e < 60 Then Write #2, a, b, "英语" If f < 60 Then Write #2, a, b, "电子"WendClose #1Close #2End Sub。

相关主题