VB实现简易计算器源码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

'fuhao存储运算符,x存储第一个数,y存储第二个数,z存储结果,flag用来标识是否连续作运算'flag2用来标识是否执行过等于操作DimfuhaoAsString,xAsSingle,yAsSingle,zAsString,flagAsInteger,flag2AsInteger'执行计算器退格操作PrivateSubbacks_Click()IfText1.TextThenText1.Text=Left(Text1.Text,Len(Text1.Text)-1)ElseText1.Text=ExitSubEndIfEndSubPrivateSubc_Click()'将计算器所有值全部置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)=0AndInStr(Text1.Text,.)Then'只能输入一个'.',输入的数开头不能为0Text1.Text=Text1.Text&IndexElseIfLeft(Text1.Text,1)=0AndText1.TextThenIfIndex=0ThenText1.Text=0ElseIfIndex0Then'PrintdddddText1.Text=Right((Text1.Text&Index),1)EndIfElseText1.Text=Text1.Text&IndexEndIfEndSub'取倒数运算PrivateSubdaoshu_Click()flag2=1IfVal(Text1.Text)0Then'分母不能为0z=1/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.TextThenIfVal(Text1.Text)=0Thenz=Text1.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(Text1.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(Text1.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(Text1.Text)ElseIffuhao=*Thenx=x*Val(Text1.Text)ElseIffuhao=/ThenIfVal(Text1.Text)0Thenx=x/Val(Text1.Text)ElseMsgBox除数不能为0,vbCritical,错误提示EndIfElsex=x+Val(Text1.Text)'Printx&cccEndIffuhao=+Text1.Text=EndIfEndSub'减法运算PrivateSubjian_Click()Ifflag=0Thenflag=flag+1x=Val(Text1.Text)fuhao=-Text1.Text=ElseIffuhao=+Thenx=x+Val(Text1.Text)ElseIffuhao=*Thenx=x*Val(Text1.Text)ElseIffuhao=/ThenIfVal(Text1.Text)0Thenx=x/Val(Text1.Text)ElseMsgBox除数不能为0,vbCritical,错误提示EndIfElsex=x-Val(Text1.Text)EndIffuhao=-Text1.Text=EndIfEndSub'乘法运算PrivateSubchen_Click()Ifflag=0Thenflag=flag+1x=Val(Text1.Text)fuhao=*Text1.Text=ElseIffuhao=+Thenx=x+Val(Text1.Text)'Printx&bbbbElseIffuhao=-Thenx=x-Val(Text1.Text)ElseIffuhao=/ThenIfVal(Text1.Text)0Thenx=x/Val(Text1.Text)ElseMsgBox除数不能为0,vbCritical,错误提示EndIfElsex=x*Val(Text1.Text)EndIffuhao=*Text1.Text=EndIfEndSub'除法运算PrivateSubchu_Click()Ifflag=0Thenflag=flag+1x=Val(Text1.Text)fuhao=/Text1.Text=ElseIffuhao=+Thenx=x+Val(Text1.Text)ElseIffuhao=-Thenx=x-Val(Text1.Text)ElseIffuhao=*Thenx=x*Val(Text1.Text)ElseIfVal(Text1.Text)0Then'判断除数是否为0x=x/Val(Text1.Text)ElseMsgBox除数不能为0,vbCritical,错误提示EndIfEndIffuhao=/Text1.Text=EndIfEndSub'小数点操作,只能有一个小数点PrivateSubxsd_Click()IfInStr(Text1.Text,.)ThenText1.Text=Text1.TextElseText1.Text=Text1.Text&.EndIfEndSub'执行等于操作PrivateSubequ_Click()y=Val(Text1.Text)flag=0flag2=1'标识执行完等于操作Iffuhao=+Thenz=Str(x+y)ElseIffuhao=-Thenz=Str(x-y)ElseIffuhao=*Thenz=Str(x*y)ElseIffuhao=/ThenIfy0Then'判断除数是否为0z=Str(x/y)ElseMsgBox除数不能为0,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

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功