第6章过程1.输入一个四位正整数,将其逆序输出。PrivateSubCommand1_Click()Dimn1AsInteger,n2AsIntegerRemIsNumeric函数用于判断是否为数字字符组成,若是数字字符为True,否则为False。IfNotIsNumeric(Text1.Text)ThenMsgBox文本框中包含非数字字符,请重新输入。Text1.Text=Text1.SetFocusElseIfLen(Text1.Text)4ThenMsgBox文本框中输入4位整数,请重新输入。Text1.Text=Text1.SetFocusElsen1=Val(Text1.Text)CallNX(n1,n2)Text2.Text=n2EndIfEndSubPrivateSubCommand2_Click()EndEndSubPrivateSubNX(ByValn1AsInteger,n2AsInteger)DimiAsIntegerFori=1To4r=n1Mod10n2=n2*10+rn1=n1\10NextiEndSub2.验证任意一个大于5的奇数可表示为3个素数之和。书P182[实例6-11]3.编写一个将N进制数转换为十进制数的通用程序。PrivateSubcmdexit_Click()‘退出EndEndSubPrivateSubcmdtrs_Click()‘转换txtresult.Text=CStr(mytrs())EndSub'讲N进制数转换成十进制数的通用程序'对输入的数据的正确性暂不作检查'这里假设输入的为正整数PrivateFunctionmytrs()AsIntegerDimiAsInteger,iLenAsInteger'先对输入的数据进行合理性检查iLen=Len(txtinput.Text)Fori=1ToiLenIfMid(txtinput.Text,i,1)0OrMid(txtinput.Text,i,1)9ThenMsgBox输入了非法数据,请重新输入!,vbExclamation,提示txtinput.Text=txtinput.SetFocusExitFunctionEndIfmytrs=mytrs*Val(txtcase.Text)+Asc(Mid(txtinput.Text,i,1))-Asc(0)NextiEndFunction4.已知竞赛小组12名学生竞赛成绩,请排列出他们的名次(成绩由键盘输入,成绩相同的名次相同)。OptionBase1PrivateSubCommand1_Click()‘输入并排名Dima(12,2)AsIntegerDimiAsInteger,numAsIntegerFori=1To12a(i,1)=InputBox(请输入&i&号学生的成绩,成绩排序)a(i,2)=iPicture1.Printi&号:;a(i,1)NextiCallSort(a)Fori=1To12Ifi=1Thennum=1ElseIfa(i,1)a(i-1,1)Thennum=i'若要名次连续的话,将该语句替换为num=num+1EndIfPicture2.Print第&num&名:;a(i,2)&号,成绩为;a(i,1)NextiEndSubPrivateSubSort(b()AsInteger)‘对二维数组元素的第一列数据排序DimiAsInteger,jAsIntegerDimtempAsIntegerFori=1ToUBound(b,1)-1Forj=i+1ToUBound(b,1)Ifb(i,1)b(j,1)Then‘交换成绩temp=b(i,1)b(i,1)=b(j,1)b(j,1)=temp‘交换对应的学号temp=b(i,2)b(i,2)=b(j,2)b(j,2)=tempEndIfNextjNextiEndSubPrivateSubCommand2_Click()‘退出EndEndSub5.编写用于判断输入的正整数是否为降序数的函数,设正整数n=d1d2d3。。。dk,如果满足di=di+1,则n就是一个降序数。OptionBase1PrivateSubCommand1_Click()‘判断Dima()AsInteger‘用于存放整数的各位上的数字DimiAsInteger,nAsInteger,numAsIntegern=Val(Text1.Text)‘从个位开始存放,也可以从最高位存放Fori=1ToLen(Trim(Text1.Text))num=num+1ReDimPreservea(num)a(num)=nMod10n=Int(n/10)NextiIfJudge(a)ThenText2.Text=Text1.Text&是降序数ElseText2.Text=Text1.Text&不是降序数EndIfEndSub‘从低位开始存放到数组,用;从高位开始存放到数组,用。PrivateFunctionJudge(b()AsInteger)AsBooleanDimiAsIntegerFori=2ToUBound(b)Ifb(i-1)b(i)ThenExitForNextiIfiUBound(b)ThenJudge=TrueEndFunction6.求出2000以内满足以下条件的正整数;该数本身不素数,但它的所有因子之和是素数。将素数的判断、因子求和的求解写成函数形式。OptionBase1PrivateSubForm_Click()Dima()AsInteger,iAsIntegerFori=1To2000IfNotPrime(i)ThenCallYZ(a,i)IfPrime(SUM(a))ThenPrinti;EndIfNextiEndSub'数组元素求和PrivateFunctionSUM(b()AsInteger)AsIntegerDimiAsIntegerFori=1ToUBound(b)SUM=SUM+b(i)NextiEndFunction'判断n是否为素数PrivateFunctionPrime(ByValnAsInteger)AsBooleanDimiAsIntegerFori=2Ton-1IfnModi=0ThenExitForNextiIfi=nThenPrime=TrueEndFunction'求n的所有因子,并把这些因子放在数组a里PrivateSubYZ(a()AsInteger,ByValnAsInteger)DimiAsIntegerr=0Fori=1TonIfnModi=0Thenr=r+1ReDimPreservea(r)a(r)=iEndIfNextiEndSub7.编写一个求裴波拉契数列的递归过程,并将其前六项显示在文本框中。PrivateSubCommand1_Click()‘显示DimnAsInteger,iAsIntegerDimtAsString,s()AsIntegern=InputBox(请输入裴波拉契数列的项n:)ReDims(n)AsIntegerFori=1Tons(i)=fab(i)t=t+Str(s(i))Text1.Text=t+NextiEndSubPrivateFunctionfab(nAsInteger)AsLongIfn=1Thenfab=1ElseIfn=2Thenfab=1Elsefab=fab(n-2)+fab(n-1)EndIfEndFunction8.编写程序求下列数列的和,计算精确到第n项的值小于等于10-5为止。ny26421642142121(其中n=1,2,3,…)OptionExplicitPrivateSubCmdCalc_Click()‘计算DimnAsSingle,epsAsSingleDimsAsSingle,aAsSingle,tempAsStringtemp=InputBox(请输入允许误差:,求数列和,1e-5)temp=Trim(temp)s=0:eps=Right(temp,Len(temp)):n=1Doa=afun(n)Ifa=epsThenExitDos=s+an=n+1LoopText1.Text=sEndSubPrivateSubCmdClear_Click()‘清除Text1.Text=EndSubPrivateSubCmdExit_Click()‘退出EndEndSubPrivateFunctionafun(ByValnAsInteger)AsSingleDimiAsInteger,pAsSinglep=1Fori=1Tonp=p*1/(2*i)Nextiafun=pEndFunction9.编写程序求下列级数的和,计算精确到第n项的值小于等于10-5为止。nnfffxxxxxSn15325332210x1其中:1n=1fn=1n=2fn-1+fn-2n2OptionExplicitPrivateSubCmdCalc_Click()‘计算DimxAsSingle,nAsSingle,epsAsSingleDimsAsSingle,aAsSingle,tempAsStringtemp=InputBox(请输入一小于等于1的正数与允许误差:,求函数值,0,1e-5)temp=Trim(temp)n=InStr(temp,,)x=Left(temp,n-1):eps=Right(temp,Len(temp)-n)s=x:n=3Doa=afun(x,n)Ifa=epsThenExitDos=s+an=n+1LoopLabel2.Caption=s(&CStr(x)&)=Text1.Text=sEndSubPrivateSubCmdClear_Click()‘清除Text1.Text=EndSubPrivateSubCmdExit_Click()‘退出EndEndSubPrivateFunctionafun(ByValxAsSingle,ByValnAsInteger)AsSingleafun=x^fab(n)/((fab(n-1))*fab(n))EndFunctionPrivateFunctionfab(hAsInteger)AsLongIfh=1Thenfab=1ElseIfh=2Thenfab=1Elsefab=fab(h-1)+fab(h-2)EndIfEndFunction10.从键盘读取数组a和b的元素(各5个),a,b都是严格递增的(即元素从小到大排列,且无重复元素),将a,b合并成数组c,使c也严格递增。OptionBase1Dima(5)AsInteger,b(5)AsIntegerDimc()AsIntegerRem输入数组aPrivateSubCommand1_Click()DimiAsIntegerFori=1To5a(i)=InputBox(请输入数组a第&i&元素)Text1.Text=Text1.Text&Str(a(i))NextiEndSubRem输入数组bPrivateSubCommand2_Click()DimiAsIntegerFori=1To5b(i)=InputBox(请输入数组b第&i&元素)Text2.Text=Text2.Text&Str(b(i))NextiEndSubRem调用合并数组Sub过程CombPrivateSubCommand3_Click()CallComb(a,b,c)EndSubRem合并数组Sub过程Comb定义PrivateSubComb(a()AsInteger,b()AsInteger,c()AsInteger)DimiAsInteger,jAsInteger,rAsIntegeri=1:j=1Rem比较合并DoWhilei=UBound(a)Andj=UBound(b)r=r+1ReDimPreservec(r)Ifa(i)=b(j)Thenc(r)=a(