各位大家好,有一个问题,一直以来不明白,就是我做的计算器可以实现基本的加法,减法,乘法,除法运算,但是我想要实现连续的加法,减法,乘法,除法,我不知道该在哪里增加代码,还请高手指点一二。
就是用一个全局变量a来记录运算符号(加乘除,你的代码中貌似没有减),b则记录在文本框输入的第一个数字,当点击等号键时,根据a的值来决定执行何种算法,比如a=1就是加法,就把b和当前文本框的数字(也就是算式中的第二个数)相加,再把计算结果回填到文本框即可。乘法和除法原理一样。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2019-05-21
如果我理解没有错误的话你代码中的a是状态变量(emm...反正我是这么叫它,因为我经常这么用),b是存储输入第一个数的变量,commond18应该是等号。
如果a=1,那么用户是想做加法运算,把变量b和当前输入的数相加
如果a=2,那么用户是想做减法运算,把变量b和当前输入的数相减
如果a=3,那么用户是想做乘法运算,把变量b和当前输入的数相乘
如果a=4,那么用户是想做除法运算,把变量b和当前输入的数相除
当然如果你的代码中commond18的那部分没有缺一个条件的话,我上面的解释就有瑕疵了
如果a=1,那么用户是想做加法运算,把变量b和当前输入的数相加
如果a=2,那么用户是想做减法运算,把变量b和当前输入的数相减
如果a=3,那么用户是想做乘法运算,把变量b和当前输入的数相乘
如果a=4,那么用户是想做除法运算,把变量b和当前输入的数相除
当然如果你的代码中commond18的那部分没有缺一个条件的话,我上面的解释就有瑕疵了
第2个回答 2020-03-09
首先拖动3个文本框在右边的属性栏里把名称分别改为第一个为:text1第二个为:text2第三个为text3.
然后再拖4个控件下来分别命名:command1,command2,command3,command4
接下来就随便点一个空间输入下面的代码吧
option
explicit
private
sub
command1_click()
text3.text
=
val(text1.text)+
val(text2.text)
private
sub
command2_click()
text3.text
=
val(text1.text)-
val(text2.text)
private
sub
command3_click()
text3.text
=
val(text1.text)*
val(text2.text)
private
sub
command4_click()
text3.text
=
val(text1.text)*
val(text2.text)
end
sub
然后再拖4个控件下来分别命名:command1,command2,command3,command4
接下来就随便点一个空间输入下面的代码吧
option
explicit
private
sub
command1_click()
text3.text
=
val(text1.text)+
val(text2.text)
private
sub
command2_click()
text3.text
=
val(text1.text)-
val(text2.text)
private
sub
command3_click()
text3.text
=
val(text1.text)*
val(text2.text)
private
sub
command4_click()
text3.text
=
val(text1.text)*
val(text2.text)
end
sub
第3个回答 2008-09-24
累死我了 这是我刚做的10 以下的连续计算
例如 在text1.text中输入 1-2*3 点击Command1
看看是不是能得到结果。。。
还有写地方有错误 下班了 下次再改 !!!
Private Sub Command1_Click()
Dim b()
Dim a()
Dim n As Integer
Dim CC As String
Dim bb As String
Dim aa As Integer
Dim dd As Long
bb = Trim(Text1.Text)
aa = Len(bb)
ReDim a(aa + 2)
ReDim b(aa + 2)
For n = 1 To aa
a(n) = Mid(bb, n, 1)
Next
For n = 1 To aa
Select Case Asc(a(n))
Case 42 '*
If n <= 2 Then
a(n - 2) = "+"
End If
If n = aa - 1 Then
a(n + 2) = "+"
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
End If
End If
Case 47 '/
If n <= 2 Then
a(n - 2) = "+"
End If
If n >= aa - 1 Then
a(n - 2) = "+"
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
End If
End If
Case 43 '+
If n <= 2 Then
a(n - 2) = "+"
End If
If n >= aa Then
a(n + 2) = "*"
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or 47 Then
b(n) = 0
Else
b(n) = a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
b(n) = a(n - 1)
Else
b(n) = a(n - 1) + a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
End If
Case 45 '-
If n <= 2 Then
a(n - 2) = "+"
End If
If n = aa - 1 Then
a(n + 2) = "*"
a(n + 1) = 0
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
b(n) = 0
Else
b(n) = 0 - a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
b(n) = a(n - 1)
Else
b(n) = a(n - 1) - a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
End If
Case Else
If Asc(a(n)) <= 48 And Asc(a(n)) >= 57 Then
Exit Sub
End If
End Select
Next
For n = 1 To aa
If b(n) <> "" Then
dd = dd + Val(b(n))
End If
Next
MsgBox dd
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
'MsgBox KeyAscii
End Sub
Private Sub Form_Load()
Me.KeyPreview = True
End Sub本回答被提问者采纳
例如 在text1.text中输入 1-2*3 点击Command1
看看是不是能得到结果。。。
还有写地方有错误 下班了 下次再改 !!!
Private Sub Command1_Click()
Dim b()
Dim a()
Dim n As Integer
Dim CC As String
Dim bb As String
Dim aa As Integer
Dim dd As Long
bb = Trim(Text1.Text)
aa = Len(bb)
ReDim a(aa + 2)
ReDim b(aa + 2)
For n = 1 To aa
a(n) = Mid(bb, n, 1)
Next
For n = 1 To aa
Select Case Asc(a(n))
Case 42 '*
If n <= 2 Then
a(n - 2) = "+"
End If
If n = aa - 1 Then
a(n + 2) = "+"
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) * a(n + 1)
End If
End If
Case 47 '/
If n <= 2 Then
a(n - 2) = "+"
End If
If n >= aa - 1 Then
a(n - 2) = "+"
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
a(n + 1) = b(n)
b(n) = 0
Else
If Asc(a(n - 2)) = 45 Then
a(n - 1) = 0 - a(n - 1)
End If
b(n) = a(n - 1) / a(n + 1)
End If
End If
Case 43 '+
If n <= 2 Then
a(n - 2) = "+"
End If
If n >= aa Then
a(n + 2) = "*"
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or 47 Then
b(n) = 0
Else
b(n) = a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
b(n) = a(n - 1)
Else
b(n) = a(n - 1) + a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
End If
Case 45 '-
If n <= 2 Then
a(n - 2) = "+"
End If
If n = aa - 1 Then
a(n + 2) = "*"
a(n + 1) = 0
End If
If Asc(a(n - 2)) = 42 Or Asc(a(n - 2)) = 47 Then
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
b(n) = 0
Else
b(n) = 0 - a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
Else
If Asc(a(n + 2)) = 42 Or Asc(a(n + 2)) = 47 Then
b(n) = a(n - 1)
Else
b(n) = a(n - 1) - a(n + 1)
a(n + 1) = b(n)
b(n) = 0
End If
End If
Case Else
If Asc(a(n)) <= 48 And Asc(a(n)) >= 57 Then
Exit Sub
End If
End Select
Next
For n = 1 To aa
If b(n) <> "" Then
dd = dd + Val(b(n))
End If
Next
MsgBox dd
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
'MsgBox KeyAscii
End Sub
Private Sub Form_Load()
Me.KeyPreview = True
End Sub本回答被提问者采纳
第4个回答 2008-09-24
重复调用