简体   繁体   English

Visual Basic计算器的逻辑

[英]Logic with Visual Basic Calculator

I'm working on a simple calculator program. 我正在研究一个简单的计算器程序。 I know that I am missing something. 我知道我缺少什么。 I've followed steps that have been provided, but I have already pointed out several inconsistencies previously so I am sure that this is the case again or information is just plain "missing". 我已经按照提供的步骤进行操作,但是我之前已经指出了一些不一致之处,因此我可以确定再次出现这种情况,或者信息只是“丢失”。 I have two issues. 我有两个问题。 One, is that when I press a second number before pressing an operator button, the second (and all consecutive number buttons pressed) concatenates twice. 一种是,当我在按下操作员按钮之前按下第二个数字时,第二个(以及所有连续的数字按钮被按下)连接两次。 Meaning, if I press "1" then "2", I get "122". 意思是,如果我按“ 1”然后按“ 2”,则得到“ 122”。 If I then press "3" I get "12233". 如果再按“ 3”,则得到“ 12233”。 The second problem I have is that I cannot figure out how to code the equal button. 我的第二个问题是我无法弄清楚如何编码等号按钮。 I thought that the variable dblCurrentNumber.ToString() would be what I assign to the Equals_click . 我认为变量dblCurrentNumber.ToString()将是我分配给Equals_click But, that does not work. 但是,这不起作用。 I'm not looking for anyone to tell me the answer or complete my assignment for me. 我不希望有人告诉我答案或完成我的任务。 That would defeat the purpose of learning. 那会破坏学习的目的。 I'm just looking for a clue as to what I'm missing. 我只是在寻找我所缺少的线索。

Option Explicit On
Option Strict On
Imports System.Math

Public Class Calculator
    'declare the global variables here
    Dim dblResult As Double 'Stores the result of an operation
    Dim dblCurrentNumber As Double 'Stores the current number in the display
    Dim dblMemory As Double 'Stores a value placed in memory by the user
    Dim blnStartNewNumber As Boolean 'Determines whether a new number should be started in the display
    Dim strLastMathOperator As String 'Stores the operation selected by the user; possible values include Clear, Add, Subtract, Multiply, Divide or Equals


'''<summary>
'''clears the values of all variables
'''required to reset the calculator
'''</summary>
'''<remarks>none</remarks>
Private Sub resetCalculator()
    Me.lblResult.Text = "0"
    dblResult = 0
    strLastMathOperator = "Clear"
    blnStartNewNumber = True
    dblCurrentNumber = 0
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    resetCalculator()
End Sub
Private Sub btnPower_Click(sender As Object, e As EventArgs) Handles btnPower.Click
    'Clears the values of all variables
    resetCalculator()
End Sub

Private Sub buildNumber(ByVal strNumber As String)
    lblResult.Text = lblResult.Text & strNumber
    dblCurrentNumber = Convert.ToDouble(lblResult.Text)
    'Should we start a new number or add to an existing number?
    If blnStartNewNumber Then
        'start a new number
        lblResult.Text = strNumber
    Else
        'append to the current number
        lblResult.Text = lblResult.Text & strNumber
    End If
    blnStartNewNumber = False
End Sub

''' <summary>
''' Applies the last operator to result using current number
''' </summary>
''' <param name="strOperation">the math operation to 
'''     perform +, -, *, / or clear
'''</param>

Private Sub handleOperator(ByVal strOperation As String)
    strLastMathOperator = strOperation
    Select Case strLastMathOperator.ToUpper
        Case "ADD"
            dblResult = dblResult + dblCurrentNumber
        Case "SUBTRACT"
            dblResult = dblResult - dblCurrentNumber
        Case "MULTIPLY"
            dblResult = dblResult * dblCurrentNumber
        Case "DIVIDE"
            dblResult = dblResult / dblCurrentNumber
        Case Else
            dblResult = dblCurrentNumber
    End Select
    'dblCurrentNumber = dblResult
    Me.lblResult.Text = dblCurrentNumber.ToString
    Me.blnStartNewNumber = True
    btnDecimal.Enabled = True

End Sub

Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
    buildNumber("0")
End Sub

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    buildNumber("1")
End Sub

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
    buildNumber("2")
End Sub

Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
    buildNumber("3")
End Sub

Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
    buildNumber("4")
End Sub

Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
    buildNumber("5")
End Sub

Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
    buildNumber("6")
End Sub

Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
    buildNumber("7")
End Sub

Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
    buildNumber("8")
End Sub

Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
    buildNumber("9")
End Sub

Private Sub btnSquareRoot_Click(sender As Object, e As EventArgs) Handles btnSquareRoot.Click
    dblCurrentNumber = Sqrt(dblCurrentNumber)
    Me.lblResult.Text = Str(dblCurrentNumber)
End Sub

Private Sub btnPercent_Click(sender As Object, e As EventArgs) Handles btnPercent.Click
    Me.lblResult.Text = Str(dblCurrentNumber / 100)
    dblCurrentNumber = dblCurrentNumber / 100
    blnStartNewNumber = True
End Sub

Private Sub btnPlusOrMinus_Click(sender As Object, e As EventArgs) Handles btnPlusOrMinus.Click
    dblCurrentNumber = (dblCurrentNumber * (-1))
    Me.lblResult.Text = dblCurrentNumber.ToString()
End Sub

Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
    handleOperator("DIVIDE")
End Sub

Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
    handleOperator("MULTIPLY")
End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
    handleOperator("SUBTRACT")
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    handleOperator("ADD")
End Sub

Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
    dblCurrentNumber.ToString()
End Sub
End Class

First problem is an easy one: 第一个问题是一个简单的问题:

 1 Private Sub buildNumber(ByVal strNumber As String)
 2     lblResult.Text = lblResult.Text & strNumber
 3     dblCurrentNumber = Convert.ToDouble(lblResult.Text)
 4     'Should we start a new number or add to an existing number?
 5     If blnStartNewNumber Then
 6         'start a new number
 7         lblResult.Text = strNumber
 8     Else
 9         'append to the current number
10         lblResult.Text = lblResult.Text & strNumber
11     End If
12     blnStartNewNumber = False
13 End Sub

Have a look at lines 2, 7, and 10. 看一下第2、7和10行。

In the case where you're appending a digit, line 2 appends it regardless of state (first or subsequent digit). 如果要添加数字,则无论状态(第一个或后续数字)如何,第2行都会添加它。 Then line 10 appends it again! 然后第10行再次将其追加

That has no effect for the first digit (when blnStartNewNumber is true) because, while line 2 appends the digit, that's thrown away when setting the value in line 7. 这对第一个数字没有影响(当blnStartNewNumber为true时),因为尽管第2行附加了数字,但在第7行中设置该值时会丢弃该数字。

You should probably be asking yourself about the utility of line 2 in that code (nudge, nudge, wink, wink). 您可能应该问自己有关该代码中第2行的用途(轻推,轻推,眨眼,眨眼)。


In terms of what to do in the equals subroutine, have a look at the difference between it and (for example) the square root handler. 关于equals子例程中的操作,请看一下它与(例如)平方根处理程序之间的区别。 You'll see the latter actually does something with the result rather than just calculating it (and presumably throwing that result away). 您会看到后者实际上对结果做了一些事情,而不是仅仅对其进行计算(并且可能会将结果丢弃)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM