您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 数据通信与网络 > 《VB程序设计》课堂例题总结
1《VB程序设计》课堂例题总结消息对话框的使用PrivateSubCommand1_Click()IfNotIsNumeric(Text1)Theni%=MsgBox(你输入的不是数字!,5+vbQuestion,警告)Ifi=4ThenText1.SetFocusElseEndElseMsgBox你好,,欢迎EndIfEndSub求圆的周长与面积PrivateSubForm_Click()Dimr!,s!,c!Constpi=3.14159‘用户声明常量r=InputBox(请输入园的半径:)s=pi*r*rc=2*pi*rMsgBox园的周长为:&c&面积为:&sEndSub计算三角形的面积PrivateSubCommand1_Click()Dima%,b%,c%,s#,area#Dimstr1$a=Val(InputBox(a:))b=Val(InputBox(b:))c=Val(InputBox(c:))s=(a+b+c)/2area=Sqr(s*(s-a)*(s-b)*(s-c))str1=area=&areaMsgBoxstr1Printstr1EndSub字体的放大与缩小随机放大1~3整数倍:Text1.FontSize=Text1.FontSize*Int(Rnd()*3+1)缩小的比例为任意:Text1.FontSize=Text1.FontSize*Rnd()连接与求和PrivateSubCommand1_Click()Text3.Visible=True2Text3.Text=Text1.Text&Text2.Text'也可以用Text1.Text+Text2.TextCommand1.Enabled=FalseEndSubPrivateSubCommand2_Click()Text3.Text=Val(Text1.Text)+Val(Text2.Text)EndSub字符串的替换方法一:(多个函数)PrivateSubCommand1_Click()i=InStr(Text1,Text2)'在Text1中查找出现Text2内容的位置k=i+Len(Text2)'定位右子串的起始位置Ls=Left(Text1,i-1)'取左子串Text4=Ls+Text3+Mid(Text1,k)'左子串连接替换为子串连接右子串实现替换'也可以将上面两步合并为Text4=Left(Text1,i-1)+Text3+Mid(Text1,k)EndSub方法二:(Replace函数)PrivateSubCommand2_Click()Text4=Replace(Text1,Text2,Text3)EndSub身份证的查找PrivateSubCommand1_Click()DimaAsString,nl%,yue%,ri%,b%,xb$a=Text1.Textnl=2010-Val(Mid(a,7,4))'nl=Year(Now())-Val(Mid(a,7,4))yue=Mid(a,11,2)ri=Mid(a,13,2)b=Val(Mid(a,17,1))IfbMod2=1Thenxb=GGElsexb=MMLabel3.Caption=nlLabel5.Caption=yue&月&ri&日Label7.Caption=你是&xbEndSub电话号码升位:将原来任意一个带区号的7位电话号码升到8位,在最前面加8。如020-1234567→020-81234567区号及电话号码确定:Left(s,4)&”8”&Right(s,7)区号及电话号码不确定:PrivateSubCommand1_Click()DimaAsString3a=Text1.Textb=InStr(a,-)Label3.Caption=Left(a,b)&8&Right(a,7)'Label3.Caption=Left(a,b)&8&mid(a,b+1)EndSub求一元二次方程的根PrivateSubCommand1_Click()Dima!,b!,c!,x1!,x2!,y!a=Val(Text1.Text):b=Val(Text2.Text):c=Val(Text3.Text)y=b^2-4*a*cIfy0ThenMsgBox(方程没有实数根)Elsex1=(-b+Sqr(y))/2/ax2=(-b-Sqr(y))/2/aLabel2.Caption=x1Label3.Caption=x2EndIfEndSub鸡兔同笼问题:鸡兔同笼,有h个头和f只脚,问有鸡兔各几只?判断条件:1.头和脚的数大于零2.脚数为偶数3.脚数是头数的2~4倍之间设:有鸡c只,兔r只h=c+rf=2c+4rc=(4h-f)/2r=(f-2h)/2PrivateSubCommand1_Click()DimHAsInteger,FAsIntegerDimCAsInteger,RAsIntegerH=Val(Text1.Text)F=Val(Text2.Text)IfF0AndH0AndFMod2=0AndF=H*2AndF=H*4ThenR=(F-2*H)/2C=H-RLabel3.Caption=Chicken:+Str(C)+vbCrLf+Rabbit:+Str(R)ElseMsgBox输入错EndIfEndSub4加密程序:将“明文”的大写字母随机向右移动1~3位,形成“密码”。PrivateSubCommand1_Click()Dimm%,s$Text1=UCase(Text1)'变成大写字母m=Int(Rnd*3)+1'随机为生加密移动的位数Label2.Caption=向右移动的位数是:&ms=Text1Ifs=AAndChr(Asc(s)+m)=ZThen'移动后不会超过字母Z的字母s=Chr(Asc(s)+m)'正常右移m位Elses=Chr(Asc(s)+m-26)'移动后超过Z的,要减去26EndIfLabel4=sEndSubPrivateSubForm_Load()RandomizeEndSub加密程序:根据单选按钮的数字,将明文中每个字母改为其后第n个字母。PrivateSubCommand1_Click()Dimm%,k%Text2=m=IIf(Option1.Value=True,1,IIf(Option2.Value=True,2,3))Fori=1ToLen(Text1)s=Mid(Text1,i,1)Ifa=Chr(Asc(s)+m)=zOrA=Chr(Asc(s)+m)=ZThens=Chr(Asc(s)+m)Elses=Chr(Asc(s)+m-26)EndIfText2=Text2&sNextiEndSub显示特殊图形PrivateSubForm_Click()5PrintFori=1To5PrintTab(i);String(6-i,★);Spc(6);String(i,▲)NextiEndSub登陆程序判断PrivateSubCommand1_Click()IfText1.Text=basic1234ThenMsgBox祝贺你,成功登录!ElseMsgBox对不起,口令错误,无法登录!Text1.Text=Text1.SetFocusEndIfEndSub判断能否构成三角形,若能则判断是否等边、等腰、直角三角形PrivateSubCommand1_Click()Dima%,b%,c%a=Val(InputBox(a=))a=Val(InputBox(a=))a=Val(InputBox(a=))Ifa0Andb0Andc0Anda+bcAnd_b+caAnda+cbThenMsgBox可以构成三角形Ifa=bAndb=cThens=s&,是等边三角形ElseIfa=bOra=cOrb=cThens=s&,等腰三角形ElseIfa^2+b^2=c^2Ora^2+c^2=b^2Orc^2+b^2=a^2Thens=s&,直角三角形EndIfElseMsgBox不可以构成三角形EndIfEndSub输入一个字符,判断该字符是字母字符、数字字符还是其它字符,并作相应的显示。方法一:PrivateSubCommand1_Click()DimstrcAsString*1strc=InputBox(请输入一个符号:)6IfUCase(strc)=AAndUCase(strc)=ZThenText1.Text=strc&是字母ElseIfstrc=0Andstrc=9ThenText1.Text=strc&是数字ElseText1.Text=strc&是其它字符EndIfEndSub方法二:PrivateSubCommand2_Click()DimstrcAsString*1strc=InputBox(请输入一个符号:)SelectCasestrcCaseaToz,AToZText1.Text=strc&是字母Case0To9Text1.Text=strc&是数字CaseElseText1.Text=strc&是其它字符EndSelectEndSub模拟计算器方法一:PrivateSubCommand1_Click()DimsInput1AsSingle,sInput2AsSingle,operAsString*1DimiErrorAsIntegersInput1=Val(Text1.Text)sInput2=Val(Text3.Text)oper=Trim(Text2)Ifoper=+ThenText4.Text=Str(sInput1+sInput2)ElseIfoper=-ThenText4.Text=Str(sInput1-sInput2)ElseIfoper=*ThenText4.Text=Str(sInput1*sInput2)ElseIfoper=/ThenIfsInput20ThenText4.Text=Str(sInput1/sInput2)ElseMsgBox(分母为零,出错)Text3.Text=Text3.SetFocusEndIf7ElseMsgBox(运算符出错,再输入)Text2.Text=Text2.SetFocusEndIfEndSub方法二:PrivateSubCommand2_Click()DimsInput1AsSingle,sInput2AsSingleDimiErrorAsIntegersInput1=Val(Text1.Text)sInput2=Val(Text3.Text)SelectCaseTrim(Text2)Case+Text4.Text=Str(sInput1+sInput2)Case-Text4.Text=Str(sInput1-sInput2)Case*Text4.Text=Str(sInput1*sInput2)Case/IfsInput20ThenText4.Text=Str(sInput1/sInput2)ElseMsgBox(分母为零,出错)Text3.Text=Text3.SetFocusEndIfCaseElseMsgBox(运算符出错,再输入)Text2.Text=Text2.SetFocusEndSelectEndSub开始运行如左下图所示,单击“出题”按钮后计算机自动出题,窗口如右下图所示,此时由用户输入加法运算答案,然后单击“判定”按钮。如果用户运算正确则提示“运算正确”,否则显示“运算错误”。再次按下“出题”就会再出一道题。PrivateSubForm_Load()RandomizeCommand1.Caption=出题EndSubPrivateSubCommand1_Click()IfCommand1.Caption=出题ThenText1=Int(Rnd*101):Text2=Int(Rnd*101)Text3.SetFocus8Command1.Caption=判定ElseIfVal(Text1)+V
本文标题:《VB程序设计》课堂例题总结
链接地址:https://www.777doc.com/doc-3374307 .html