VB计算器代码的问题

[复制链接]
查看11 | 回复4 | 2010-2-18 15:38:09 | 显示全部楼层 |阅读模式
可以用String数据格式操作数,楼上的方法有小缺憾,但思路完全相同。例如输入“9”:Dim X,Y as string ' 这里把类型改成String。Private Sub Command9_Click()x = x & “9”Text1.Text = xEnd Sub在运算时,需将x、y强制转为数字格式 例如: Result = val(x) + val(y)。
回复

使用道具 举报

千问 | 2010-2-18 15:38:09 | 显示全部楼层
你这个计算器按钮1为什么是x*10+1?要这样才行啊text1.text = text1.text & "1"
回复

使用道具 举报

千问 | 2010-2-18 15:38:09 | 显示全部楼层
这个计算器程序中,你的按钮设置为按钮数组比较合理:数字0-9设置为Command1(0) - Command1(9)+、-、*、/ 设置为Command2(0) - Command2(3)= 设置为Command3. 设置为Command1(10)C 设置为Command4代码如下:Dim X As Single, Y As Single, FH As StringPrivate Sub Command1_Click(Index As Integer)Dim K As IntegerK = Command1(Index).IndexIf K10 Then
If Right(Text1.Text, 1) = "." Then
Text1.Text = Text1.Text & K
ElseIf Int(Val(Text1.Text)) = Val(Text1.Text) And Right(Text1.Text, 1)"." Then
Text1.Text = Val(Text1.Text) * 10 + K
ElseIf Int(Val(Text1.Text))Val(Text1.Text) And Right(Text1.Text, 1)"." Then
Text1.Text = Text1.Text & K
End IfElse
Text1.Text = Text1.Text & "."End IfIf FH = "" ThenX = Val(Text1.Text)ElseY = Val(Text1.Text)End IfEnd SubPrivate Sub Command2_Click(Index As Integer)Dim K1 As IntegerK1 = Command2(Index).IndexText1.Text = ""
If Command2(K1).Caption = "+" Then
FH = "+"
ElseIf Command2(K1).Caption = "-" Then
FH = "-"
ElseIf Command2(K1).Caption = "*" Then
FH = "*"
ElseIf Command2(K1).Caption = "/" Then
FH = "/"
End IfEnd SubPrivate Sub Command3_Click()If X0 And Y0 Then
If FH = "+" Then
Text1.Text = X + Y
ElseIf FH = "-" Then
Text1.Text = X - Y
ElseIf FH = "*" Then
Text1.Text = X * Y
ElseIf FH = "/" Then
If Y = 0 Then
MsgBox "除数不能为0", 16, "错误!"
Exit Sub
End If
Text1.Text = X / Y
End If
X = Val(Text1.Text)Else
MsgBox "操作数不足!", 16, "错误!"End IfEnd SubPrivate Sub Command4_Click()Text1.Text = "": X = 0: Y = 0: FH = ""End SubPrivate Sub Form_Load()Text1.Text = "": X = 0: Y = 0: FH = ""End Sub
回复

使用道具 举报

千问 | 2010-2-18 15:38:09 | 显示全部楼层
DimaAsBoolean,ysf%,jsjg#,xsd1AsBoolean,xsd2AsBooleanPrivateSubCommand1_Click(IndexAsInteger)Dimx$,i%Label4.Caption="输入"SelectCaseIndexCase0x=Chr(48)Case1x=Chr(49)Case2x=Chr(50)Case3x=Chr(51)Case4x=Chr(52)Case5x=Chr(53)Case6x=Chr(54)Case7x=Chr(55)Case8x=Chr(56)Case9x=Chr(57)Case10x=Chr(45)Case11x=Chr(46)EndSelectIfa=FalseThenIfIndex=11ThenIfxsd1=FalseThenText1=Text1&xxsd1=TrueEndIfElseIfIndex=10ThenIfText1=""ThenText1="-"ElseText1=Text1&xEndIfElseIfaThenIfIndex=11ThenIfxsd2=FalseThenText2=Text2&xxsd2=TrueEndIfElseIfIndex=10ThenIfText2=""ThenText2="-"ElseText2=Text2&xEndIfEndIfEndSubPrivateSubCommand2_Click(IndexAsInteger)Dimcs1#,cs2#IfIndex=0ThenText1.Text=""Text2.Text=""Label4.Caption="清空"ElseIfIndex=1Thencs1=Text1cs2=Text2Label4.Caption="计算"SelectCaseysfCase0jsjg=cs1+cs2Case1jsjg=cs1-cs2Case2jsjg=cs1*cs2Case3IfText2>0Thenjsjg=cs1/cs2Elsel=MsgBox("除数不能少于或者等于0!请重新在“参数二”中输入",65,"错误提示:")Ifl=2ThenEndEndIfCase4IfText2>0Thenjsjg=cs1/cs2*100Elsel=MsgBox("除数不能少于或者等于0!请重新在“参数二”中输入",65,"错误提示:")Ifl=2ThenEndEndIfCase5jsjg=cs1^cs2EndSelectEndIfText3=jsjgEndSubPrivateSubCommand3_Click(IndexAsInteger)a=TrueSelectCaseIndexCase0ysf=0Label4.Caption="加"Case1ysf=1Label4.Caption="减"Case2ysf=2Label4.Caption="乘"Case3Label4.Caption="除"ysf=3Case4ysf=4Label4.Caption="百分"Case5ysf=5Label4.Caption="幂"EndSelectEndSubPrivateSubCommand4_Click()Text1=Text3EndSubPrivateSubCommand5_Click()Text2=Text3EndSubPrivateSubCommand6_Click()Text1=Asc("/")EndSubPrivateSubCommand7_Click()Text1=""EndSubPrivateSubCommand8_Click()Text2=""EndSubPrivateSubText1_KeyPress(KeyAsciiAsInteger)Dimi%Label4.Caption="输入"IfKeyAscii=8ThenKeyAscii=8ElseIfKeyAscii=37ThenKeyAscii=0Command3(4)=TrueText2.SetFocusElseIfKeyAscii=42ThenKeyAscii=0Command3(2)=TrueText2.SetFocusElseIfKeyAscii=94ThenKeyAscii=0Command3(5)=TrueText2.SetFocusElseIfKeyAscii=43ThenKeyAscii=0Command3(0)=TrueText2.SetFocusElseIfKeyAscii=45ThenIfText1=""Thenkeyaseii=45ElseIfLen(Text1)=1AndText1="-"ThenKeyAscii=0l=MsgBox("不能只有一个负数,否则无法进行下一步运算,请重新在“参数一”中输入",65,"错误提示:")Ifl=2ThenEndElseIfLen(Text1)>2ThenKeyAscii=0Command3(1)=TrueText2.SetFocusEndIfElseIfKeyAscii=47ThenKeyAscii=0Command3(3)=TrueText2.SetFocusElseIfKeyAscii=46ThenIfxsd2=FalseThenKeyAscii=46xsd2=TrueElseIfxsd2ThenKeyAscii=0EndIfElseIfKeyAscii57ThenKeyAscii=0EndIfEndSubPrivateSubText2_KeyPress(KeyAsciiAsInteger)Label4.Caption="输入"IfKeyAscii=8ThenKeyAscii=8ElseIfKeyAscii=13ThenKeyAscii=0Command2(1)=TrueElseIfKeyAscii=45ThenIfText2=""Thenkeyaseii=45ElseIfLen(Text2)>=1ThenKeyAscii=0EndIfElseIfKeyAscii=46ThenIfxsd2=FalseThenKeyAscii=46xsd2=TrueElseIfxsd2ThenKeyAscii=0EndIfElseIfKeyAscii57ThenKeyAscii=0EndIfEndSub试试我这个吧,功能比较全了,数字跟小数点为一组按钮控件数组;运算符号为一组按钮控件数组;等于跟清空为一组按钮控件数组;


回复

使用道具 举报

千问 | 2010-2-18 15:38:09 | 显示全部楼层
我已经回答你了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行