《河南大學10級VB程序設計課后習題參考答案.doc》由會員分享,可在線閱讀,更多相關《河南大學10級VB程序設計課后習題參考答案.doc(49頁珍藏版)》請在裝配圖網(wǎng)上搜索。
河南大學09級VB程序設計課后習題參考答案(編碼)
2-1
PrivateSubForm_Click()
Cls
EndSub
PrivateSubForm_DblClick()
FontSize=40
FontName="隸書"
ForeColor=vbRed
Print"熱烈慶祝"
FontSize=60
FontName="宋體"
ForeColor=vbGreen
Print"河南大學運動會召開"
EndSub
2-2
PrivateSubCommand1_Click()
Label1.MoveWidth*Rnd,Height*Rnd
EndSub
2-3
PrivateSubCommand1_Click()
Label1.Visible=True
EndSub
PrivateSubCommand2_Click()
Label1.Visible=False
EndSub
2-4
PrivateSubTimer1_Timer()
Text1.Text=Now
Caption=Text1.Text
EndSub
2-5
PrivateSubTimer1_Timer()
IfBackColor=vbRedThen
BackColor=vbYellow
Else
BackColor=vbRed
EndIf
EndSub
2-6
PrivateSubTimer1_Timer()
IfLabel1.Left
-Label1.HeightThen
Label1.Top=Label1.Top-100
Else
Label1.Top=Form1.Height
EndIf
EndSub
3-2
PrivateSubForm_Click()
DimnAsInteger
Randomize
n=Int((90*Rnd)+10)
Printn
EndSub
3-3
PrivateSubTimer1_Timer()
Cls
Print"河南大學環(huán)境與規(guī)劃學院"
FontSize=Int(71*Rnd+10)
EndSub
3-4
PrivateSubCommand1_Click()
n=Text1.Text
Text2.Text=Hex(Asc(Right(Text1.Text,1)))
EndSub
PrivateSubCommand2_Click()
Text1.Text=""
Text2.Text=""
Text1.SetFocus
EndSub
3-5
PrivateSubCommand1_Click()
DimmAsDate
m=Text1.Text
n=Date
Text2.Text=n-m
EndSub
PrivateSubCommand2_Click()
Text1.Text=""
Text2.Text=""
Text1.SetFocus
EndSub
3-6
DimbAsString
DimnAsString
DimmAsString
PrivateSubCommand1_Click()
m=Left(Text1,1)
n=Right(Text1,1)
b=Mid(Text1,2,1)
Text2.Text=n&b&m
EndSub
PrivateSubCommand2_Click()
Text1.Text=""
Text2.Text=""
Text1.SetFocus
EndSub
3-7
PrivateSubForm_click()
r=InputBox("請輸入字符")
PrintLeft(r,1)&Mid(r,3,1)&Right(r,1)
EndSub
3-8
PrivateSubTimer1_Timer()
Label1.FontSize=Rnd*40+10
Label1.BackColor=RGB(Rnd*256,Rnd*256,Rnd*256)
Label1.ForeColor=QBColor(Int(16*Rnd))
EndSub
3-10
PrivateSubForm_Click()
r!=InputBox("請輸入您的工資:")
a=r\100
b=(r-100*a)\50
c=(r-100*a-b*50)\10
d=(r-100*a-b*50-c*10)\5
e=(r-100*a-b*50-c*10-d*5)\2
f=r-100*a-b*50-c*10-d*5-e*2
Print"您的工資是:"&r
Print"100元張數(shù):"&a
Print"50元張數(shù):"&b
Print"10元張數(shù):"&c
Print"5元張數(shù):"&d
Print"2元張數(shù):"&e
Print"1元張數(shù):"&f
EndSub
4-1
PrivateSubCommand1_Click()
DimpAsSingle,rAsSingle,nAsSingle
p=Text1
r=Text2
n=Text3
s=p*(1+r)^n
MsgBox"您的本息是:"&Round(s,2)
EndSub
4-2
PrivateSubForm_click()
DimmaxAsSingle,minAsSingle
a!=InputBox("請輸入第一個實數(shù)")
b!=InputBox("請輸入第二個實數(shù)")
Ifa>bThen
Printa,b
Else
Printb,a
EndIf
EndSub
4-3
PrivateSubForm_click()
Cls
a!=InputBox("請輸入年份")
IfaMod4=0AndaMod100<>0&aMod400=0Then
Print"您剛剛輸入的年份為閏年!"
Else
Print"您剛剛輸入的年份不是閏年!"
EndIf
EndSub
4-4
PrivateSubForm_click()
a!=InputBox("a=")
b!=InputBox("b=")
c!=InputBox("c=")
d!=b*b-4*a*c
Ifd>=0Then
m!=(-b+Sqr(d))/(2*a)
n!=(-b-Sqr(d))/(2*a)
Print"X1=";m,",","X2=";n
Else
r=(-b)/(2*a)
i=Sqr(Abs(-d))/(2*a)
Print"X1=";r&"+"&Abs(i)&"i",",","X2=";r&"-"&Abs(i)&"i"
EndIf
EndSub
4-4(2)
Dim a, b, c, d As Single
a = InputBox("輸入數(shù)據(jù)")
b = InputBox("輸入數(shù)據(jù)")
c = InputBox("輸入數(shù)據(jù)")
d = b ^ 2 - 4 * a * c
If d >= 0 Then
X1 = (-b + Sqr(d)) / (2 * a)
X2 = (-b - Sqr(d)) / (2 * a)
Print "x1=" & X1 & " " & "x2=" & X2
Else
x3 = -b / (2 * a) & "+" & Sqr(-d) / (2 * a) & "i"
x4 = -b / (2 * a) & "-" & Sqr(-d) / (2 * a) & "i"
Print "x3=" & x3 & " " & "x4=" & x4
End If
4-5
PrivateSubCommand1_Click()
Dimx!
x=Text1
Ifx<60Then
Text2="重考!"
ElseIfx<70Then
Text2="及格!"
ElseIfx<80Then
Text2="中等!"
ElseIfx<90Then
Text2="良好!"
Else
Text2="優(yōu)秀!"
EndIf
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
EndSub
4-6
PrivateSubCommand1_Click()
Dimx!
x=Text1
SelectCasex
CaseIs<60
Text2="重考!"
CaseIs<70
Text2="及格!"
CaseIs<80
Text2="中等!"
CaseIs<90
Text2="良好!"
CaseElse
Text2="優(yōu)秀!"
EndSelect
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
EndSub
4-7
DimsAsString
PrivateSubForm_Click()
s=InputBox("請輸入年齡")
Ifs<30Then
Print"您的年齡為";s,",","請參加長跑運動"
ElseIfs<40Then
Print"您的年齡為";s,",","請參加跳繩運動"
ElseIfs<50Then
Print"您的年齡為";s,",","請參加競走運動"
Else
Print"您的年齡為";s,",","請參加慢跑運動"
EndIf
EndSub
4-8
PrivateSubForm_Click()
Forn=2To100Step2
Printn;
IfnMod20=0ThenPrint
Nextn
EndSub
4-9
PrivateSubForm_click()
Forn=100To1000
IfnMod3=2AndnMod5=3AndnMod7=2ThenPrintn;
Nextn
Print
EndSub
4-10
PrivateSubForm_click()
m=0:n=0
Do
r%=InputBox("請輸入學生成績")
m=m+r
n=n+1
LoopUntilr=-1
Print"學生平均成績=";(m+1)/(n-1)
EndSub
4-11
PrivateSubForm_click()
DimaAsInteger
DimsAsInteger
a=1
Do
s=s+(-1)^(a+1)*a
a=a+1
LoopUntila>100
Print"1-2+3-4+5-6+…+99-100=";s
EndSub
4-12
PrivateSubForm_Click()
DimnAsInteger
DimmAsInteger
DimsAsInteger
m%=InputBox("請輸入正整數(shù)m(m>=1)","階乘")
n%=InputBox("請輸入正整數(shù)n(n>=m)","階乘")
Form=mTon
t=1
Fori=1Tom
t=t*i
Nexti
s=s+t
Nextm
Print"m!+(m+1)!+…+n!=";s
EndSub
4-13
PrivateSubForm_click()
Dima%
Dimb%
Dimc%
Fora=1To20
Forb=1To20
Forc=1To20
Ifa*a+b*b=c*cThen
Printa,b,c
EndIf
Nextc
Nextb
Nexta
EndSub
4-14
PrivateSubForm_click()
Forn=1To500
Fori=1Ton-1
IfnModi=0Thens=s+i
Nexti
Ifs=nThenPrinti;
s=0
Nextn
Print
EndSub
4-15
PrivateSubForm_click()
DimiAsInteger
DimnAsLong
n=1
t=1
DoWhilet>0.00001
e=e+t
i=i+1
n=n*i
t=1/n
Loop
Print"e的近似值是";e
EndSub
5—1
Private Sub Form_Click()
Dim i%, x(1 To 5) As String
For i = LBound(x) To UBound(x)
x(i) = InputBox(" x ( " & i & " ) ")
Next i
For i = UBound(x) To LBound(x) Step -1
Print x(i)
Next i
Print
End Sub
5-2
PrivateSubForm_Click()
Dimi%,s
s=Array(1,2,3,4,5,6,7,8,9,10)
Fori=0To9
Ifs(i)Mod3=0Then
Prints(i);i
Else
Print""
EndIf
Nexti
Print
EndSub
5-3
OptionBase1
PrivateSubCommand1_Click()
Dimx()
x=Array(1,2,3,4,5,6,7,8,9,10)
t=x(1)
Fori=1To9
x(i)=x(i+1)
Printx(i);
Next
x(10)=t
Printx(i)
EndSub
PrivateSubCommand2_Click()
Dimx()
x=Array(1,2,3,4,5,6,7,8,9,10)
t=x(10)
Fori=9To1Step-1
x(i+1)=x(i)
Nexti
x(1)=t
Fori=1To10
Printx(i);
Nexti
EndSub
5-4
PrivateSubForm_Click()
Dima(5,5)
Fori=1To5
Sum=0
Forj=1To5
a(i,j)=Int(Rnd*51+10)
Sum=Sum+a(i,j)
Printa(i,j);
Nextj
PrintSum
Nexti
Print
Fori=1To5
Forj=1To5
Ifi=jThenPrinta(i,j);
Nextj
Nexti
Print
EndSub
5-5
PrivateSubForm_Click()
Dima()AsInteger
DimMaxAsInteger,iMaxAsInteger,jMaxAsInteger
DimMinAsInteger,iMinAsInteger,jMinAsInteger
M=InputBox("輸入M")
N=InputBox("輸入N")
ReDima(M,N)
Fori=1ToM
Forj=1ToN
a(i,j)=Int(Rnd*900)+100
Nextj
Nexti
Fori=1ToM
Forj=1ToN
Printa(i,j);
Nextj
Print
Nexti
Max=a(1,1):Min=a(1,1)
Fori=1ToM
Forj=1ToN
Ifa(i,j)>MaxThenMax=a(i,j):iMax=i:jMax=j
fa(i,j)0Then
Image1.Picture=LoadPicture("c:\圖片\p2.JPG")
Image2.Picture=LoadPicture("c:\圖片\p1.JPG")
Else
Image1.Picture=LoadPicture("c:\圖片\p1.JPG")
Image2.Picture=LoadPicture("c:\圖片\p2.JPG")
EndIf
EndSub
PrivateSubForm_Load()
Image1.Picture=LoadPicture("c:\圖片\p1.JPG")
Image2.Picture=LoadPicture("c:\圖片\p2.JPG")
EndSub
7-3
PrivateSubTimer1_Timer()
Form1.Picture=Image3.Picture
Image3.Picture=Image2.Picture
Image2.Picture=Image1.Picture
Image1.Picture=Form1.Picture
EndSub
7-4
PrivateSubForm_Load()
Timer1.Enabled=False
EndSub
PrivateSubTimer1_Timer()
IfImage2.Top>=-Image2.HeightThen
Image2.Top=Image2.Top-100
Else
Image2.Top=-Image2.Height
EndIf
EndSub
7-5
PrivateSubForm_Load()
HScroll1.Min=0
HScroll1.Max=Image1.Width
Image1.Width=0
EndSub
PrivateSubHScroll1_Scroll()
Image1.Width=HScroll1
Label1="當前圖片寬度:"&HScroll1.Value
EndSub
7-6
PrivateSubCommand1_Click()
Fori=ProgressBar1.MinToProgressBar1.Max
ProgressBar1.Value=i
(Forj=1To20000
Nextj)(延時)
Nexti
EndSub
7-7
PrivateSubCommand1_Click()
a=IIf(Option1,"男","女")
b=IIf(Option3,"碩士","博士")
c=IIf(Check1,"音樂","")
d=IIf(Check2,"演講","")
e=IIf(Check3,"體育","")
f=IIf(Check4,"集郵","")
Text2="姓名:"&Text1.Text&","&"性別:"&a&","&"學歷:"&b&","&"特長:"&c&","&d&","&e&","&f
EndSub
PrivateSubCommand2_Click()
Text1=""
Text2=""
Text1.SetFocus
EndSub
7-8
PrivateSubForm_Load()
Combo1.AddItem"河南省"
Combo1.AddItem"開封市"
Combo1.AddItem"河南大學"
Combo1.AddItem"金明校區(qū)"
Combo1.AddItem"環(huán)境與規(guī)劃學院"
Combo1.AddItem"09級"
Combo1.AddItem"地理科學專業(yè)"
Combo1.AddItem"劉彬"
Combo1.AddItem"畢業(yè)于"
Combo1.AddItem"湖南省"
Combo1.AddItem"衡山縣"
Combo1.AddItem"第二中學"
Combo1.AddItem"218班"
EndSub
PrivateSubCombo1_Click()
List1.AddItemCombo1.Text
EndSub
PrivateSubList1_DblClick()
List1.RemoveItemList1.ListIndex
EndSub
8-1
DimxAsSingle,yAsSingle
PrivateSubPt1_Click()
Pt1.Scale(0,100)-(100,0)
Pt1.DrawWidth=2
Forx=0To100Step0.02
Pt1.PSet(x,2),vbRed
Nextx
Fory=0To100Step0.02
Pt1.PSet(2,y),vbRed
Nexty
Pt1.CurrentX=2
Pt1.CurrentY=5
Pt1.Print"(0,0)"
EndSub
8-2
PrivateSubP1_Click()
P1.Scale(0,100)-(100,0)
P1.FillStyle=7
P1.DrawWidth=5
P1.FillColor=vbGreen
P1.Line(10,90)-(90,10),vbRed,B
EndSub
8-3
PrivateSubForm_click()
Scale(-100,100)-(100,-100)
Line(-100,0)-(100,0)
Line(0,-100)-(0,100)
CurrentX=2
CurrentY=6
Print"(0,0)"
EndSub
8-4
PrivateSubForm_click()
Scale(-100,100)-(100,-100)
Line(-100,0)-(100,0)
Line(0,-100)-(0,100)
Forx=-100To100Step0.02
y=10*Sin(x)-10*Cos(2*x)
PSet(x,y),vbRed
Nextx
EndSub
8-7
PrivateSubForm_Click()
Timer1.Enabled=True
EndSub
PrivateSubTimer1_Timer()
Randomize
Scale(-120,120)-(120,-120)
DrawWidth=2
Ifr<100Then
r=r+2
Forr=0To100
Circle(0,0),r,RGB(255*Rnd,255*Rnd,255*Rnd)
Nextr
EndIf
EndSub
9-1
PrivateSubText1_KeyPress(KAsInteger)
K=K+3
EndSub
9-2
PrivateSubText1_KeyPress(KAsInteger)
SelectCaseK
Case48To57
K=K
CaseElse
K=0
EndSelect
EndSub
9-6
PrivateSubCommand1_MouseMove(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
MousePointer=5
EndSub
PrivateSubText1_MouseMove(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
MousePointer=11
EndSub
PrivateSubPicture1_Click()
MouseIcon=Picture1.MouseIcon
Picture1.MouseIcon=Image1.MouseIcon
Image1.MouseIcon=Form1.MouseIcon
EndSub
9-7
SubForm_MouseMove(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
IfShift=vbCtrlMaskAndButton=vbLeftButtonThenPrint"Ctrl+左鍵+移動"
IfShift=vbAltMaskAndButton=vbRightButtonThenPrint"Alt+右鍵+移動"
EndSub
SubForm_MouseUp(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
Cls
EndSub
9-8
SubPicture1_MouseMove(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)
IfButton=vbLeftButtonThenPicture1.PrintStr(X)+","+Str(Y)
BackColor=RGB(Rnd*256,Rnd*256,Rnd*256)
EndSub
10-1
PrivateSubCommand1_Click()
Open"d:\data1.txt"ForOutputAs#1
Write#1,"601","楊大昌";"4550"
Write#1,"603","李文圣";"3886"
Write#1,"605","張燕";"2235"
Close#1
EndSub
PrivateSubCommand2_Click()
Open"d:\data1.txt"ForInputAs#2
DoWhileNotEOF(2)
Input#2,X1,X2,x3
PrintX1,X2,x3
Loop
Close
EndSub
10-2
PrivateSubCommand1_Click()
Open"d:\data1.txt"ForAppendAs#3
Write#3,Text1,Text2,Text3
Text1=""
Text2=""
Text3=""
Close
EndSub
PrivateSubCommand2_Click()
Close
End
EndSub
10-5
PrivateSubForm_Click()
DimsumAsLong
Forn=1To100Step2
sum=sum+n
Nextn
Printsum
EndSub
PrivateSubForm_Load()
Open"d:\data5.txt"ForOutputAs#1
Forn=1To100Step1
Print#1,n;
Nextn
Close
EndSub
10-6
PrivateSubForm_Click()
DimsumAsInteger
Forn=10To100Step10
sum=sum+n
Next
Printsum
EndSub
PrivateSubForm_Load()
Open"d:\data6.dat"ForRandomAs#1
Forn=1To100
Put#1,,n
Next
EndSub
11-1
PrivateSubCommand1_Click()
CommonDialog1.Action=3
Text1.BackColor=CommonDialog1.Color
EndSub
PrivateSubCommand2_Click()
CommonDialog1.Flags=263
CommonDialog1.Action=4
Text1.FontSize=CommonDialog1.FontSize
Text1.FontName=CommonDialog1.FontName
Text1.FontBold=CommonDialog1.FontBold
Text1.FontStrikethru=CommonDialog1.FontStrikethru
Text1.ForeColor=CommonDialog1.Color
Text1.FontUnderline=CommonDialog1.FontUnderline
EndSub
PrivateSubCommand3_Click()
CommonDialog1.Action=2
EndSub
11-2
PrivateSubCommand1_Click()
CD1.InitDir="C:\DocumentsandSettings\Administrator\桌面"
CD1.Filter="圖片|*.jpg|文本文件|*.txt"
CD1.ShowOpen
IfCD1.FilterIndex=1Then
Image1.Picture=LoadPicture(CD1.FileName)
Else
OpenCD1.FileNameForInputAs#1
DoWhileNotEOF(1)
LineInput#1,n
Text1=Text1+n+vbNewLine
Loop
EndIf
EndSub
11—3
Private Sub Form_Load()
Text1.Text = "河南大學首頁"
Text1.FontName = "黑體"
Text1.FontSize = 20
End Sub
Private Sub Form_Resize()
Text1.Left = 50
Text1.Top = 50
Text1.Width = Width - 500
Text1.Height = Height - 1000
End Sub
Private Sub mnublue_Click()
Text1.ForeColor = vbBlue
End Sub
Private Sub mnuclear_Click()
Text1.Text = ""
End Sub
Private Sub mnugreen_Click()
Text1.ForeColor = vbGreen
End Sub
Private Sub mnured_Click()
Text1.ForeColor = vbRed
End Sub
11-4
PrivateSubForm_Resize()
Text1.Left=50
Text1.Top=50
Text1.Width=Width-2000
Text1.Height=Height-5000
EndSub
PrivateSubmnuName_Click(IndexAsInteger)
SelectCaseIndex
Case5
Text1.FontName="宋體"
Case10
Text1.FontName="黑體"
Case15
Text1.FontName="隸書"
EndSelect
EndSub
11-5
Private Sub mnuinput_Click()
Dim a As Integer
a = InputBox("輸入整數(shù)")
If a < 0 Then MsgBox ("輸入無效")
Label4.Caption = a
End Sub
Private Sub mnuoct_Click()
Dim a As Integer
a = InputBox("輸入整數(shù)")
If a < 0 Then MsgBox ("輸入無效")
Label5.Caption = Oct(a)
End Sub
Private Sub mnuhex_Click()
Dim a As Integer
a = InputBox("輸入整數(shù)")
If a < 0 Then MsgBox ("輸入無效")
Label6.Caption = Hex(a)
End Sub
11-6
PrivateSubForm_Load()
Text1.Width=Width
Text1.Height
鏈接地址:http://www.820124.com/p-7972467.html