在计算器中进行多次计算 [英] Perfoming multiple calculations in Calculator
问题描述
HI Everyone,
我已经构建了这个(非常基本的)计算器,并且不能一次执行多个操作(5 +5 = 10,5 + 5 + 5 = 10)因为它只保留输入的最后两个数字。我尝试过使用
HI Everyone,
I've built this (very rudimentary) calculator and can't to do more than one operation at a time (5 +5 =10, 5+5+5=10) as it's only holding the last two numbers entered. I've tried using
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Get the number, display the number, retun the focus
Number1 = Number1 + Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "+"
End Sub
但是没有帮助,也尝试过其他一些hings。我一直无法找到答案,希望有人能给我一个暗示。
以下是大部分代码:
But doesn't help, also tried quite a few other things. I haven't been able to find an answer and was hoping that someone could throw me a hint on this.
Here's most of the code:
Dim Number1 As Double
Dim Number2 As Double
Dim Numberx As Double
Dim conversion As Double
Dim User As String
Dim hasDecimal As Boolean
Dim tmpValue As Double
Private Sub frmCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub buttons(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
If txtDisplay.Text = "" Then txtDisplay.Text = sender.text Else txtDisplay.Text = txtDisplay.Text & sender.text
txtDisplay.Focus()
End Sub
Private Sub btnCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCE.Click
' Remove last entered number
txtDisplay.Text = ""
txtDisplay.Focus()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
' Clear all numbers entered and held
txtDisplay.Text = String.Empty : Number1 = 0.0 : Number2 = 0.0
txtDisplay.Focus()
End Sub
Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
' Add decimal point at point of insertion
If InStr(txtDisplay.Text, ".") > 0 Then
Exit Sub
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Get the number, display the number, retun the focus
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "+"
End Sub
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
' Get the number, display the number, retun the focus
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "-"
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
' Get the number, display the number, retun the focus
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "*"
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
' Get the number, display the number, retun the focus
Number1 = Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "/"
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
Dim Result As Double
Number2 = txtDisplay.Text
' Select math operatons
Select Case User
Case "+"
Result = Number1 + Number2
txtDisplay.Text = Result.ToString()
Case "-"
Result = Number1 - Number2
txtDisplay.Text = Result.ToString()
Case "/"
Result = Number1 / Number2
txtDisplay.Text = Result.ToString()
Case "*"
Result = Number1 * Number2
txtDisplay.Text = Result.ToString()
Case "x^"
Result = Number1 ^ Number2
txtDisplay.Text = Result.ToString()
Case "1/x"
Result = Number1 * 1 / 100
txtDisplay.Text = Result.ToString()
Case "PosNeg"
Result = Number1 * -1
txtDisplay.Text = Result.ToString()
End Select
txtDisplay.Text = Result.ToString()
End Sub
我知道这是非常Frankensteined,但它是我能够想出的,但是,除了它一次不会执行多于一个操作的事实。
___________________________________________________________
实际上,使用:
I know that it's pretty Frankensteined up, but it's what I was able to come up with that works, Well, except for the fact that it won't perform more than one op at a time.
___________________________________________________________
Actually, using:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Number1 = Number1 + Val(txtDisplay.Text)
txtDisplay.Text = ""
txtDisplay.Focus()
User = "+"
适用于添加( 5 + 6 + 9 + 4 = 24),但我无法理解处理减法。我添加了许多不同类型的
Works for the addition (5+6+9+4=24), but I can't figure out to handle the subtraction. I've put in many different types of
Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
num1 - Val(txtDisplay.Text)
(+, - ,+ - 等)都给我一个错误的结果。任何帮助都将非常感激!!
( +, -, +-, etc.) all give me a wrong result. Any help at all would be much appreciated!!
推荐答案
从查看代码看起来它的运行方式与它的设计方式相同。
From looking at your code it looks like it is running the way it was designed.
- 当我按
5
时,它会输出到TextBox。 - 当我按下
+
,Number1
设置为5
; TextBox被清除。 - 我再次按
5
然后输出到TextBox。 - 当我按<$ c时$ c> + ,
Number1
设置为5
; TextBox已清除。 (再次) - 我再次按
5
然后输出到TextBox。 - 当我按
=
,Number2
设置为5
;算术加法运算在上执行。结果= Number1 + Number2 = 10
- When I press
5
it is output to the TextBox. - When I press
+
,Number1
is set to5
; the TextBox is cleared. - I press
5
again and it is output to the TextBox. - When I press
+
,Number1
is set to5
; the TextBox is cleared. (again) - I press
5
again and it is output to the TextBox. - When I press
=
,Number2
is set to5
; The arithmetic addition operation is performed onResult = Number1 + Number2 = 10
每次按下一个操作员按钮,您需要评估和更新结果
。让我们说有3个变量,结果
,数字
,以及运算符
。
Each time an operator button is pressed, you need to evaluate and update Result
. Lets says there are 3 variables, Result
, Number
, and Operator
.
Result = 0
(5 pressed)
Number = 5
(+ pressed)
Operator = +
Result = {Result==0} {Operator==+} {Number==5} = 5
(5 pressed)
Number = 5
(+ pressed)
Operator = +
Result = {Result==5} {Operator==+} {Number==5} = 10
(5 pressed)
Number = 5
(= pressed)
Operator = + (unchanged)
Result = {Result==10} {Operator==+} {Number==5} = 15
是的,您解决此问题的方式是......有趣。
我会让你重新思考问题以及如何简化和简化问题,而不是告诉你你做错了什么。 :)这是成为更好的程序员的一部分。您应该能够将所有这些减少到20行代码。提示:使用 Button.Tag
对您有利。
Yes, the way you have approached this problem is... interesting.
Instead of telling you what you did "wrong", I'll let you work on rethinking the problem and how it can be simplified and streamlined. :) It's part of becoming a better programmer. You should be able to reduce all of this to maybe 20 lines of code. Tip: Use Button.Tag
to your advantage.
这篇关于在计算器中进行多次计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!