VB第4-6章课后编程题代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

代码:第四章:Page。108。4(1),分段函数:PrivateSubCommand1_Click()DimxAsSingle,yAsSinglex=Val(InputBox(assignanumbertox,Numberx,0))Ifx1Theny=xElseIfx10Andx=1Theny=3*x-2Elsey=4*x-12EndIfPicture1.Printwhenx=;x;theny=;yEndSubPrivateSubCommand2_Click()EndEndSub4(2):PrivateSubCommand1_Click()RandomizeFori=1To100n=Int(Rnd*(99-10+1)+10)Text1=Text1+CStr(n)+SelectCasenCaseIs=40m1=m1+1Case41To70m2=m2+1CaseIs70m3=m3+1EndSelectNextiPicture1.Print小于等于40的数有:;m1Picture1.Print大于40小于等于70的数有:;m2Picture1.Print大于70的数有:;m3EndSubPrivateSubCommand2_Click()Text1=Picture1.ClsEndSubPrivateSubForm_Load()Text1=Picture1.ClsEndSub4(3):3和7的倍数:PrivateSubCommand1_Click()Fori=1To100IfiMod3=0Thenn1=n1+1EndIfIfiMod7=0Thenn2=n2+1EndIfNextiPrint1-100中是3的倍数的有:;n1Print1-100中是7的倍数的有:;n2EndSubPrivateSubCommand2_Click()EndEndSub4(4):分开奇偶数DimiAsInteger,jAsIntegerDimn(1To20)AsIntegerDimmAsIntegerPrivateSubCommand1_Click()RandomizeFori=1To20n(i)=Int(Rnd*(999-100+1)+100)Picture1.Printn(i);m=m+1IfmMod4=0ThenPicture1.PrintNextiEndSubPrivateSubCommand2_Click()Forj=1To20Ifn(j)Mod2=0ThenPicture2.Printn(j);k=k+1IfkMod4=0ThenPicture2.PrintElsePicture3.Printn(j);t=t+1IftMod4=0ThenPicture3.PrintEndIfNextjEndSubPrivateSubCommand3_Click()EndEndSub课程成绩一览表:DimiAsInteger,nAsIntegerDimCmrkAsString,sumAsIntegerPrivateSubCommand1_Click()Print课程名称;Spc(3);分数n=Val(InputBox(请输入课程总数))Fori=1TonCmrk=InputBox(输入课程名称及分数,课目及分数,数学86)m=InStr(Cmrk,)k=Len(Cmrk)C=Left(Cmrk,m-1)mrk=Right(Cmrk,k-m)PrintC;Tab(12);mrksum=sum+mrkNextiPrintPrint总分;sumPrint平均分;Format(sum/n,0.0)EndSub4(5):输出素数PrivateSubCommand1_Click()Fori=100To300Forj=2Toi-1IfiModj=0ThenExitForNextjIfj=iThenPrinti;n=n+1IfnMod5=0ThenPrintEndIfNextiEndSubPrivateSubCommand2_Click()EndEndSub4(6):水仙花数PrivateSubCommand1_Click()PrintPrintFori=100To999t=Int(i/100)m=Int((i-t*100)/10)f=Int(i-t*100-m*10)Ifi=t^3+m^3+f^3ThenPrinti;EndIfNextiEndSubPrivateSubCommand2_Click()EndEndSub4(7):判别回文数PrivateSubCommand1_Click()m=Text1.Textn=Len(m)Fori=1Ton\2IfMid(m,i,1)Mid(m,n-i+1,1)ThenExitForEndIfNextiIfi=n\2+1ThenText2=此数为回文数ElseText2=此数不是回文数EndIfEndSubPrivateSubCommand2_Click()EndEndSubPrivateSubForm_Load()Text1=Text2=EndSub4(8):PrivateSubCommand1_Click()DimxAsString,yAsStringDimy1AsInteger,y2AsInteger,y3AsInteger,y4AsIntegerx=Text1.TextFori=1ToLen(x)y=Asc(Mid(x,i,1))If(y=90Andy=65)Or(y=122Andy=65)Theny1=y1+1ElseIfy=48Andy=57Theny2=y2+1ElseIfy=32Theny3=y3+1Elsey4=y4+1EndIfNextiPicture1.Print字母的个数为:;y1Picture1.Print数字的个数为:;y2Picture1.Print空格的个数为:;y3Picture1.Print其他字符的个数为:;y4EndSub4(10):PrivateSubForm_Click()DimiAsInteger,jAsIntegerDimxAsInteger,sumAsInteger,averAsIntegeri=InputBox(请输入所修学科数)Forj=1Toix=InputBox(请输入成绩)sum=sum+Val(x)Nextjaver=sum/iPrint所修课程的总成绩为:;sumPrint所修课程的平均成绩为:;averEndSub第五章:page:1474(1):OptionBase1Privatea(15)AsIntegerPrivateSubCommand1_Click()RandomizePrintcreate15numbersinrandomandstockinarrayaFori=1To15a(i)=Int(Rnd*100)Text1.Text=Text1+Str(a(i))NextiEndSubPrivateSubCommand2_Click()Fori=15To1Step-1Text2.Text=Text2+Str(a(i))NextiEndSub4(5):OptionBase1Privatea(5,5)PrivateSubCommand1_Click()RandomizeFori=1To5Forj=1To5a(i,j)=Int(Rnd*10)Picture1.Printa(i,j);NextjPicture1.PrintNextiEndSubPrivateSubCommand2_Click()Fori=1To5Forj=1To5Sum=Sum+a(i,j)NextjNextiText1.Text=Sumofallelements=&CStr(Sum)EndSubPrivateSubCommand3_Click()Forj=1To5Sum=Sum+a(1,j)+a(5,j)NextjFori=2To4Sum=Sum+a(i,1)+a(i,5)NextiText2=Sumofelementsisedge=&CStr(Sum)EndSubPrivateSubCommand4_Click()Fori=1To5Sum=Sum+a(i,i)+a(i,6-i)Ifa(i,i)=a(i,6-i)ThenSum=Sum-a(i,i)NextiText3=Sumofelementsininter=&CStr(Sum)EndSubPrivateSubForm_Load()Text1=Text2=Text3=Picture1.ClsEndSub4(6)OptionBase1DimA()AsInteger,NAsInteger,AT()AsIntegerPrivateSubCommand1_Click()N=InputBox(pleaseenterrownumbers)ReDimA(N,N)RandomizeFori=1ToNForj=1ToNA(i,j)=Int(Rnd*10)Text1=Text1+CStr(A(i,j))+NextjText1=Text1+Chr(13)+Chr(10)NextiEndSubPrivateSubCommand2_Click()ReDimAT(N,N)Fori=1ToNForj=1ToNAT(i,j)=A(j,i)Text2=Text2+CStr(AT(i,j))+NextjText2=Text2+vbCrLfNextiEndSubPrivateSubForm_Load()Text1=Text2=EndSub4(7):OptionBase1Dimprime()AsIntegerPrivateSubCommand1_Click()Fori=1To100Forj=2Toi-1IfiModj=0ThenExitForEndIfNextjIfj=iThenk=k+1ReDimPreserveprime(k)prime(k)=iEndIfNextiFori=1TokPrintprime(i);n=n+1IfnMod10=0ThenPrintNextiEndSub4(8):OptionBase1PrivateA(4,4),MaxAsInteger,nAsIntegerPrivateSubCommand1_Click()RandomizePrintFori=1To4Forj=1To4A(i,j)=Int(Rnd*90+10)PrintA(i,j);NextjPrintNextiEndSubPrivateSubCommand2_Click()Dimpointer(4)AsIntegerFori=1To4Max=A(i,1)Forj=2To4IfA(i,j)MaxThenMax=A(i,j):pointer(i)=jEndIfNextjIfMax=A(i,1)ThenPrint第;CStr(i);行的第;1;列元素;CStr(Max);为最大值ElsePrint第;CStr(i);行的第;CStr(pointer(i));列元素;CStr(Max);为最大值EndIfNextiEndSub4(9):DimfirstAsSingle,secAsSinglePrivateSubCommand1_Click()EndEndSubPrivateSubForm_Load()Fori=1To7Option1(i).Top=Option1(0).Top+i*600Option1(i).Left=Option1(0).LeftNextiEndSubPrivateSubOption1_Click(IndexAsInteger)first=Text1:sec=Text2SelectCaseIndexCase0Picture1.ClsPicture1.Printfirst+secCase1Picture1.ClsPicture1.Printfirst-secCase2Picture1.ClsPicture1.Printfirst*secCase3Pictu

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功