医学VB实验练习题复习资料.docx
《医学VB实验练习题复习资料.docx》由会员分享,可在线阅读,更多相关《医学VB实验练习题复习资料.docx(15页珍藏版)》请在冰豆网上搜索。
![医学VB实验练习题复习资料.docx](https://file1.bdocx.com/fileroot1/2023-1/14/266cc0cc-5bba-4b7c-88fe-ff4a3a04d11d/266cc0cc-5bba-4b7c-88fe-ff4a3a04d11d1.gif)
医学VB实验练习题复习资料
VB实验代码
实验一
1-1
PrivateSubCommand1_Click()
Text1=""
Text2=""
Text3=""
EndSub
PrivateSubCommand2_Click()
Text3=Val(Text1)/Val(Text2)
EndSub
PrivateSubCommand3_Click()
Text1=Val(Text3)*Val(Text2)
EndSub
PrivateSubCommand4_Click()
End
EndSub
实验二
2-1
PrivateSubCommand1_Click()
Label1=Text1
EndSub
PrivateSubForm_Load()
Text1.PasswordChar="*"
Text1.FontSize=20
Label1.FontSize=20
Label1.Alignment=2
Label1.BorderStyle=1
Label1.BackColor=vbWhite
Command1.FontSize=20
EndSub
2-2
PrivateSubCommand1_Click()
Label1="欢迎来到重庆"
Command1.Visible=False
Command2.Visible=True
EndSub
PrivateSubCommand2_Click()
Label1="welcometoChongQing"
Command2.Visible=False
Command1.Visible=True
EndSub
PrivateSubForm_Load()
Label1="欢迎来到重庆"
Label1.Alignment=2
Command1.Visible=False
Command2.Visible=True
EndSub
2-3
PrivateSubForm_Load()
Label1.BackColor=vbRed
Label2.BackColor=vbBlue
Label3.BackColor=vbGreen
Label4.BackColor=vbYellow
EndSub
PrivateSubLabel1_Click()
Text1.ForeColor=Label1.BackColor
EndSub
PrivateSubLabel2_Click()
Text1.ForeColor=Label2.BackColor
EndSub
PrivateSubLabel3_Click()
Text1.ForeColor=Label3.BackColor
EndSub
PrivateSubLabel4_Click()
Text1.ForeColor=Label4.BackColor
EndSub
PrivateSubText1_Change()
EndSub
实验三
3-1
PrivateSubCommand1_Click()
DimRAsSingle
ConstPiAsSingle=3.1415926
R=Val(Text1)
Label3=Int(2*Pi*R*1000+0.5)/1000
Label5=Int(Pi*R^2*1000+0.5)/1000
EndSub
3-2
PrivateSubCommand1_Click()
DimLxAsLong,UxAsLong
Lx=Val(Text1)
Ux=Val(Text2)
Randomize
Label3=Int((Ux-Lx)*Rnd+Lx)
EndSub
3-3
PrivateSubCommand1_Click()
DimxAsInteger
x=Val(InputBox("请输入一个整数!
"))
Picture1.Cls
IfxMod2=0Then
Picture1.Print"你输入的数";x;"是偶数"
Else
Picture1.Print"你输入的数";x;"是奇数"
EndIf
EndSub
3-4
3-5
PrivateSubCommand1_Click()
DimaAsDate
a=Now
Print"输出结果为:
"
Print
PrintFormat(a,"hh:
mm:
ssAMPMdddd,yyyy-mm-dd")
EndSub
PrivateSubForm_Load()
Form1.FontSize=18
Command1.FontSize=15
EndSub
实验四
4-1
Ifa=0Then
Ifb=0Then
MsgBox("方程无解")
Else
r1=-c/b
MsgBox("方程有一个根为:
"+Str(r1))
EndIf
Else
d=b*b-4*a*c
Ifd>0Then
r1=(-b+Sqr(d))/(2*a)
r2=(-b-Sqr(d))/(2*a)
MsgBox("方程有两个根为:
"+Str(r1)+Chr(13)+""+Str(r2))
ElseIfd=0Then
r1=-b/(2*a)
MsgBox("方程有两个相同根为:
"+Str(r1))
Else
MsgBox("方程无实根")
EndIf
EndIf
EndSub
PrivateSubCommand2_Click()
End
EndSub
4-2
PrivateSubCommand1_Click()
DimaAsDouble,bAsDouble
DimysAsString*1
a=Val(Text1)
b=Val(Text3)
ys=Text2
SelectCaseys
Case"+"
Label4=a+b
Case"-"
Label4=a-b
Case"*"
Label4=a*b
Case"/"
Ifb=0Then
MsgBox"除数不能为0!
重新输入"
Text3.SetFocus
Text3.SelStart=0
Text3.SelLength=Len(Text3)
Else
Label4=a/b
EndIf
EndSelect
EndSub
4-3
PrivateSubCommand1_Click()
DimaAsSingle
DimbAsSingle
DimcAsSingle
DimtAsSingle
a=Val(InputBox("请输入第一个数:
"))
b=Val(InputBox("请输入第二个数:
"))
c=Val(InputBox("请输入第三个数:
"))
Label1="输入的三个数为:
"&a&"、"&b&"、"&c
Ifaa=b:
b=t
Ifat=c:
c=b:
b=a:
a=t
Else
Ifbb=c:
c=t
EndIf
Label2="排序后的结果为:
"&a&"、"&b&"、"&c
EndSub
4-4
Text1.SelLength=1
EndSub
PrivateSubCommand2_Click()
DimzfAsString
DimzaAsInteger
zf=Text1
za=Asc(zf)
SelectCaseza
CaseAsc("A")ToAsc("Z")
Label3=Chr(za+32)
CaseAsc("a")ToAsc("z")
Label3=Chr(za-32)
CaseElse
Label3="*"
EndSelect
Text1.SetFocus
Text1.SelStart=0
Text1.SelLength=1
EndSub
PrivateSubForm_Load()
Text1.MaxLength=1
EndSub
实验五
5-1
PrivateSubCommand1_Click()
DimnAsInteger,sumAsSingle
n=0
sum=0
Do
n=n+1
sum=sum+n^2
LoopUntilsum>1000
Label2="n="&n
EndSub
5-2
PrivateSubCommand1_Click()
DimnAsInteger,iAsInteger
DimsumAsSingle,midAsSingle
n=Val(InputBox("请输入n值"))
sum=0
mid=0
Fori=1Ton
mid=mid+i
sum=sum+mid
Nexti
Label4=n
Label5=sum
EndSub
5-3
PrivateSubCommand1_Click()
DimxAsInteger,maxAsInteger,minAsInteger
DimkAsInteger
Randomize
Picture1.Cls
max=100
min=999
Fork=1To10
x=Int(Rnd*900+100)
IfmaxIfmin>xThenmin=x
Picture1.Printx
Nextk
Text1=max
Text2=min
EndSub
5-4
实验六
6-1
PrivateSubCommand1_Click()
Dimcj(1To10)AsSingle
DimsumAsSingle,iAsInteger
sum=0
Cls
Fori=1To10
cj(i)=Val(InputBox("请输入第"&i&"位同学的成绩:
"))
sum=sum+cj(i)
Nexti
Print"这10位同学的成绩如下:
"
Fori=1To10
Printcj(i);
IfiMod5=0ThenPrint
Nexti
Print
Print"平均成绩为:
";Spc(3);sum/i
EndSub
6-2
PrivateSubCommand1_Click()
Dima(5,5)AsInteger
DimsAsInteger
Cls
Fori=1To5
PrintTab(10);
Forj=1To5
a(i,j)=Int(Rnd*10)
Printa(i,j);
Nextj
Print
Nexti
s=0
Fori=2To5
Forj=1Toi-1
s=s+a(i,j)
Nextj,i
Print
Print"下三角元素之和为:
";s
s=0
Fori=1To4
Forj=i+1To5
s=s+a(i,j)
Nextj,i
Print"上三角元素之和为:
";s
s=0
Fori=1To5
s=s+a(i,i)
Nexti
Print"主对角线元素之和为:
";s
EndSub
6-3
PrivateSubCommand1_Click()
ReDimsl(1To6)
DimiAsInteger,jAsInteger,tAsInteger
Cls
Fori=1To6
sl(i)=Int(90*Rnd+10)
Nexti
Fori=1To5
Forj=i+1To6
Ifsl(i)t=sl(i)
sl(i)=sl(j)
sl(j)=t
EndIf
Nextj
Nexti
Print"原数据序列是:
"
Fori=1To6
Printsl(i);
Nexti
Print
EndSub
PrivateSubCommand2_Click()
ReDimPreservesl(1To7)
DimnAsInteger
n=Val(InputBox("请输入一个两位数:
"))
Print"插入的数为:
";n
Fori=6To1Step-1
IfnExitFor
Else
sl(i+1)=sl(i)
EndIf
Nexti
sl(i+1)=n
Print"新的数据序列是:
"
Fori=1To7
Printsl(i);
Next
EndSub
6-4
实验七
7-1
7-2
PrivateSubCommand1_Click()
DimaAsSingle,bAsSingle,cAsSingle
a=Val(InputBox("请输入第一个数:
"))
b=Val(InputBox("请输入第二个数:
"))
c=Val(InputBox("请输入第三个数:
"))
Print"输入的三个数是:
";a;b;c
Print"其中的最大值是:
";Max3(a,b,c)
EndSub
FunctionMax3(ByValXAsSingle,ByValYAsSingle,ByValZAsSingle)AsSingle
IfX>=YAndX>=ZThen
Max3=X
ElseIfY>=ZThen
Max3=Y
Else
Max3=Z
EndIf
EndFunction
7-3
PrivateSubCommand1_Click()
DimaAsString*1
a=InputBox("请输入一个字符:
")
Print"输入的字符是:
";a
Print"它是:
";
CallPd(a)
EndSub
SubPd(ByValOsAsString)
DimaAsInteger
a=Asc(Os)
SelectCasea
Case48To57
Print"数字字符"
Case65To90
Print"大写字母"
Case97To122
Print"小写字母"
CaseElse
Print"其它字符"
EndSelect
EndSub
7-4
PrivateSubCommand1_Click()
DimaAsLong
a=Val(Text1)
Ifws1(a)=TrueThen
Label2=a&"是完数"
Else
Label2=a&"不是完数"
EndIf
EndSub
PrivateSubCommand2_Click()
DimaAsLong
DimresAsBoolean
a=Val(Text1)
Ws2a,res
Ifres=TrueThen
Label2=a&"是完数"
Else
Label2=a&"不是完数"
EndIf
EndSub
SubWs2(ByValXAsLong,ByRefYAsBoolean)
DimiAsInteger,sumAsSingle
Y=False
sum=1
Fori=2ToInt(X/2)
IfXModi=0Thensum=sum+i
Nexti
IfX=sumThenY=True
EndSub
Functionws1(ByValXAsLong)AsBoolean
DimiAsInteger,sumAsSingle
ws1=False
sum=1
Fori=2ToInt(X/2)
IfXModi=0Thensum=sum+i
Nexti
IfX=sumThenws1=True
EndFunction