1Vb.net习题解答习题二Xt2.9编写应用程序,读入一行字符,统计其中有字母、数字、空格和其它字符各有几个。PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickDimstr1,str2AsStringDimi,n,sum1,sum2,sum3,sum4AsIntegerstr1=InputBox(enterastring)n=Len(str1)Fori=1Tonstr2=Mid(str1,i,1)SelectCasestr2CaseaToz,AToZsum1=sum1+1Case0To9sum2=sum2+1Casesum3=sum3+1CaseElsesum4=sum4+1EndSelectNextMsgBox(字母&sum1&数字&sum2&Chr(10)&空格&sum3&其它&sum4)EndSubXt2.10编写应用程序,统计并逐行显示(每行5个数)在区间[10000,50000]上的回文数。PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickDimi,j,kAsIntegerDima,b,c,d,fAsIntegerDimstr1AsStringk=0Fori=10000To50000a=iMod10b=(iMod100)\10c=(iMod1000)\100d=(iMod10000)\1000f=i\10000Ifi=a*10000+b*1000+c*100+d*10+fThenstr1=str1+Str(i)+Space(3)2k=k+1IfkMod5=0Thenstr1=str1+Chr(10)+Chr(13)EndIfEndIfNextMsgBox(str1+Chr(10)+Str(k))EndSubXt2.15使用While语句,求两个正整数的最小公倍数。PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickDima,b,gbsAsIntegera=Val(InputBox(entera=?))b=Val(InputBox(enterb=?))Ifa=bThengbs=bElsegbs=aEndIfWhile(gbsModa0)Or(gbsModb0)gbs=gbs+1EndWhileMsgBox(正整数&a&和&b&的最小公倍数为:&gbs)EndSub习题三[Xt3.1----0.6326531]编写求nii1的函数过程,调用此函数求以下表达式的值。y=)7654321()654321()54321()4321()321(Functionsum(ByValnAsInteger)AsIntegerDimi,sAsIntegers=0Fori=1Tons=s+iNextsum=sEndFunction3PrivateSubButton1_Click()DimyAsDoubley=(sum(3)+sum(4)+sum(5))/(sum(6)+sum(7))MsgBox(y=&y)EndSub[xt3.2]编写一个求3个数中最大值Max和最小值Min的过程。然后用这个过程分别求3个数和5个数、7个数中的最大值和最小值。'求三个数的最大值的过程Functionmax(ByValaAsInteger,ByValbAsInteger,ByValcAsInteger)DimmAsIntegerIfabThenm=aElsem=bEndIfIfmcThenmax=mElsemax=cEndIfEndFunction'求三个数的最小值的过程Functionmin(ByValaAsInteger,ByValbAsInteger,ByValcAsInteger)DimmAsIntegerIfabThenm=aElsem=bEndIfIfmcThenmin=mElsemin=cEndIfEndFunction4PrivateSubButton1_Click()Dimmax1,min1AsDoublemax1=max(34,124,68)'求三个数的最大值min1=min(34,124,68)'求三个数的最小值MsgBox(三个数34、124、68的最大值是:&max1)MsgBox(三个数34、124、68的最小值是:&min1)'求五个数34、124、68、73、352的最大值max1=max(34,124,68)max1=max(max1,73,352)MsgBox(五个数34、124、68、73、352最大值是:&max1)'求七个数34、124、68、73、352、493、25的最大值max1=max(34,124,68)max1=max(max1,73,352)max1=max(max1,493,25)MsgBox(七个数34、124、68、73、352、493、25的最大值:&max1)EndSub[xt3.3]编写程序,求S=A!+B!+C!。阶乘的计算分别用Sub过程和Function过程两种方法来实现。'function过程Functionjcf(ByValnAsInteger)DimiAsInteger,tAsDoublet=1Fori=1Tont=t*iNextjcf=tEndFunction'sub过程Subjcs(ByValnAsInteger,ByRefjcAsSingle)DimiAsIntegerjc=1Fori=1Tonjc=jc*iNextEndSubPrivateSubButton1_Click()Dima,b,cAsInteger5DimsAsDoublea=3:b=4:c=5s=jcf(a)+jcf(b)+jcf(c)MsgBox(调用function过程s=&s)EndSubPrivateSubButton2_Click()Dima,b,cAsIntegerDimsAsDoublea=3:b=4:c=5Dima1,b1,c1AsDoubleCalljcs(a,a1)Calljcs(b,b1)Calljcs(c,c1)s=a1+b1+c1MsgBox(调用sub过程s=&s)EndSub[xt3.4]调用3.3题的阶乘函数过程,求组合数Cnm=)!(!!nmnm'function过程Functionjcf(ByValnAsInteger)DimiAsInteger,tAsDoublet=1Fori=1Tont=t*iNextjcf=tEndFunctionPrivateSubButton3_Click()Dimm,nAsIntegerDimcmnAsDoublem=Val(InputBox(输入m的值))n=Val(InputBox(输入n的值))IfnmThenMsgBox(输入数据错误)EndIfcmn=jcf(m)/(jcf(n)*jcf(m-n))MsgBox(组合数是:&cmn)EndSub6[xt3.5]斐波纳契(Fibonacci)数列的第一项是0,第二项是1,以后各项都是前两项的和。试用递归算法和非递归法各编写一个程序,求斐波纳契数列第N项的值.functionfib1(ByValnAsInteger)'非递归算法----(递推)Dimf1,f2,f3AsIntegerDimiAsIntegerf1=0:f2=1Fori=3TonF3=f1+f2'第i项的值f1=f2f2=f3NextFib1=f3EndSubFunctionfib2(ByValnAsInteger)'递归算法Ifn=1Thenfib2=0Ifn=2Thenfib2=1Ifn=3Thenfib2=fib2(n-1)+fib2(n-2)EndFunctionPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickDimnAsIntegern=Val(InputBox(n=?))MsgBox(用递推算法,数列第&n&项的值为&fib1(n))MsgBox(用递归算法,数列第&n&项的值为&fib2(n))EndSub[xt3.6]编写八进制数与十进制数相互转换的过程:(1)过程ReadOctal,读入八进制数,转换为等值的十进制数。(2)过程WriteOctal,将十进制正整数以等值的八进制形式输出。'oct_num转int_num(8转10)Functionreadoctal(ByValoct_numAsString)AsSingleDimn,i,t,sAsIntegers=0n=Len(oct_num)Fori=1Ton7t=Val(Mid(oct_num,i,1))*8^(n-i)'各位按权位求值s=s+tNextreadoctal=sEndFunction'int_num转oct_num(10转8)Functionwriteoctal(ByValint_numAsString)AsSingleDimn,i,tAsInteger,sAsStringDimoctstr(6)AsStringFori=6To1Step-1octstr(i)=int_num-Int(int_num/8)*8'连除基数int_num=Int(int_num/8)NextFori=1To6s=s+(octstr(i))'逆序取余Nextwriteoctal=Val(s)EndFunctionPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickDima,bAsIntegera=Val(TextBox1.Text)b=writeoctal(a)'调用函数过程TextBox2.Text=Str(b)EndSubPrivateSubButton2_Click_1(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.ClickDima,bAsIntegera=TextBox3.Textb=readoctal(a)'调用函数过程TextBox4.Text=Str(b)EndSub[xt3.7]编写一个过程,用来计算并输出S=1+21+31+…+1001的值。Functionsumd(ByValnAsInteger)AsSingle8Dimi%,s!s=1:i=1Doi=i+1s=s+1/iLoopUntili=nsumd=sEndFunctionFunctionsumf(ByValnAsInteger)AsSingleDimi%,s!s=0Fori=1Tons=s+1/iNextsumf=sEndFunctionPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickMsgBox(sumd=&sumd(100))MsgBox(sumf=&sumf(100))EndSub----5.187378[xt3.8]编写过程,用下面的公式计算π的近似值:4=1-31+51-71+…(-1)n-1121n在事件过程中调用该过程,并输出当n=100、500、1000、5000时π的近似值。Functionpi(ByValnAsInteger)AsSingleDi