使用Visual Basic计算器的逻辑

时间:2017-03-14 02:49:09

标签: vb.net

我正在开发一个简单的计算器程序。我知道我错过了什么。我已经按照已经提供的步骤进行了操作,但我之前已经指出了一些不一致之处,所以我确信这种情况会再次发生,或者信息只是简单的"缺失"。我有两个问题。一,是当我在按下操作按钮之前按下第二个数字时,第二个(和所有连续数字按钮)连接两次。意思是,如果我按下" 1"然后" 2",我得到" 122"。如果我然后按" 3"我得到" 12233"。我遇到的第二个问题是我无法弄清楚如何编写相等的按钮。我认为变量dblCurrentNumber.ToString()将是我分配给Equals_click的内容。但是,这不起作用。我没有找任何人告诉我答案或完成我的任务。这会破坏学习的目的。我只是想找到我失踪的线索。

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

1 个答案:

答案 0 :(得分:1)

第一个问题很简单:

 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

看看第2行,第7行和第10行。

如果您要追加数字,则无论状态如何(第一个或后一个数字),第2行都会附加数字。然后第10行再次附加

这对第一个数字没有影响(当blnStartNewNumber为真时),因为,当第2行附加数字时,在第7行设置值时会丢弃该数字。

你可能应该问自己第2行在该代码中的效用(轻推,轻推,眨眼,眨眼)。

equals子例程中的操作而言,请查看它与(例如)平方根处理程序之间的区别。你会看到后者实际上对结果做了而不是仅仅计算它(并且可能会把结果扔掉)。