资源描述:
《vb程序-利用栈进行运算》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1站PrivateSubCommand1_Click()Dimos(1To10)AsString,ns(1To10)AsString'数组os是符号栈,'ns是数值栈DimtopoAsInteger,topnAsIntegerDimw,fAsVariant,t,qAsVariantDimwnAsInteger,qnAsIntegertopo=1topn=1t=0n=1os(topo)=";"DoWhilet<>2s=Trim(Text1.Text)'去空格,读取表达式Ifs=""Thens=MsgBox("错误,请输入数据"
2、,,"错误提示"):ExitDoEndIfIfMid(s,Len(s),1)<>";"Thens=MsgBox("错误,请在表达式结尾加上分号",,"错误提示"):ExitDoEndIfIft=0Thenw=Mid(s,n,1)'这是本程序的缺陷,只能实现个位数的运算n=n+1EndIf'实现表达式中字符的逐一分离IfNotIsNumeric(w)Then'识别表达式中的符号SelectCasewCase";"wn=0Case"-","+"wn=1Case"/","*"wn=2Case"^"wn=3EndSelectq=os
3、(topo)SelectCaseq'定义符号的优先级Case";"qn=0Case"-","+"qn=1Case"/","*"qn=2Case"^"qn=3EndSelectIfwn>qnThen'入栈topo=topo+1os(topo)=wt=0ElseIfq=";"Andw=";"Thenz=ns(topn)t=2Elsex=Val(ns(topn))topn=topn-1y=Val(ns(topn))topn=topn-1q=os(topo)topo=topo-1SelectCaseq'出栈运算Case"-"f=y-
4、xCase"+"f=y+xCase"*"f=y*xCase"/"f=y/xCase"^"f=y^xEndSelecttopn=topn+1ns(topn)=f'运算的临时结果入栈t=1EndIfElsetopn=topn+1ns(topn)=wEndIfLoopText2.Text=zText1.SetFocusEndSubPrivateSubCommand2_Click()Text1=""Text2=""Text1.SetFocusEndSubPrivateSubForm_Load()Text1=""Text2=""End
5、Sub2.冒泡OptionBase1Dima()AsVariant,fAsIntegerPrivateSubCommand1_Click()a=Array(8,5,6,3,5,9,10,2,1,4)'8、5、6、3、5、9、10、2、1、4Picture1.Print"排列前的数组为:"Fori=1ToUBound(a)Picture1.Printa(i);Nextif=UBound(a)DoWhilef>0k=f-1:f=0Forj=1TokIfa(j)>=a(j+1)ThenT=a(j):a(j)=a(j+1):a(j+
6、1)=T:f=jEndIfNextjLoopPicture1.PrintPicture1.Print"排列后的数组为:"Fori=1ToUBound(a)Picture1.Printa(i);NextiPicture1.PrintEndSubPrivateSubCommand2_Click()Picture1.ClsEndSub3.对半Dimr()AsVariantPrivateSubCommand1_Click()Dimi,j,low,high,mAsIntegerr=Array(0,8,5,6,3,5,9,10,2,1,
7、4)Fori=2To10r(0)=r(i):low=1:high=i-1DoWhilehigh>=lowm=(high+low)2Ifr(0)8、)Dimlow,high,m,zhaoAsInteger,c1AsBoolean,c2AsBooleanlow=1:high=10zhao=Val(InputBox("输入要查找的数","输入","",100,100))DoWhilehigh>lowm=(high+low)2Ifr(m)