首页 二级vb编程

二级vb编程

举报
开通vip

二级vb编程VB常考程序 1.求两个数的最小公倍数 Private function zxgb(x as integer,y as integer)as integer For i=x to x*y If i mod x=0 and i mod y=0 then exit for Next i Zxgb=i End function 2.求两个数的最大公约数 Private function zdgy(x as integer,y as integer) as integer For i=x to 1 st...

二级vb编程
VB常考程序 1.求两个数的最小公倍数 Private function zxgb(x as integer,y as integer)as integer For i=x to x*y If i mod x=0 and i mod y=0 then exit for Next i Zxgb=i End function 2.求两个数的最大公约数 Private function zdgy(x as integer,y as integer) as integer For i=x to 1 step -1 If x mod i=0 and y mod i=0 then exit for Next i Zdgy=i 3.生成20个互相不同的两位整数[10,99] 方法一: Dim a(20) as integer a(1)=int(rnd*90+10) s=s & a(1) k=1 Do until k=20 m=int(rnd*90+10) If instr(s,m)=0 then k=k+1 a(k)=m s=s & m End if Loop 说明:instr([N1,]C1,C2,[N])函数的用法 在C1中从N1开始找C2,省略N1从头开始找,找不到为0,找到返回第一个字符的位置。 N-0 区分大小写(缺省); N-1 不区分大小写 方法二: Dim a(20) as integer ,k as integer, i as integer a(1)=int(rnd*90+10) s=s & a(1) idx=1 do while idx<20 k=int(rnd*90+10) for j=1 to idx if k=a(j) then exit for next j if j>idx then idx=idx+1 a(idx)=k s=s & k loop text1=text & str(a(idx)) if idx mod 10=0 then text1=text & vbcrlf 说明:VBCRLF是一个字符串,是一种VB编程语言,换行的意思。 4.判断一个数在不在数组中 Function com(a() as integer m as integer) as Boolean Dim ub as integer Ub=ubound(a) For i=1 to ub If a(i)=m then exit for Next i If i=ub+1 then Com =false Else Com=true End if End function 5.判断一个数是不是回文数 说明:正读倒读一样, 这个数字就是回文数。如:98789, 这个数字正读是98789,倒读也是98789,所以98789是回文数。 方法一: Function hw(x as integer) as Boolean M=cstr(x) For i=1 to len(m) S=mid(m,I,1) Next i If s=m then hw=true End function 说明:cstr函数的作用 将括号中的 内容 财务内部控制制度的内容财务内部控制制度的内容人员招聘与配置的内容项目成本控制的内容消防安全演练内容 转换为字符串,括号中的内容可为值、变量或 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 达式。 方法二: N=len(m) For i=1 to len(m) If mid(m,i,1)=mid(m,n-i+1,1) Then Hw=true 注意:cstr函数与str函数的区别在于:str函数处理正数时保留符号位,而cstr不保留符号位。 6.因子问题 (1)求一个数所有因子并存放在数组中 Function yz(a() as integer,x as integer) For i=1 to x If x mod i=0 then k=k+1 Redim preserve a(k) a(k)=i End if Next i End function 注意:使用Redim相当于数组被重新初始化,原数据将全部丢失;[preserve]表示再次使用Redim语句改变数组大小时保留数组中原数据。 (2) 求一个数所有的质因子并存放在数组中 i=2 Do If n mod i=0 then k=k+1 Redim preserve a(k) a(k)=i n=n\i Else i=i+1 End if Loop until n<=1 【拓展】求一个数的因子和 Function yz(a() as integer,x as integer) For i=1 to x If x mod i=0 then yz=yz+1 End if Next i 注意:本题易错点yz=yz+1 7.把一个n进制数转化为十进制数 方法一: Function nto10(x as string,n as string) as integer For j=len(x) to 1 step -1 S=s+val(mid(x,I,1)*n^k K=k+1 Next i Nto 10=s End function 方法二: Function nto10(x$ ,n $ ) % For j=1 to len(x) S=s+val(mid(x,I,1)*n^(len(x)-i) K=k+1 Next i Nto 10=s End function 8.把一个十进制数转化为n进制数 说明:此例是把十进制数转化为十六进制数 Function tenton(x as integer,n as integer) as string Do m=x mod n s=m & s x=x\n Loop until x=0 Tenton=s End function Dim n as integer ,p as string,k as integer P=”ABCDEF” n=asc(s) Do K=n mod 16 If k<10 then Stoh=stoh & cstr(k) Else Stoh=mid(p,k-9) & stoh End if N=n\16 Loop until n<=0 9.把二进制数转化为八进制数 Function c8to10(x as string) as string L=len(x) If L mod 3<>0 then X=string(3-l mod 3,”0”) & x End if L=len(x) For i=1 to l step 3 T=mid(x,l,3) S=0 For j=1 to 3 S=s+mid(t,j,1)*2^(3-j) Next j s1=sl & s Next i C8to10=sl End function 10.把一个数组所有元素左移一位,a(1)移动到最后 Sub zy(a() as integer) k=a(1) For i=1 to ubound(a)-1 a(i)=a(i+1) Next i a(ubound(a))=k End sub For i=1 to n‘左移n位 Call zy(a) Next i Sub zy(a() as integer) K=a(1) For i=1 to ubound(a)-1 A(i)=a(i+1) Next i A(ubound(a))=k End sub 11.数组元素的删除与插入问题 (1)把一个数组中的第n个元素删除,方法是数组左移法 Sub sc(a() as integer,n as integer) T=ubound(a) For i=n+1 to t 或 for i=n to t-1 A(j-1)=a(j) a(j)=a(j+1) Next i Redim preserve a(t-1) End sub (2)在数组第n位上插入数字m,方法是数组右移法 Sub cr(a() %,n %,m %) T=ubound(a) Redim preserve a(t+1) For i=n+1 to t+1 A(i)=a(i-1) Next i A(n)=m End sub 12.判断一个数是不是素数 方法一: Function ss(x as integer) as Boolean For i=2 to x-1(也可以写 2 to sqr(x) 或2 to x/2) If x mod i=0 then exit for(function) Next i If x=1 then(对应上面x>sqr(x)或i>x/2) Ss=true End if End function 注意:Function ss(x %) as Boolean For i=2 to x-1(也可以写 2 to sqr(x) 或2 to x/2) If x mod i=0 then exit function Next i Ss=true End function 方法二: Function ss(x as integer) as Boolean K=2 Do until k=x If x mod k=0 then Ss=false Exit function Else K=k+1 End if Loop Ss=true End function 13.求和问题 (1)把小于x的所有数排列组合求和 For i=1 to x-1 For j=i+1 to x S=i+j Print s Next j Next i (2)把20个元素的数组中元素排列组合求和 Dim a(20) For i=1 to 19 For j=i+1 to 20 S=a(i)+a(j) Print s Next j Next i 14.求一个数字中有几种数字 方法一: Function ver(byval n as integer) as integer Dim a(0 to 9) as integer’ Do while n<>0 Idex=n mod 10 A(idex)=1 N=n\10 Loop For i=0 to 9 Js=js+a(i) Next i Ver=js End function 方法二: Dim left %,k %,I % Right=ubound(a) Left=2 Do while left<=right K=left-1 For i=k to 1 step -1 If a(left)=a(right) then exit for Next i If i=0 then Left=left+1 Else a(left)=a(right) Right=right-1 End if Loop P=right Text=”有”p”个不同的数 15. 把一个数字中每一种数字出现的次数存放在数组中 Function ver(byval n as integer) as integer Dedim a(9) Do while n<>0 idex=n mod 10 A(idex)=A(idex)+1 N=n\10 Loop End function 16.把ABC26个英文字母,转成123[就是说A是1,b是2] X=一个英文 Y=ucase(x) T=asc(y)-65+1 17. 把一个数字字母中的混合字符串中的每一个连续的数字串取出,并存放在数组中,并求最长的数字串 Dim a() as integer X=一个数字字母中的混合字符串 For i= 1to len(x) T=mid(x,i,1) If t >="0" and t<"9" then P=p&t Else K=k+1 Redim preserve a(k) A(k)=p If len(p) > len(maxs) then maxs=p P="" End if Next i K=k+1 Redim preserve a(k) A(k)=p If len(p) > len(maxs) then maxs=p P=”” End if Next i 18.取ip地址中的4个数 方法一: For I =1 to n D=mid(st,I,1) If d=”.”then K=k+1 A(k)=val(s) S=”” Else S=s & d End if Next i 方法二: Do N=instr(st,”.”) K=k+1 Redim preserve a(k) If n<>0 then A(k)=val(left(st,n-1)) Else A(k)=val(st) End if St=mid(st,n+1,len(st)-n) Loop until n<=0 19. 一行一行查看列表框的每一项内容 For i=0 to list1.listcount -1 T=list1.list(i) Print T Next i 20. 求一个数组中最大的数字 Dim a(10) Max=a(1) For i=2 to 10 If a(i)>max then max=a(i) Next i Print Max 21.求一个3行3列的数组的每一行的最大数 Dim a(3,3) as integer For i=1 to 3 S=a(I,j) For j=1 to 3 If a(i,j)>s then s=a(i,j) Next j Print Next i 22.求一个3行3列的数组中,两条对角线上元素的和 Dim a(3,3) as integer For i=1 to 3 For j=1 to 3 If i=j or i+j=4 then S=s+a(I,j) End if Next j Next i 23.统计一段英文中每一个字母出现的次数,不区分大小写,并把次数存放在一个数组中 方法一: Dim a(26) as integer X=一段英文 X=ulase(x) For i=1 to len(x) T=mid(x,I,1) Idx=asc(t)-65+1 A(idx)=a(idx)+1 Next i For i=1 to 26 ?chr(65+i-1);”出现了:”;a(i);””次” Next i 方法二: For i=1 to l Ch=mid(st,I,1) If ch>=”A” and ch<=”Z” then Idx=asc(ch)-asc(“A”) A(idx)=a(idx)+1 Else if ch>=”a” and ch<=”z” then Idx=asc(ch)-asc(“a”) A(idx)=a(idx)+1 End if Next i 24. 把一组数组从小到大排序 方法一:选择排序法 Dim a(10) For i = 1 to 9 For j=i+1 to 10 If a(j) < a(i) K=a(i) A(i)=a(j) A(j)=k Next j Next i 方法二:冒泡法排序 For i=1 to ubound(a)-1 For j=1 to ubound(a)-i If a(j)>a(j+1) then T=a(j) A(j)=a(j+1) A(j+1)=t End if Next j Next i 方法三:按插入排序法由大到小排序 Dim x as integer,I as integer,j as integer For i=1 to ubound(a) X=a(i) J=t-1 Do while j>=0 and x>a(j) A(j)=a(j+1) J=j-1 If j<0 then exit do Loop If i>j+1 then A(j+1)=x End if Next i 25. 判断一个数字中有没有重复的数 方法一 Function cf(x as string) as boolean For i=1 to len(x)-1 For j=2(或i+1) to len(x) If mid(x,i,1) =mid(x,j,1) then Cf=false Exit function End if Next j Next i Cf=true End function 方法二 Function cf(x as string) as boolean Dim a(0 to 9) as integer 求数字出现次数: 方法一:For i=1 to len(x) 方法二:do while n<>0 T=mid(x,i,1) t=n mod 10 A(t)=a(t) + 1 a(t)=a(t)+1 Next I n=n\10 For i =0 to 9 loop If a(i) >=2 then Cf=false Exit function End if Next i Cf=true End function 26.生成两两互质的数,并由大到小排列 方法一: List.list(0)=int(rnd*(9999-1000))+1000 Do P= int(rnd*(9999-1000))+1000 For i=0 to list1.listcount-1 For j=2 to p If p mod j=0 and list1.list(i) mod j=0 then Exit for End if Next j If j<=p then exit for Next i If i>list1.listcount-1 then Idx=0 Do while plist1.listcount-1 then exit do Loop List.additem p,idx End if Loop until list1.listcount=10 方法二: List.list(0)=int(rnd*(9999-1000))+1000 Do P= int(rnd*(9999-1000))+1000 For i=0 to list1.listcount-1 Do R=p mod list.list(i) P=list.list(i) List.list(i)=r Loop until r=0 If p<>1 then exit do If i>list1.listcount-1 then Idx=0 Do while plist1.listcount-1 then exit do Loop List.additem p,idx End if Loop until list1.listcount=10 27.二分法查找一个数 Do while left<=right Mid=(right+left)/2 If search(mid)=find then Flg=true Exit do Elseif find>search(mid) then Left=mid+1 Else Right=mid-1 End if Loop 28.判断降序数 方法一: For i=1 to 3 Num(i)=n mod 10 N=n\10 Next i If num(3)>num(2) and num(2)>num(1) then Jx=true 方法二: For i=1 to ubound(a)-1 If a(i)>=a(i+1) then Exit function Next i Jx=true 方法三: For i=1 to ubound(a)-1 For j=i+1 to ubound(a) If a(i)=a(j) then exit function Nextj Nexti Jx=true 29.判断一个数是否为平方数 If sqr(b)=int(sqr(b)) then List1.additem str(b) 30.两位数的调换 A=n mod 10 B=n\10 M=a*10+b 31.随机产生7.0到10.0的数 Score(i)=(int(rnd*31)+70)/10 保留一位小数 (int(av*10))/10 或 format(av,”###.#”) 32.补足“0” For i=1 to 7-len(s) S=”0” & s Next i 33.n位可表示数的范围 For i=10^(n-1) to 10^n-1 34.取数 (1)取一个数字的每一位 Do I=i+1 Redim preserve p(i) P(i)=n mod 10 N=n\10 Loop until n<=0 (2)三位数取每位上的数值 A=n\100 B=(n mod 100)\10 C=n mod 10 35.阶乘 Sum=1:n=text1.text For i=1 to n Sum=sum * i Next i Text2.text=n & “!:” & sum 36.输出时n个数(如4)一排 For i=1 to ubound(num) If I mod 4<>0 then Text1=text1 & str(num(i)) Else Text1=text1 & str(num(i)) & vbcrlf End if Next i PAGE 7
本文档为【二级vb编程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_226449
暂无简介~
格式:doc
大小:106KB
软件:Word
页数:9
分类:工学
上传时间:2012-03-28
浏览量:44