Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27489

VS 2010 Another calculator question

$
0
0
I've been building a calculator program and I have it mostly working, except for a few test sequences. I'm at wits end as to how to fix these.
They are:
1 + 2 + 3 + 4 = = = must be equal to 18
3 + 4 = - 5 = must be equal to 2
2 + 3 = 9 - 5 = must be equal to 4

I have attached the project file as an attachment. Any help is greatly appreciated.
Code:

Public Class Calculator
    'holds operands
    Dim Operand1 As Double
    Dim Operand2 As Double
    'holds temporary values
    Private tmpValue As Double
    'True if decimal, else false
    Private hasDecimal As Boolean
    Private inputStatus As Boolean
    Private clearText As Boolean
    'variable to hold Operater
    Private calcFunc As String
   
    Private Sub btnZero_Click(sender As Object, e As EventArgs) Handles btnZero.Click, btnOne.Click, btnTwo.Click, btnThree.Click, btnFour.Click, btnFive.Click, btnSix.Click, btnSeven.Click, btnEight.Click, btnNine.Click
        If inputStatus = False Then
            txtDisplay.Text += sender.Text
        Else
            txtDisplay.Text = sender.Text
            inputStatus = False
        End If
    End Sub

    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.Text.Length - 1)
    End Sub

    Private Sub btnCE_Click(sender As Object, e As EventArgs) Handles btnCE.Click
        txtDisplay.Text = ""
    End Sub

    Private Sub btnC_Click(sender As Object, e As EventArgs) Handles btnC.Click
        Operand1 = 0
        Operand2 = 0
        txtDisplay.Text = ""
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        If txtDisplay.Text.Length <> 0 Then
            If calcFunc = String.Empty Then
                Operand1 = CDbl(txtDisplay.Text)
                txtDisplay.Text = String.Empty
            Else
                CalculateTotals()
                txtDisplay.Text = ""
            End If
            calcFunc = "Add"
            hasDecimal = False
        End If
    End Sub

    Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
        If txtDisplay.Text.Length <> 0 Then
            If calcFunc = String.Empty Then
                Operand1 = CDbl(txtDisplay.Text)
                txtDisplay.Text = String.Empty
            Else
                CalculateTotals()
                txtDisplay.Text = ""
            End If
            calcFunc = "Subtract"
            hasDecimal = False
        End If
    End Sub

    Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
        If txtDisplay.Text.Length <> 0 Then
            If calcFunc = String.Empty Then
                Operand1 = CDbl(txtDisplay.Text)
                txtDisplay.Text = String.Empty
            Else
                CalculateTotals()
                txtDisplay.Text = ""
            End If
            calcFunc = "Multiply"
            hasDecimal = False
        End If
    End Sub

    Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
        If txtDisplay.Text.Length <> 0 Then
            If calcFunc = String.Empty Then
                Operand1 = CDbl(txtDisplay.Text)
                txtDisplay.Text = String.Empty
            Else
                CalculateTotals()
                txtDisplay.Text = ""
            End If
            calcFunc = "Divide"
            hasDecimal = False
        End If
    End Sub

    Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
        If txtDisplay.Text.Length <> 0 AndAlso Operand1 <> 0 Then
            CalculateTotals()
            hasDecimal = False
        End If
    End Sub

    Private Sub btnNegative_Click(sender As Object, e As EventArgs) Handles btnNegative.Click
        txtDisplay.Text = -1 * txtDisplay.Text
    End Sub

    Private Sub btnDecimal_Click(sender As Object, e As EventArgs) Handles btnDecimal.Click
        If InStr(txtDisplay.Text, ".") > 0 Then
            Exit Sub
        Else
            txtDisplay.Text = txtDisplay.Text & "."
        End If
    End Sub

    Private Sub CalculateTotals()
        Operand2 = CDbl(txtDisplay.Text)
        Select Case calcFunc
            Case "Add"
                Operand1 = Operand1 + Operand2
            Case "Subtract"
                Operand1 = Operand1 - Operand2
            Case "Divide"
                Operand1 = Operand1 / Operand2
            Case "Multiply"
                Operand1 = Operand1 * Operand2
        End Select
        txtDisplay.Text = CStr(Operand1)
        inputStatus = True
    End Sub
End Class

Attached Files

Viewing all articles
Browse latest Browse all 27489

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>