资源描述:
《VB实现简易计算器源码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、'fuhao存储运算符,x存储第一个数,y存储第二个数,z存储结果,flag用来标识是否连续作运算'flag2用来标识是否执行过等于操作DimfuhaoAsString,xAsSingle,yAsSingle,zAsString,flagAsInteger,flag2AsInteger'执行计算器退格操作PrivateSubbacks_Click()IfText1.Text<>""ThenText1.Text=Left(Text1.Text,Len(Text1.Text)-1)ElseText1.Text=""ExitSubEndIfEndSubPrivateSubc_Cli
2、ck()'将计算器所有值全部置0Text1.Text=""x=0y=0EndSubPrivateSubce_Click()'将计算器输入的当前值置0Text1.Text=""y=0EndSub'点击0-9数字PrivateSubCommand14_Click(IndexAsInteger)Ifflag2=1Then'flag为1时,表示执行过等于操作,需要清空之前的计算结果Text1.Text=""flag2=flag2+1EndIfIfLeft(Text1.Text,1)="0"AndInStr(Text1.Text,".")Then'只能输入一个'.',输入的数开头不能为
3、0Text1.Text=Text1.Text&IndexElseIfLeft(Text1.Text,1)="0"AndText1.Text<>""ThenIfIndex=0ThenText1.Text="0"ElseIfIndex<>0Then'Print"ddddd"Text1.Text=Right((Text1.Text&Index),1)EndIfElseText1.Text=Text1.Text&IndexEndIfEndSub'取倒数运算PrivateSubdaoshu_Click()flag2=1IfVal(Text1.Text)<>0Then'分母不能为0z=1
4、/Text1.Text'取倒数ElseMsgBox"分母不能为0!",vbCritical,"错误提示"EndIfIfLeft(Val(z),1)="."Then'将.###转成0.###z="0"&Val(z)ElseIfLeft(Val(z),2)="-."Then'将-.###转成-0.###z="-0."&Right(Val(z),Len(z)-2)EndIfText1.Text=zEndSub'开根号运算PrivateSubkaigen_Click()flag2=1IfText1.Text<>""ThenIfVal(Text1.Text)>=0Thenz=Text1
5、.Text^(1/2)'开根号运算ElseMsgBox"负数不能开根号!",vbCritical,"错误提示"EndIfEndIfIfLeft(Val(z),1)="."Then'将.###转成0.###z="0"&Val(z)ElseIfLeft(Val(z),2)="-."Then'将-.###转成-0.###z="-0."&Right(Val(z),Len(z)-2)EndIfText1.Text=zEndSub'百分号运算,公式A?B%=A×(1+B/100)orA+(A×B/100)PrivateSubquyu_Click()flag2=1z=x*(Val(Text
6、1.Text)/100)IfLeft(Val(z),1)="."Then'将.###转成0.###z="0"&Val(z)ElseIfLeft(Val(z),2)="-."Then'将-.###转成-0.###z="-0."&Right(Val(z),Len(z)-2)EndIfText1.Text=zEndSub'取负运算PrivateSubzf_Click()flag2=1IfVal(Text1.Text)<>""ThenText1.Text=-(Text1.Text)ElseIfLeft(Text1.Text,1)="-"ThenText1.Text=Left(Text
7、1.Text,Len(Text1.Text)-1)ElseIfText1.Text=0ThenText1.Text=Left(Text1.Text,Len(Text1.Text)-1)ElseText1.Text="-"&Text1.TextEndIfEndSub'加法运算PrivateSubplus_Click()Ifflag=0Thenflag=flag+1x=Val(Text1.Text)fuhao="+"Text1.Text=""ElseIffuhao="-"Then'连续运算操作x=x-Val