VB程序設(shè)計教程 第四版 課后答案
vb課后習(xí)題答案第一章 習(xí)題參考答案一、 填空題1、對象 事件驅(qū)動2、編譯運行模式 解釋運行模式二、 簡答題1、 簡述VB的特點。答:可視化的程序設(shè)計方法面向?qū)ο蟮某绦蛟O(shè)計思想事件驅(qū)動的編程機制結(jié)構(gòu)化的程序設(shè)計語言高度的可擴充性強大的數(shù)據(jù)庫訪問能力支持動態(tài)數(shù)據(jù)交換支持對象鏈接與嵌入2、簡述VB的安裝過程。答:向光驅(qū)中放入VB安裝光盤;計算機將運行自動安裝程序,若沒有,執(zhí)行VB目錄下的Setup.exe;根據(jù)提示逐一回答問題,并點擊“下一步”;點擊“完成”;2、 如何啟動VB。答:三種方法:單擊“開始”按鈕,選擇“程序”菜單下的“Microsoft Visual Basic 6.0 中文版”菜單項并單擊;雙擊桌面上的VB6.0快捷圖標(biāo);在“開始”菜單的運行對話框中輸入命令來啟動VB;第二章 習(xí)題參考答案一、選擇題:1-10、 DCACD BCBBC11-20、BADCA BBAAB21-29、AACBB CDDC二、填空題1 窗體、控件2 屬性值3 屬性窗口設(shè)置、代碼窗口設(shè)置4 事件過程5 對象名、下劃線、事件名6 Form_Click第三章 習(xí)題參考答案一、選擇題1、B 2、C 3、A 4、B 5、A 6、C 7、C二、填空題1、 11 字符型、字節(jié)型、整型、長整型、單精度型、雙精度型、貨幣型、邏輯型、日期型、對象型、變體型。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)常量(關(guān)鍵字) (2)字符型常量 (3)邏輯型常量 (4)變量 (5)字符串常量 (6)變量 (7)字符串常量 (8)變量 (9)日期型常量 (題目應(yīng)是#11/16/1999#) (10)數(shù)值型常量(單精度) 4、315、(1)(1+y/x)/(1-y/x) (2)x3+3*x*y/(2-y)(3)sqr(abs(a*b-c3) (4)sqr(s*(s-a)*(s-b)*(s-c)6、7、(1)13 (2)17(3)#1999-11-12# (4)zxy123ABC8、(1)19 (2)8.16666666666667(3)525 (4)29、(1)-4 (2)2 . 82842712474619 (3)-3 (4)49 (5)”hello” (6)He 7)16 (8) “-459.65” (9)5 10、(4)11、(2)12、(4)第四章 習(xí)題參考答案一、選擇題:17: DAABBCD二、填空題1 500.00%2 將字號擴大為原來的二倍3 B三、編程題(略)1文本框、標(biāo)簽和命令按鈕的名稱分別為text1、label1、command1Private Sub Command1_Click()Label1.caption=text1.textCommand1.caption=text1.textEnd Sub2單價、數(shù)量、折扣和應(yīng)付款對應(yīng)的文本框的名稱分別為: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文本框、“轉(zhuǎn)大寫”命令按鈕和“轉(zhuǎn)大寫”命令按鈕的名稱分別為text1、command1和command2Private Sub Command1_Click()Text1=Ucase(text1)End SubPrivate Sub Command2_Click()Text1=Lcase(text1)End Sub第五章 習(xí)題參考答案一、選擇題:111: CAABBABBBCC二、填空題1(3) 2 1 2 3 32 2 3 4160 59678 7x<=5 (或x<6 或 x<7) 8B9iSum iSum+j 1250三、編程題1Private 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 Sub2Private Sub Form_Click()Dim n%,s&S=1:n=0While s<=400000N=n+1S=s*nWendIf s>400000 Then n=n-1Print nEnd Sub3Private 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 iPrint imax,iminEnd Sub第六章 習(xí)題參考答案一、選擇題:113: ABAACCBADADBB二、填空題1Interval Label1.caption=time 2List1.ListCount InterVal Hscroll1.Max三、編程題第七章 習(xí)題參考答案一、選擇題:112: DCADD BBDAC CB二、填空題142 2Index 3414A(i)=a(11-i) a(11-i)=Tmp51 6X Mod 7=0 721080 To 8 i+1 To 993 K 110 1001 Max MaxMax)三、編程題1Private 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統(tǒng)計按鈕名為Command1,輸入框名為text1,輸出框為text2(MultLine設(shè)為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設(shè)學(xué)生成績的平均分已在Mark(1)至Mark(10)中,學(xué)生姓名在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第八章 習(xí)題參考答案一、選擇題:110: DCBBDDDBCB二、填空題1a Ubound(b) n=n-12Key=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子函數(shù)Function P2(x As Single, esp As Single) As singleDim p!,S!,F!,i%P=1:S=1:i=1:F=xDoP=P*iS=S+F/pLoop Until Abs(F/p)<ESP P2=sEnd Function2Swap2能實現(xiàn)數(shù)據(jù)交換。因為地址傳遞方式形參和實參有共同的地址,那么形參的變化,會引起實參的變化(即形參將值回傳給實參),故可實現(xiàn)兩數(shù)交換。3Function 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 Sub4Function WeekDay(ByVal x As Integer) As StringSelect Case xCase 0WeekDay=”星期日”Case 1WeekDay=”星期一”Case 2WeekDay=”星期二”Case 3WeekDay=”星期三”Case 4WeekDay=”星期四”Case 5WeekDay=”星期五”Case 6WeekDay=”星期六”End SelectEnd Function5Function Rand30(ByVal X AsInteger) As IntegerRandomizeRand30=Int(Rnd*X+1)End FunctionSub Command1_Click()For i=1 To 30Print Rand30(100)Next iEnd Sub6Function F1(ByVal X AsSingle, ByVal Y AsSingle) As IntegerIf X>Y ThenF1=XElseF1=YEnd IfEnd Function7Function 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 Booleani = 2: k = Int(y / 2)Do While i <= kIf y Mod i = 0 Then Exit Doi = i + 1LoopIf i > k Thenisp = TrueElseisp = FalseEnd IfEnd FunctionPrivate Sub Form_Click()For i = 6 To 40 Step 2Print GDBH(i)Next iEnd Sub8Function isQO(ByVal x As Integer) As StringIf x Mod 2 =0 ThenisQO=”偶數(shù)”ElseisQO=”奇數(shù)”End IfEnd Function9Function F1(ByVal x As Integer) As BooleanIf 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 iEnd Sub10Sub 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)Next ia(L2) = xEnd IfEnd Sub第九章 習(xí)題參考答案一、選擇題:13: ADD二、填空題1下拉式 彈出式 2PopupMenu 3Visible Enabled 4-三、編程題(略)第十章 習(xí)題參考答案一、選擇題:13: DBD二、填空題1Load 2窗體模塊 標(biāo)準(zhǔn)模塊 類模塊 3DoEvents三、編程題1(略)2Function F(ByVal x&,ByVal Y&) As IntegerDim 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第十一章 習(xí)題參考答案一、選擇題:13: DDA二、填空題6 2AB A 三、編程題1Private Sub Form_MouseMove(Button As Integer, Shift As Integer, xAs Single, Y As Single)Text1 = x: Text2 = YEnd Sub2Dim 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第十二章 習(xí)題參考答案一、選擇題:18: BBABBCCA9I H F C L G E B A J K D二、填空題1Drive 2Path Print Dir1.Path3Path 4Hidden system5順序文件 隨機文件 二進制文件6FreeFile7Close #18Open “c:samplesreadme.txt” For Output AS #39Open “c:autoexec.bat” For Intput AS #410Open “c:samplesxscj.dat” For Random AS #3 Len=5411Open “smtext1.txt” For Binary AS #112For Input Eof(1)13KeyAscii END(或者End end eNd enD) Text1.text三、編程題1在標(biāo)準(zhǔn)模塊中定義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 = Text1p.name = 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 = Falsep.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 = p.nameText3 = 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 Sub2.3.4.Private 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("請輸入學(xué)號")b.name = InputBox("請輸入姓名")b.sex = InputBox("請輸入性別")b.math = InputBox("請輸入數(shù)學(xué)成績")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(1)Input #1, a, b, cIf c = "數(shù)學(xué)" 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 #1Open "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, "數(shù)學(xué)"If e < 60 Then Write #2, a, b, "英語"If f < 60 Then Write #2, a, b, "電子"WendClose #1Close #2End Sub