VB常见20道编程代码.docx

上传人:b****6 文档编号:6176195 上传时间:2023-01-04 格式:DOCX 页数:16 大小:16.40KB
下载 相关 举报
VB常见20道编程代码.docx_第1页
第1页 / 共16页
VB常见20道编程代码.docx_第2页
第2页 / 共16页
VB常见20道编程代码.docx_第3页
第3页 / 共16页
VB常见20道编程代码.docx_第4页
第4页 / 共16页
VB常见20道编程代码.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

VB常见20道编程代码.docx

《VB常见20道编程代码.docx》由会员分享,可在线阅读,更多相关《VB常见20道编程代码.docx(16页珍藏版)》请在冰豆网上搜索。

VB常见20道编程代码.docx

VB常见20道编程代码

哈哈哈

 

20道编程代码

1.1000以内的所有完数

PrivateSubCommand1_Click()

DimnAsInteger

DimiAsInteger

DimsAsLong

Forn=1To1000

s=1

Fori=2Ton-1

IfnModi=0Thens=s+i

Nexti

Ifs=nThen

Text1=Text1&n&""

IfkMod5=0ThenText1=Text1&vbCrLf

EndIf

Nextn

EndSub

PrivateSubCommand2_Click()

End

EndSub

2.百元买百鸡

PrivateSubCommand1_Click()

DimiAsInteger

DimjAsInteger

Fori=0To20

Forj=0To33

If5*i+3*j+(100-i-j)*1/3=100Then

Text1=Text1&"买"&CStr(i)&"只公鸡"&CStr(j)&"只母鸡"&CStr(100-i-j)&"只小鸡"

Text1=Text1&Chr(13)&Chr(10)

EndIf

Nextj

Nexti

EndSub

PrivateSubCommand2_Click()

End

EndSub

3.不靠边元素和

PrivateSubCommand1_Click()

Cls

Randomize

Dima(5,5)AsInteger,s%

Fori=0To5

Forj=0To5

a(i,j)=Int(Rnd*9)

Printa(i,j)&",";

Sum=Sum+a(i,j)

Nextj

Print

Nexti

k=Sum

Sum=0

Fori=0To5

Sum=Sum+a(i,0)+a(i,5)+a(0,i)+a(5,i)

Nexti

Sum=Sum-a(0,0)-a(5,0)-a(5,5)-a(0,5)

Text1=k-Sum

EndSub

PrivateSubCommand2_Click()

End

EndSub

4.递归函数求斐波拉契数列

OptionExplicit

PrivateSubCommand1_Click()

Dima1,a2,bAsInteger

Dimi,nAsInteger

n=InputBox("显示数列的前N项")

a1=1

Fori=1Ton

b=a1+a2

Text1.SelText=b&""

a1=a2

a2=b

Nexti

EndSub

PrivateSubCommand2_Click()

End

EndSub

5.各位数之和

PrivateSubCommand1_Click()

DimiAsString

Dimj%

Dims%

i=Text1

Forj=1ToLen(i)

st=Mid(i,j,1)

s=s+Val(st)

Nextj

Text2=s

EndSub

PrivateSubCommand2_Click()

End

EndSub

6.回文数

PrivateSubCommand1_Click()

Dimx$,y$,c$,i%

x=Text1

Fori=1ToLen(x)

c=Mid(x,i,1)

y=c+y

Nexti

Ify=xThen

Text2="x是回文数"

Else

Text2="x不是回文数"

EndIf

EndSub

PrivateSubCommand2_Click()

Text1=""

Text2=""

EndSub

PrivateSubCommand3_Click()

End

EndSub

7.级数求和PrivateSubCommand1_Click()

DimiAsInteger,tAsLong,sAsSingle

i=0:

s=o:

t=1

Do

s=s+1/t

i=i+1

t=t*i

LoopUntil1/t<0.000001

Text1=s

EndSub

PrivateSubCommand2_Click()

End

EndSub

8.矩阵的转置

OptionExplicit

OptionBase1

DimiAsInteger

DimjAsInteger

Dima()AsInteger

DimnAsInteger

PrivateSubCommand1_Click()

Text3.SetFocus

n=Val(Text3)

ReDima(n,n)'动态数组

Fori=1Ton

Forj=1Ton

a(i,j)=Int(10*Rnd+0)'随机数

Text1=Text1&a(i,j)&""'一行

Nextj

Text1=Text1&Chr(13)&Chr(10)'多行

Nexti

EndSub

PrivateSubCommand2_Click()

Dimb()AsInteger

n=Val(Text3)

ReDimb(n,n)

Fori=1Ton

Forj=1Ton

b(i,j)=a(j,i)'转置

Text2=Text2&b(i,j)&""'一行

Nextj

Text2=Text2&Chr(13)&Chr(10)'多行

Nexti

EndSub

PrivateSubCommand3_Click()

Text1=""

Text2=""

Text3=""'清除

Text3.SetFocus

EndSub

PrivateSubCommand4_Click()

End'结束

EndSub

PrivateSubForm_Load()

Text1=""

Text2=""

Text3=""'清除

EndSub

9.靠边元素和

PrivateSubCommand1_Click()

Cls

Randomize

Dima(5,5)AsInteger

Fori=0To5

Forj=0To5

a(i,j)=Int(Rnd*9)

Printa(i,j)&",";

Sum=Sum+a(i,j)

Nextj

Print

Nexti

Sum=0

Fori=0To5

Sum=Sum+a(i,0)+a(i,5)+a(0,i)+a(5,i)

Next

Sum=Sum-a(0,0)-a(5,0)-a(5,5)-a(0,5)

Text1=Sum

EndSub

PrivateSubCommand2_Click()

End

EndSub

10.冒泡排序

OptionExplicit

OptionBase1

Dima(10)AsInteger

DimiAsInteger

PrivateSubCommand1_Click()

Fori=1To10

a(i)=Int(90*Rnd+10)

Text1=Text1&Str(a(i))&""

IfiMod5=0ThenText1=Text1&Chr(13)&Chr(10)

Nexti

EndSub

PrivateSubCommand2_Click()

DimjAsInteger

DimtAsInteger

Fori=1To9

Forj=1To10-i

Ifa(j)

t=a(j)

a(j)=a(j+1)

a(j+1)=t

EndIf

Nextj

Nexti

Fori=1To10

Text2=Text2&Str(a(i))&""

IfiMod5=0ThenText2=Text2&Chr(13)&Chr(10)

Nexti

EndSub

PrivateSubCommand3_Click()

Text1=""

Text2=""

EndSub

PrivateSubCommand4_Click()

End

EndSub

11.逆序数

PrivateSubCommand1_Click()

DimaAsString

Dimi%

DimsAsString

a=Text1

Fori=1ToLen(a)

st=Mid(a,i,1)

s=st+s

Nexti

Text2=s

EndSub

PrivateSubCommand2_Click()

End

EndSub

12.判断素数

PrivateSubCommand1_Click()

DimnAsInteger

DimiAsInteger

n=Val(Text1)

Ifn=1Then

MsgBox(CStr(n)&"是素数")

Else

Fori=2Ton-1

IfnModi=0ThenExitFor

Nexti

Ifi=nThen

MsgBox(CStr(n)&"是素数")

Else

MsgBox(CStr(n)&"不是素数")

EndIf

EndIf

EndSub

PrivateSubCommand2_Click()

Text1=""

EndSub

PrivateSubCommand3_Click()

End

EndSub

13.求阶乘(递归)

PrivateFunctionf(ByValnAsLong)AsLong

DimiAsInteger

f=1

Ifn=0Orn=1Then

f=1

Else

Fori=1Ton

f=f*i

Nexti

EndIf

EndFunction

PrivateSubCommand1_Click()

DimnAsLong

n=Val(Text1)

Text2=f(n)

EndSub

PrivateSubCommand2_Click()

Text1=""

Text2=""

EndSub

PrivateSubCommand3_Click()

End

EndSub

14.求质因子

PrivateSubCommand1_Click()

Dimn%,i%,m%

n=Val(Text1)

Fori=1Ton

Ifi=1Then

Picture1.Print

Else

IfnModi=0Then

Form=2Toi-1

IfiModm=0ThenExitFor

Nextm

Ifm=iThen

Picture1.Printi,

EndIf

Picture1.Print

EndIf

EndIf

Nexti

EndSub

PrivateSubCommand2_Click()

Text1=""

Picture1.Cls

EndSub

PrivateSubCommand3_Click()

End

EndSub

15.水仙花数

PrivateSubCommand1_Click()

Dima,b,cAsInteger

Fora=0To9

Forb=0To9

Forc=1To9

Ifa^3+b^3+c^3=a+10*b+100*cThen

MsgBox100*c+10*b+a

EndIf

Nextc

Nextb

Nexta

EndSub

PrivateSubCommand2_Click()

End

EndSub

16.顺序查找

OptionExplicit

OptionBase1

Dima(10)AsInteger

DimiAsInteger

PrivateSubCommand1_Click()

Fori=1To10

a(i)=Int(90*Rnd+10)

Text1=Text1&Str(a(i))

IfiMod5=0ThenText1=Text1&Chr(13)&Chr(10)

Nexti

EndSub

PrivateSubCommand2_Click()

DimxAsInteger

x=Val(Text2)

Fori=1To10

Ifa(i)=xThenExitFor

Nexti

Ifi<=10Then

Text3=Str(x)+"在第"+Str(i)+"个位置"

Else

Text3="没有找到"

EndIf

EndSub

PrivateSubCommand3_Click()

Text1=""

Text2=""

Text3=""

EndSub

PrivateSubCommand4_Click()

End

EndSub

17.随机10个整数最值,均值,和

DimiAsInteger

Dima(10)AsInteger

PrivateSubCommand1_Click()

Randomize

Fori=1To10

a(i)=Int(90*Rnd+10)

Nexti

Fori=1To10

Text1=Text1&a(i)&""

Nexti

EndSub

PrivateSubCommand2_Click()

DimkAsInteger

DimlAsInteger

DimsAsInteger

k=a

(1)

Fori=2To10

Ifa(i)>kThenk=a(i)

Nexti

Text2=k

l=a

(1)

Fori=2To10

Ifa(i)

Nexti

Text3=l

s=0

Fori=1To10

s=s+a(i)

Nexti

Text5=s

Text4=s/10

EndSub

PrivateSubCommand3_Click()

End

EndSub

PrivateSubForm_Load()

EndSub

18.字符统计

PrivateSubCommand1_Click()

DimsAsString,i%,mAsString,n%

s=Text1

n=0

Fori=1ToLen(s)

m=Mid(s,i,1)

Ifm>="a"Andm<="z"Then

n=n+1

EndIf

Nexti

Text2=n

EndSub

PrivateSubCommand2_Click()

End

EndSub

19.最大公约数

PrivateSubCommand1_Click()

DimmAsInteger

DimnAsInteger

DimkAsInteger

m=Val(Text1)

n=Val(Text2)

Do

k=mModn

m=n

n=k

LoopUntilk=0

Text3=CStr(m)

EndSub

PrivateSubCommand2_Click()

Text1=""

Text2=""

Text3=""

EndSub

PrivateSubCommand3_Click()

End

EndSub

20.最小公倍数

OptionExplicit

PrivateSubCommand1_Click()

Dimm%,n%,k%

Dimm1%,n1%

Dims%

m=Val(Text1):

n=Val(Text2)

m1=m:

n1=n

Do

k=mModn

m=n

n=k

LoopUntilk=0

s=m1*n1/m

Text3=s

EndSub

PrivateSubCommand2_Click()

Text1=""

Text2=""

Text3=""

EndSub

PrivateSubCommand3_Click()

End

EndSub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 初中作文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1