Question How to perform calculations without pressing the equal sign in a calculator?

LMCKC207

Member
Joined
Apr 9, 2020
Messages
17
Programming Experience
Beginner
CODE FOR MY BUTTONS 0-9:
 Private Sub btnNumbers_Click(sender As Object, e As EventArgs) Handles btn9.Click, btn8.Click, btn6.Click, btn5.Click, btn4.Click, btn3.Click, btn2.Click, btn1.Click, btn0.Click


        Dim btnNum As Button = CType(sender, Button)

        input = CDbl(btnNum.Text)

        If txtInput.Text = "0" Or flag = True Then

            txtInput.Text = ""
            txtInput.Text = btnNum.Text
            flag = False


        Else

            txtInput.Text = txtInput.Text & btnNum.Text

        End If


    End Sub


CODE FOR MY OPERATORS:
 Private Sub btnSign(sender As Object, e As EventArgs) Handles btnMultiply.Click, btnMinus.Click, btnDivide.Click, btnAdd.Click

        Dim btnSigns As Button = CType(sender, Button)
        operation = btnSigns.Text


        If txtInput.Text <> 0 Then

                operation = btnSigns.Text
                txtHistory.Text = txtHistory.Text & " " & txtInput.Text & " " & btnSigns.Text
                flag = True

        Else

                operation = btnSigns.Text
                txtHistory.Text = txtHistory.Text & " " & txtInput.Text & " " & btnSigns.Text
                flag = True
            End If

    End Sub


My solution is like this:
  1. user input a number on txtInput.text then save it to variable 1.
  2. if the user presses an operator and input another number in txtInput.text that number will be saved to variable2 since variable1 has already a number in it.
  3. if the user presses an operator again do the calculation.
I'm keeping track of the operator that has been pressed so that the program will know if it needs to add,subtract, etc.
What I'm struggling with, is how can I keep track of the variable2? because I can track the variable1 obviously.

I don't know what event should I trigger to keep track of the variable2.
 
When the user enters an operator, check whether the operator variable is set. If not, the current number goes in the first numeric variable and the operator goes in the operator variable. If the operator variable is set, you put the current number in the second numeric variable, you perform the operation in the operator variable on the two numbers and put the result in the first numeric variable, then put the new operator in the operator variable.
 
Back
Top