《VB程序设计教程课后实验答案 .pdf》由会员分享,可在线阅读,更多相关《VB程序设计教程课后实验答案 .pdf(37页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、A1 Private Sub Command1_Click()Label3=Text1 End Sub A2 Private Sub Form_Load()Timer1.Interval=0 End Sub Private Sub Command1_Click()自动Timer1.Interval=200 End Sub Private Sub Command2_Click()手动Timer1.Interval=0 Call MyMove End Sub Private Sub Timer1_Timer()Call MyMove End Sub Sub MyMove()Label1.Move
2、Label1.Left-50 If Label1.Left 0 Then Label1.Left=Form1.Width End Sub A3 Private Sub Form_Click()Text1=Val(Text1)+1 End Sub Private Sub Form_Load()Text1=0 End Sub A4 Private Sub Form_Click()Caption=单击窗体,改变图片 Picture=LoadPicture(App.Path+n_015.bmp)Print 欢迎使用VB End Sub Private Sub Form_DblClick()Cls Ca
3、ption=双击窗体,卸去图片 Picture=LoadPicture()End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 37 页 -Private Sub Form_Load()Caption=装入窗体 Picture=LoadPicture(App.Path+n_016.bmp)Print 装入图 End Sub Private Sub Form_Resize()该事件的作用窗体始终与图一样大Caption=窗体大小不变 Form1.Width=260*16 260 是 Tongji-2.bmp 图的宽度,象素单位Form1.Height=260*16+200
4、 260 是图的高度,象素单位,200 是窗体的标题栏高度End Sub A5 Sub Command1_Click()Text1.FontName=隶书 Text1.FontSize=25 End Sub Sub Command2_Click()Text2.Text=Text1.SelText Text2.FontName=Text1.FontName Text2.FontSize=Text1.FontSize End Sub B1 Private Sub Command1_Click()Text2=Format(5/9*(Val(Text1)-32),0.00)End Sub Privat
5、e Sub Command2_Click()Text1=Format(9/5*Val(Text2)+32,0.00)End Sub 或Private Sub Command1_Click()Dim f!,c!声明两个变量f=Val(Text1)c=5/9*(f-32)Text2=Format(c,0.00)保留两位小数End Sub Private Sub Command2_Click()Dim ff!,cc!声明两个变量cc=Val(Text2)ff=9/5*cc+32 Text1=Format(ff,0.00)保留两位小数End Sub B2 名师资料总结-精品资料欢迎下载-名师精心整理-
6、第 2 页,共 37 页 -Private Sub Command1_Click()Label2=Format(Val(Text1)*Val(Text1)*3.14,0.00)End Sub Private Sub Command2_Click()Label3=Format(Val(Text1)*3.14*2,0.00)End Sub Private Sub Text1_LostFocus()If Not IsNumeric(Text1.Text)Then MsgBox 输入有非数字字符,请重新输入,警告 Text1.Text=Text1.SetFocus End If End Sub 或Pr
7、ivate Sub Command1_Click()Label2=Format(Val(Text1)*Val(Text1)*3.14,0.00)End Sub Private Sub Command2_Click()Label3=Format(Val(Text1)*3.14*2,0.00)End Sub Private Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii=13 Then If Not IsNumeric(Text1.Text)Then Text1.Text=End If End If End Sub B3 Private Sub
8、 Command1_Click()n=Int(Log(2)/Log(1.008)+1)Label1=n&年后人数超过26 亿 End Sub B4 Private Sub Command1_Click()Dim x,dx,cd x=Text1.Text dx=UCase(x)cd=Len(x)Print 大写字母为:;dx Print 字符串长度为:;cd End Sub B5 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 37 页 -Private Sub Command1_Click()Text1=Int(Rnd*900+100)End Sub Private Sub Co
9、mmand2_Click()Dim x%,x1%,x2%,x3%x=Val(Text1)x1=x Mod 10 分离出的个位数x2=(x Mod 100)10 分离出的十位数x3=x 100 分离出的百位数Label1=x1*100+x2*10+x3 End Sub B6 Private Sub Form_Click()Label1=Left(Text1,11)Label2=Mid(Text1,12,6)Label3=Right(Text1,5)End Sub B7 Private Sub Command1_Click()Print For i=1 To 5 Print Tab(15-i*2)
10、;String(2*i-1,);Spc(18-4*(i-1);String(2*i-1,)Next i End Sub Private Sub Command2_Click()Cls End Sub 进一步要求:Private Sub Command1_Click()Print For i=1 To 5 Print Tab(15-i*2);String(2*i-1,);String(10-(2*i-1),);String(2*i-1,)Next i End Sub Private Sub Command2_Click()Cls End Sub B8 Private Sub Form_Click
11、()x=Val(InputBox(输入一正实数,计算,0)名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 37 页 -pf=Format(x*x,0.000)pfg=Format(Sqr(x),0.000)lf=Format(x*x*x,0.000)lfg=Format(x (1/3),0.000)Print 平方为:;pf;Space(5);平方根为:;pfg;Space(5);立方为:;lf;Space(5);立方根为:;lfg End Sub C1 Private Sub Form_Click()Dim x!,y!x=Val(InputBox(输入 x 的值)If x 10
12、00 Then y=x ElseIf x 2000 Then y=0.9*x ElseIf x 3000 Then y=0.8*x Else y=0.7*x End If Print y End Sub C2 Private Sub Form_Click()Dim x!,y!x=Val(InputBox(输入上网时间)If x 10 Then y=30 ElseIf x 150 Then y=150 End If Print y End Sub C3 Private Sub Command1_Click()Dim x!,y!,z!名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 3
13、7 页 -x=InputBox(input x)y=InputBox(input y)z=InputBox(input z)Print x y z Print 排序前;x;y;z If x y Then t=x:x=y:y=t If x z Then t=x:x=z:z=t If y z Then t=y:y=z:z=t Print 排序后&x&y&z End Sub Private Sub Command2_Click()Dim x!,y!,z!x=InputBox(input x)y=InputBox(input y)z=InputBox(input z)Print x y z Print
14、 排序前;x;y;z If x y Then t=x:x=y:y=t If y z Then t=y:y=z:z=t If x a(i+1)Then z=a(i+1)a(i+1)=a(i)a(i)=z 名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 37 页 -End If Next i Picture2.Print a(0)Picture2.Print a(1)Picture2.Print a(2)End Sub C5 Private Sub Text2_LostFocus()Dim m%,n%,y%m=Val(Text1)n=Val(Text2)If n Mod 2 0 Th
15、en MsgBox(脚数必定为偶数)Text2=Text2.SetFocus Else y=n/2-m If y 0 Then Text4=Format(-b+Sqr(de)/t,0.00)Text5=Format(-b-Sqr(de)/t,0.00)名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 37 页 -Else Text4=Format(-b/t,0.00)&+&Format(Sqr(Abs(de)/t,0.00)&i Text5=Format(-b/t,0.00)&-&Format(Sqr(Abs(de)/t,0.00)&i End If End Sub Private
16、 Sub Command2_Click()Text1=Text2=Text3=Text4=Text5=End Sub C7 Private Sub Text3_LostFocus()Select Case Trim(Text3)Case+Text4=Val(Text1)+Val(Text2)Case-Text4=Val(Text1)-Val(Text2)Case*Text4=Val(Text1)*Val(Text2)Case/If Val(Text2)=0 Then MsgBox 分母为零,重新输入 Text2=Text2.SetFocus Else Text4=Val(Text1)/Val(
17、Text2)End If End Select End Sub C8 Private Sub Text1_LostFocus()Select Case Trim(Text1)Case 1 Text2=Monday Case 2 Text2=Tuesday Case 3 Text2=Wednesday Case 4 Text2=Thursday 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 37 页 -Case 5 Text2=Friday Case 6 Text2=Saturday Case 7 Text2=Sunday Case Is 7,Is 7 Or Text1 1 Th
18、en MsgBox 数字为 17,重新输入 Text1=Text1.SetFocus Else Text2=Choose(Text1,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday)End If End Sub D1 Private Sub Form_Click()For i=1 To 9 Print Tab(10-i);String(2*i-1,Trim(Str(i)Next i End Sub D2 Private Sub Form_Click()For i=1 To 10 Step 1 Print Tab(i);Strin
19、g(20-2*i),Chr(64+i)Next i End Sub D3 Private Sub Command1_Click()Dim s!,t!,i&s=1 t=1 For i=1 To 100000 名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 37 页 -t=t+i s=s+1/t If 1/t 0.00001 Then Exit For Next i Print For 结构;s,i;项 End Sub D4 Private Sub Command1_Click()Dim n&,pi#,i&n=InputBox(输入 n 值)pi=2 For i=1 To n pi=
20、pi*(2*i)/(2*i-1)*(2*i)/(2*i+1)Next i Print 当 n=&n&时,pi=;pi End Sub D5 Private Sub Form_Click()Dim s!,t!,i!,a%,n%a=Int(Rnd*9+1)n=Int(Rnd*6+5)t=0:s=0 Print a=;a,n=;n For i=1 To n t=t*10+a s=s+t Print t;Next i Print Print s=;s End Sub D6 Private Sub Command1_Click()Dim s As Integer s=0 For i=1 To 9 For
21、 j=0 To 9 For k=0 To 9 s=i*100+j*10+k If s=i 3+j 3+k 3 Then Print s 名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 37 页 -End If Next k Next j Next i End Sub Private Sub Command3_Click()formd6.Hide main.Show End Sub D7 Private Sub Command1_Click()Dim a!,x0!,x1!a=27 x0=2 i=0 Do i=i+1 x1=2*x0/3+a/(3*x0*x0)If Abs(x1-x
22、0)Max Then Max=a(i)If a(i)Min Then Min=a(i)Avg=Avg+a(i)Next i Avg=Avg/10 Print Print Max=;Max;Min=;Min;Avg=;Avg End Sub E2 Private Sub Form_Click()Dim a a=Array(56,78,98,88,76,78)For i=0 To 5 Print String(a(i)5,);a(i)Print Next i End Sub E3 Dim a%(19)Private Sub Command1_Click()Picture1.Cls 名师资料总结-精
23、品资料欢迎下载-名师精心整理-第 12 页,共 37 页 -For i=0 To 19 a(i)=Int(Rnd*101)Picture1.Print a(i);If(i+1)Mod 4=0 Then Picture1.Print Next i End Sub Private Sub Command2_Click()Picture2.Cls Dim s(5 To 9)For i=0 To 19 k=a(i)10 Select Case k Case 0 To 5 s(5)=s(5)+1 Case 9 To 10 90100 分的人数s(9)=s(9)+1 Case 6 To 8 存放其他三个分
24、数段的下标有规律,根据K 获得s(k)=s(k)+1 End Select Next i For i=5 To 9 If s(i)0 Then Picture2.Print s(;i;)的人数有;Format(s(i),0);个 Next i End Sub E4 Private Sub Command1_Click()Picture1.Cls Dim d%(1 To 10)For i3=1 To 10 Randomize d(i3)=Int(Rnd*91+10)Next i3 For i=1 To 10 For j=1 To 10-i If d(j)=5 Then MsgBox(超过 5 门
25、课程,不能再选)Exit Sub Else List2.AddItem List1.Text List1.RemoveItem List1.ListIndex End If End Sub E8 Sub Combo1_KeyPress(KeyAscii As Integer)Select Case KeyAscii Case 48 To 57,13 Case Else 名师资料总结-精品资料欢迎下载-名师精心整理-第 15 页,共 37 页 -KeyAscii=0 End Select If KeyAscii=13 Then Combo1.AddItem Combo1.Text Combo1.
26、Text=End If End Sub Private Sub Command1_Click()Dim min%,max%min=Val(Combo1.List(0)max=Val(Combo1.List(0)imin=0 imax=0 For i=1 To Combo1.ListCount-1 If Val(Combo1.List(i)max Then imax=i max=Combo1.List(i)ElseIf Val(Combo1.List(i)min Then imin=i min=Combo1.List(i)End If Next i t=Combo1.List(0)Combo1.
27、List(0)=Combo1.List(imin)Combo1.List(imin)=t t=Combo1.List(Combo1.ListCount-1)Combo1.List(Combo1.ListCount-1)=Combo1.List(imax)Combo1.List(imax)=t End Sub E9 Private Sub Form_Click()For i=0 To Screen.FontCount-1 If Asc(Left(Screen.Fonts(i),1)=5 Then MsgBox(输入人数超过数组声明的个数)Else With a(n).number=Text1.n
28、ame=Text2.salary=Text3 Picture1.Print a(n).number,a(n).name,a(n).salary Print End With n=n+1 Text1=Text2=Text3=End If End Sub Private Sub Command2_Click()Dim t As clerk,i%,j%For i=0 To n-1 For j=i To n-1 If a(i).salary a(j+1).salary Then t=a(i):a(i)=a(j+1):a(j+1)=t End If Next j Next i Picture1.Cls
29、Picture1.Print 工号姓名工资 For i=0 To n-1 Picture1.Print a(i).number,a(i).name,a(i).salary Print Next i End Sub F1 Private Sub Form_Click()Dim a(1 To 10),amin,i%名师资料总结-精品资料欢迎下载-名师精心整理-第 17 页,共 37 页 -For i=1 To 10 a(i)=-Int(Rnd*101+300)Print a(i);Next i Call s(a(),amin)Print Print min=;amin End Sub Sub s(
30、b(),min)Dim i%min=b(LBound(b)For i=LBound(b)+1 To UBound(b)If b(i)min Then min=b(i)Next i End Sub F2 Private Sub Command1_Click()Dim mm%,nn%mm=Val(Text1)nn=Val(Text2)Picture1.Print mm;Tab(6);nn;Tab(12);gcd(mm,nn)End Sub Function gcd%(ByVal m%,ByVal n%)If m n Then t=m:m=n:n=t r=m Mod n Do While(r 0)m
31、=n:n=r:r=m Mod n Loop gcd=n End Function F3 Dim x!Private Sub Command1_Click()Print 调用标准函数Sin 的结果;Sin(x)End Sub Private Sub Command2_Click()Print 调用自定义函数MySin 的结果;MySin(x)End Sub Function MySin(x!)As Double Dim i%,t!,s!t=x 名师资料总结-精品资料欢迎下载-名师精心整理-第 18 页,共 37 页 -s=t i=1 Do While Abs(t)0.00001 t=-1*t*x
32、*x/(i+1)*(i+2)s=s+t i=i+2 Loop MySin=s End Function Private Sub Command3_Click()x=InputBox(输入要计算正弦函数的角度值x)x=x*3.14/180 End Sub F4 Private Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii=13 Then If Not IsNumeric(Text1)Then MsgBox 输入非数字串,重新输入 Text1.Text=Text1.SetFocus Else If IsH(Text1)Then Picture
33、1.Print Text1;Else Picture1.Print Text1 End If Text1=End If End If End Sub Function IsH(ss As String)As Boolean Dim i%,Ls%IsH=True ss=Trim(ss)Ls=Len(ss)For i=1 To Ls 2 If Mid(ss,i,1)Mid(ss,Ls+1-i,1)Then IsH=False Exit Function End If Next i End Function F5 名师资料总结-精品资料欢迎下载-名师精心整理-第 19 页,共 37 页 -Funct
34、ion prime(ByVal m As Integer)As Boolean prime=True Dim i%For i=2 To m-1 If(m Mod i)=0 Then prime=False:Exit Function 注意冒号和exit 的范围Next i End Function Private Sub Command1_Click()n=0 For i=6 To 100 Step 2 For j=3 To i 2 If prime(j)Then If prime(i-j)Then List1.AddItem i&=&j&+&i-j n=n+1 End If End If N
35、ext j Next i Picture1.Print 6 和 100 之间共有;n;对素数和 End Sub F6 Dim a%()Private Sub Form_Click()Print 1000 以内的完数为:For i=1 To 1000 If IsWs(i)Then Print i;=1;For j=1 To UBound(a)Print+;a(j);Next j Print End If Next i End Sub Function IsWs(m)As Boolean Dim s%s=0 For i=1 To m 2 If m Mod i=0 Then ReDim Preser
36、ve a(j)a(j)=i 名师资料总结-精品资料欢迎下载-名师精心整理-第 20 页,共 37 页 -j=j+1 s=s+i End If Next i If m=s Then IsWs=True End Function F7 Private Sub DeleStr(s1 As String,ByVal s2 As String)Dim i%ls2=Len(s2)i=InStr(s1,s2)Do While i 0 s1=Left(s1,i-1)+Mid(s1,i+ls2)在 s1 中去除 s2 子串i=InStr(s1,s2)Loop End Sub Private Sub Comman
37、d1_Click()调用 DeleStr 子过程Dim ss1 As String ss1=Text1 Call DeleStr(ss1,Text2)Text3=ss1 End Sub Private Sub Command2_Click()End Sub F8 Private Sub Command1_Click()Dim maxw$maxlen Text1&,maxw Text2=maxw End Sub Sub maxlen(s$,maxw$)Dim word$maxw=Do While s i=InStr(s,)word=Left(s,i-1)If Len(word)Len(maxw)
38、Then maxw=word s=Mid(s,i+1)Loop 名师资料总结-精品资料欢迎下载-名师精心整理-第 21 页,共 37 页 -End Sub G1 Private Sub Command1_Click()List1.Clear List1.AddItem Combo1 If Option1 Then List1.AddItem Pentium II If Option2 Then List1.AddItem Pentium I If Option3 Then List1.AddItem Celeron List1.AddItem Text1 If Check1 Then List
39、1.AddItem 声卡 If Check2 Then List1.AddItem Modem If Check3 Then List1.AddItem 网络适配器 End Sub Private Sub Text1_LostFocus()st=UCase(Trim(Text1)le=Len(st)If Not IsNumeric(Left(st,le-2)Or Right(st,2)MB Then MsgBox 有不合法字符!Text1=Text1.SetFocus End If End Sub G2 Private Sub Check1_Click()Picture1.Font.Bold=
40、Not Picture1.Font.Bold End Sub Private Sub Check2_Click()Picture1.Font.Italic=Not Picture1.Font.Bold End Sub Private Sub Command1_Click()Picture1.Cls If Option1 Then Picture1.Print Sin(Val(Text1)ElseIf Option2 Then Picture1.Print Exp(Val(Text1)ElseIf Option3 Then Picture1.Print Sqr(Val(Text1)End If
41、End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 22 页,共 37 页 -Private Sub return_Click(Index As Integer)Form6.Show Unload Form2 End Sub G3 Private Sub HScroll1_Change()Text1=VScroll1.V alue Text2=HScroll1.Value Text3=HScroll2.Value Text4=Format(Val(Text1)*(Text3/100)*(Text2/12),0.00)Text5=Format(Val(Text4)+Val(Text
42、1),0.00)End Sub Private Sub HScroll2_Change()Text1=VScroll1.V alue Text2=HScroll1.Value Text3=HScroll2.Value Text4=Format(Val(Text1)*(Text3/100)*(Text2/12),0.00)Text5=Format(Val(Text4)+Val(Text1),0.00)End Sub Private Sub VScroll1_Change()Text1=VScroll1.V alue Text2=HScroll1.Value Text3=HScroll2.Valu
43、e Text4=Format(Val(Text1)*(Text3/100)*(Text2/12),0.00)Text5=Format(Val(Text4)+Val(Text1),0.00)End Sub G4 Dim t As Single Private Sub Command1_Click()t=InputBox(输入倒计时分钟数)t=t*60 ProgressBar1.Min=0 ProgressBar1.Max=t 名师资料总结-精品资料欢迎下载-名师精心整理-第 23 页,共 37 页 -ProgressBar1.Value=ProgressBar1.Max End Sub Priv
44、ate Sub Command2_Click()Timer1.Interval=1000 ProgressBar1.Visible=True Timer1.Enabled=True End Sub Sub Timer1_Timer()Dim m%,s%t=t-1 ProgressBar1.Value=t m=t 60 s=t Mod 60 Label1=m&分&s&秒 If t=0 Then MsgBox 时间到!Timer1.Interval=0 ProgressBar1.Visible=False End If End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 24 页,共
45、 37 页 -G5 Private Sub Command1_Click()CommonDialog1.ShowColor Label1.ForeColor=CommonDialog1.Color End Sub Private Sub Command2_Click()CommonDialog1.ShowOpen i=Shell(C:WINDOWSNOTEPAD.exe +CommonDialog1.FileName,vbNormalFocus)End Sub G6 rivate Sub bold_Click()Text1.FontBold=Not Text1.FontBold bold.Ch
46、ecked=Not bold.Checked End Sub Private Sub del_Click()Text1=End Sub Private Sub end_Click()End End Sub Private Sub font12_Click()Text1.FontSize=12 End Sub Private Sub font16_Click()Text1.FontSize=16 End Sub Private Sub Form_Load()bold.Checked=False italic.Checked=False End Sub Private Sub italic_Cli
47、ck()Text1.FontItalic=Not Text1.FontItalic italic.Checked=Not italic.Checked End Sub 名师资料总结-精品资料欢迎下载-名师精心整理-第 25 页,共 37 页 -Private Sub Text1_MouseDown(Button As Integer,Shift As Integer,X As Single,Y As Single)If Button=2 Then PopupMenu menu2 End Sub G7 Private Sub Command1_Click()CommonDialog1.ShowC
48、olor Label1.ForeColor=CommonDialog1.Color End Sub Private Sub Command2_Click()CommonDialog1.ShowOpen i=Shell(C:WINDOWSNOTEPAD.exe +CommonDialog1.FileName,vbNormalFocus)End Sub Private Sub return_Click(Index As Integer)Form6.Show Unload Form5 End Sub G8 G9 Private Sub 结束 _Click()End End Sub Private S
49、ub 删除最大值 _Click()max=List1.List(0)For i=1 To List1.ListCount-1 If max List1.List(i)Then min=List1.List(i):j=i Next i List1.RemoveItem j End Sub Private Sub 随机产生 _Click()名师资料总结-精品资料欢迎下载-名师精心整理-第 26 页,共 37 页 -Randomize For i=0 To 9 List1.List(i)=Int(Rnd*30+70)Next i End Sub Private Sub 添加数据 _Click()Li
50、st1.AddItem Int(Rnd*30+70)End Sub Private Sub 统计 _Click()Dim max%,min%,ave!,m%,n%max=List1.List(0)min=List1.List(0)ave=List1.List(0)m=0 n=0 For i=1 To List1.ListCount-1 If max List1.List(i)Then min=List1.List(i):n=i ave=ave+List1.List(i)Next i Form2.Text1=List1.List(n)Form2.Text2=List1.List(m)Form2.