资源描述:
《[计算机软件及应用]visualbasic快速入门》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、VisualBasic快速入门什么是Sub过程同用对话框n=Text2Text3=gcd(m,n)EndSub方法二:PrivateSubCommand2_Click()'最小公倍数DimmAsInteger,nAsIntegerm=Text1n=Text2Text4=m*n/(gcd(m,n))EndSubPrivateSubCommand3_Click()Text1.Text=""Text2.Text=""Text3.Text=""Text4.Text=""EndSubPrivateSubCommand4_Click()En
2、dEndSubFunctiongcd(mAsInteger,nAsInteger)'此过程并非递归,递归解法见课本P162DimkAsIntegerDok=mModnm=nn=kLoopUntilk=0gcd=mEndFunction3.冒泡排序过程;书P107Dima(10)AsIntegerPrivateSubsort(a()AsInteger,nAsInteger)DimiAsInteger,tempAsIntegerDimswitchAsBooleanswitch=TrueDoWhileswitchswitch=Fals
3、en=n-1Fori=1TonIfa(i)>a(i+1)Thenswitch=Truetemp=a(i)a(i)=a(i+1)a(i+1)=tempEndIfNextiLoopEndSubPrivateSubCommand1_Click()DimiAsIntegerFori=1To10a(i)=Int(90*Rnd)+10Text1=Text1&a(i)&""NextiEndSubPrivateSubCommand2_Click()DimiAsIntegerCallsort(a,10)Fori=1To10Text2=Text2&
4、a(i)&""NextiEndSub4.顺序查找过程;书P1461.顺序查找法(在一列数中查找某数x)基本思想:一列数放在数组a(1)---a(n)中,待查找的数放在x中,把x与a数组中的元素从头到尾一一进行比较查找。用变量p表示a数组元素下标,p初值为1,使x与a(p)比较,如果x不等于a(p),则使p=p+1,不断重复这个过程;一旦x等于a(p)则退出循环;另外,如果p大于数组长度,循环也应该停止。(这个过程可由下语句实现)p=1DoWhilex<>a(p)Andp<=np=p+1Loop下面写一查找函数Find,若找到则返
5、回下标值,找不到返回0OptionBase1PrivateFunctionFind(a()AsSingle,xAsSingle)AsIntegerDimn%,p%n=Ubound(a)p=1DoWhilepa(p)p=p+1LoopIfIfp=nAnda(p)<>xThenp=0Find=pEndFunction方法二:PrivateSubCommand1_Click()Dimb(),k%,n%b=Array(1,3,5,7,9,2,4)k=Val(InputBox("输入要查找的关键值"))Callsearch
6、(b(),k,n)Ifn>=0ThenMsgBox("找到的位置为"&n)ElseMsgBox("找不到")EndSubSubsearch(a(),ByValkey%,ByRefindex%)Dimi%Fori=LBound(a)ToUBound(a)Ifkey=a(i)Thenindex=iExitSubEndIfNextiindex=-1EndSub5.判断回文数过程;P140,4-4-3PrivateFunctionhuiwen(strAsString)DimiAsInteger,lAsIntegerstr=Text1l=
7、Len(str)Fori=1Tol/2IfMid(str,i,1)<>Mid(str,l-i+1,1)ThenLabel1.Caption=str&"不是回文数"ElseLabel1.Caption=str&"是回文数"EndIfNextiEndFunctionPrivateSubCommand1_Click()DimxAsStringhuiwen(x)EndSub6.递归函数求阶乘;书P137PrivateSubCommand1_Click()DimnAsIntegern=Val(InputBox("请输入n以求n的阶乘"))
8、Printn&"!=";fac(n)EndSubPublicFunctionfac(nAsInteger)AsIntegerIfn=1Thenfac=1Elsefac=n*fac(n-1)EndIfEndFunction7.递归函数求Fibonacci数列;(求