VB试验重点章节.docx
《VB试验重点章节.docx》由会员分享,可在线阅读,更多相关《VB试验重点章节.docx(72页珍藏版)》请在冰豆网上搜索。
VB试验重点章节
3-1
DimfAsSingle,cAsSingle
DimanswerAsInteger
'PrivateSubCmdend_Click()
'UnloadMe
'Frmmain.Show
'EndSub
PrivateSubCmdhs_Click()
f=Val(Txthua.Text)
c=(f-32)*5/9
Txtshe.Text=Str$(c)
EndSub
PrivateSubCmdsh_Click()
c=Val(Txtshe.Text)
Ifc=0Then
Txthua.Text="32"
ElseIfc>0Then
f=(c*9#/5)+32
Txthua.Text=Str$(f)
Else
answer=MsgBox("非法数据!
",vbOKOnly+vbExclamation,"提示信息")
Ifanswer=vbOKThen
Txtshe.Text=""
Txthua.Text=""
Txtshe.SetFocus
EndIf
EndIf
EndSub
PrivateSubLabel1_Click()
EndSub
3-2
OptionExplicit
DimfsAsInteger
PrivateSubcmdGreat_Click()
Text1.Text=UCase(Text1.Text)
EndSub
PrivateSubcmdRecall_Click()
Text2.Text=Len(Text1.Text)
EndSub
PrivateSubcmdSmall_Click()
Text1.Text=LCase(Text1.Text)
EndSub
PrivateSubForm_Load()
EndSub
3-3
PrivateSubForm_Click()
Dims1AsSingle,s2AsSingle,s3AsSingle
Dimstr1AsString,str2AsString
Dimb1AsBoolean,b2AsBoolean,b3AsBoolean
Dimn1AsInteger,n2AsInteger,n3AsInteger
s1=18\4*4#^2/1.6
s2=25\3Mod3.2*Int(2.5)
str1=UCase("Xyz")&1234&LCase("abC")
b1=3>4Or5>4
b2=TrueAndFalse
n1=Int(-3.5)+Fix(3.5)+CInt(2.6)
n2=Len(Str(123)+"Hello")
s3=Sgn(7Mod3-4)+"0.75"
b3="aeoplane"<"airplane"
n3=Len("VB程序设计")
Prints1
Prints2
Printstr1
Printb1
Printb2
Printn1
Printn2
Prints3
Print"******************************"
Printb3
Printn3
EndSub
3-4
OptionExplicit
PrivateSubForm_Click()
DimxAsInteger,yAsSingle
ConstPI=3.1415926
x=30:
y=x*PI/180
Print"SIN(";x;")=";Sin(y)
Print"cos(";x;")=";Cos(y)
Print"tan(";x;")=";Sin(y)/Cos(y)
Print
x=45:
y=x*PI/180
Print"SIN(";x;")=";Sin(y)
Print"cos(";x;")=";Cos(y)
Print"tan(";x;")=";Sin(y)/Cos(y)
Print
x=90:
y=x*PI/180
Print"SIN(";x;")=";Sin(y)
Print"cos(";x;")=";Cos(y)
EndSub
3-5
OptionExplicit
PrivateSubCommand1_Click()
DimmAsInteger,aAsInteger,bAsInteger,cAsInteger,dAsInteger
m=Val(Text1.Text)
a=m\1000
b=m\100-a*10
c=(m\10)Mod10
d=mMod10
Text2.Text=CStr(d*1000+c*100+b*10+a)
EndSub
PrivateSubCommand2_Click()
End
EndSub
3-6
OptionExplicit
DimfsAsInteger
PrivateSubcmdGreat_Click()
DimiAsInteger
Randomize
i=Int(Rnd*(3-1+1))+1
txtInput.FontSize=i*txtInput.FontSize
cmdGreat.Enabled=False
cmdSmall.Enabled=True
EndSub
PrivateSubcmdRecall_Click()
txtInput.FontSize=fs
cmdSmall.Enabled=True
cmdGreat.Enabled=True
EndSub
PrivateSubcmdSmall_Click()
DimiAsInteger
Randomize
i=Int(Rnd*(3-1+1))+1
txtInput.FontSize=txtInput.FontSize/i
cmdSmall.Enabled=False
cmdGreat.Enabled=True
EndSub
PrivateSubForm_Load()
fs=txtInput.FontSize
EndSub
PrivateSubtxtInput_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
txtInput=UCase(Trim(txtInput))
cmdGreat.SetFocus
EndIf
EndSub
4-2
PrivateSubCommand1_Click()
DimstrAsString
str=InputBox("请输入文本信息","输入",_
"欢迎使用VB6")
Label1.Caption="欢迎使用VB6"
EndSub
PrivateSubCommand2_Click()
DimiAsInteger
i=MsgBox("你确实要退出吗?
",_
vbYesNo+vbExclamation,"退出警告")
Ifi=6ThenEnd
EndSub
4-3
OptionExplicit
PrivateSubCommand1_Click()
DimgzAsSingle,zgzAsSingle
gz=Val(txtGz)
Ifgz>=1600Then
zgz=gz*(1+0.1)
ElseIfgz>=1400Then
zgz=gz*(1+0.15)
Else
zgz=gz*(1+0.2)
EndIf
txtZGz=CStr(zgz)
EndSub
PrivateSubCommand2_Click()
txtGz=""
txtZGz=""
txtGz.SetFocus
EndSub
4-4
OptionExplicit
PrivateSubCommand1_Click()
DimtAsInteger,yAsSingle
t=Val(txtTime)
Ift<10Then
y=30
ElseIft<50Then
y=30+(t-10)*3
Else
y=30+(t-10)*2.5
EndIf
Ify<=150Then
txtCost=CStr(y)
Else
txtCost=CStr(150)
EndIf
EndSub
PrivateSubCommand2_Click()
txtTime=""
txtCost=""
txtTime.SetFocus
EndSub
4-5
PrivateSubCommand1_Click()
DimcjAsSingle,str1AsString
cj=Val(Text1.Text)
Ifcj>=90Then
Text2.Text="A"
ElseIfcj>=80Then
Text2.Text="B"
ElseIfcj>=70Then
Text2.Text="C"
ElseIfcj>=60Then
Text2.Text="D"
ElseIfcj>=0Then
Text2.Text="E"
EndIf
EndSub
PrivateSubCommand2_Click()
Text1.Text=""
Text2.Text=""
EndSub
PrivateSubCommand3_Click()
End
EndSub
PrivateSubText1_LostFocus()
IfNotIsNumeric(Text1.Text)OrVal(Text1.Text)<0Then
MsgBox"数据输入错误,重输"
Text1.Text=""
Text1.SetFocus
EndIf
EndSub
4-6
OptionExplicit
PrivateSubForm_Load()
DimYAsInteger,mAsInteger
DimdAsInteger,w1AsInteger
DimRqAsDate
Dimw2AsString
Rq=Date
Y=Year(Rq)
m=Month(Rq)
d=Day(Rq)
w1=Weekday(Rq)
SelectCasew1
Case1
w2="日"
Case2
w2="一"
Case3
w2="二"
Case4
w2="三"
Case5
w2="四"
Case6
w2="五"
Case7
w2="六"
EndSelect
Label1.Caption="今天是"&Y&"年"&m&"月"&d&"日"&"星期"&w2
EndSub
4-7
PrivateSubCommand1_Click()
DimY1AsInteger,M1AsInteger
DimflagAsBoolean,D1AsInteger,S1AsString
Y1=Val(txtYear)
M1=Val(txtMonth)
flag=(Y1Mod4=0AndY1Mod100<>0)OrY1Mod400=0
SelectCaseM1
Case1,2,3
D1=31:
S1="一季度"
IfM1=2Then
IfflagThen
D1=29
Else
D1=28
EndIf
EndIf
Case4,5,6
D1=30:
S1="二季度"
IfM1=5ThenD1=31
Case7,8,9
D1=31:
S1="三季度"
IfM1=9ThenD1=30
Case10,11,12
D1=31:
S1="四季度"
IfM1=11ThenD1=30
EndSelect
IfflagThen
txtRun="是闰年"
Else
txtRun="不是闰年"
EndIf
txtSea=S1
txtDays=Str(D1)
EndSub
PrivateSubCommand2_Click()
txtYear=""
txtMonth=""
txtSea=""
txtRun=""
txtDays=""
EndSub
4-8
PrivateSubCmdclear_Click()
txta.Text=""
Txtb.Text=""
Txtc.Text=""
Txtx1.Text=""
Txtx2.Text=""
txta.SetFocus
EndSub
PrivateSubCmdcup_Click()
a=Val(txta.Text)
b=Val(Txtb.Text)
c=Val(Txtc.Text)
disc=b*b-4*a*c
p=-b/(2*a)
q=Sqr(Abs(disc))/(2*a)
Ifdisc>=0Then
Txtx1=Format$(p+q,"####0.###")
Txtx2=Format$(p-q,"####0.###")
Else
Txtx1=Str$(p)&"+"&Str$(q)&"i"
Txtx2=Str$(p)&"-"&Str$(q)&"i"
EndIf
EndSub
PrivateSubCmdend_Click()
End
EndSub
PrivateSubtxta_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
IfNotIsNumeric(txta.Text)Then
MsgBox"数据非法,重输"
txta.Text=""
txta.SetFocus
Else
Txtb.SetFocus
EndIf
EndIf
EndSub
PrivateSubTxtb_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
IfNotIsNumeric(Txtb.Text)Then
MsgBox"数据非法,重输"
Txtb.Text=""
Txtb.SetFocus
Else
Txtc.SetFocus
EndIf
EndIf
EndSub
PrivateSubTxtc_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
IfNotIsNumeric(Txtc.Text)Then
MsgBox"数据非法,重输"
Txtc.Text=""
Txtc.SetFocus
Else
Cmdcup.SetFocus
EndIf
EndIf
EndSub
4-9
PrivateSubCmdclear_Click()
txtm1=""
txtm2=""
txtm3=""
Lblinfo.Caption=""
txtm1.SetFocus
EndSub
PrivateSubCmdsort_Click()
Dimm1AsInteger,m2AsInteger,m3AsInteger
m1=Val(txtm1.Text)
m2=Val(txtm2.Text)
m3=Val(txtm3.Text)
If(m1+m2+m3)\3>95Or(m1=100Andm2=100Andm3>=80)_
Or(m1=100Andm3=100Andm2>=80)Or_
(m1>=80Andm2=100Andm3=100)Then
Lblinfo.Caption="此学生奖学金为一等奖"
ElseIf(m1+m2+m3)\3>90Or(m1=100Andm2>=75Andm3>=75)_
Or(m1>=75Andm2=100Andm3>=75)Or_
(m1>=75Andm2>=75Andm3=100)Then
Lblinfo.Caption="此学生奖学金为二等奖"
ElseIfm1>=70Andm2>=70Andm3>=70Then
Lblinfo.Caption="此学生奖学金为三等奖"
Else
Lblinfo.Caption="此学生无奖学金"
EndIf
EndSub
PrivateSubtxtm1_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
IfNotIsNumeric(txtm1)Then
MsgBox"数据非法,重新输入",vbExclamation,"输入操作数1"
txtm1.Text=""
txtm1.SetFocus
Else
txtm2.SetFocus
EndIf
EndIf
EndSub
PrivateSubtxtm2_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
IfNotIsNumeric(txtm2)Then
MsgBox"数据非法,重新输入",vbExclamation,"输入操作数2"
txtm2.Text=""
txtm2.SetFocus
Else
txtm3.SetFocus
EndIf
EndIf
EndSub
PrivateSubtxtm3_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=13Then
IfNotIsNumeric(txtm3)Then
MsgBox"数据非法,重新输入",vbExclamation,"输入操作数3"
txtm3.Text=""
txtm3.SetFocus
Else
Cmdsort.SetFocus
EndIf
EndIf
EndSub
5-1
OptionExplicit
'方法一:
用三重循环,将三个一位数合并成一个三位数
'PrivateSubForm_Click()
'DimiAsInteger,jAsInteger,kAsInteger
'Fori=1To9
'Forj=0To9
'Fork=0To9
'Ifi*100+j*10+k=i^3+j^3+k^3Then
'PrintStr$(i*100+j*10+k);
'EndIf
'Nextk
''Nextj
''Nexti
'EndSub
'方法二:
用一重循环,将一个三位数分离为三个一位数
PrivateSubForm_Click()
DimnAsInteger
DimiAsInteger,jAsInteger,kAsInteger
Forn=100To999
i=nMod10
j=(n\10)Mod10
k=n\100
Ifn=i^3+j^3+k^3ThenPrintStr$(n);
Nextn
EndSub
5-2
PrivateSubCommand1_Click()
DimnAsInteger,iAsInteger,sAsString,sumAsInteger
Forn=1To1000
sum=0
s=Str(n)+"="
Fori=1Ton\2
IfnModi=0Then
sum=sum+i
s=s+Str(i)+"+"
EndIf
Nexti
Ifn=sumThen
s=Left(s,Len(s)-1)
Picture1.Prints
EndIf
Nextn
EndSub
PrivateSubCommand2_Click()
End
EndSub
5-3
PrivateSubForm_Click()
DimnAsInteger,x0AsSingle,x1AsSingle,sum!
x!
x=Val(Text1.Text)
n=1
sum=1
x1=1
Do
x0=x1
x1=x0*x/n
sum=sum+x1
n=n+1
LoopUntilx1<0.000001
Printsum
EndSub
5-4
PrivateSubcmdClear_Click()
txt1.Text=""
txt2.Text=""
txt1.SetFocus
EndSub