Creating a calculator in Visual Basic,the instructions given to me by the professor and type the code in the order
Option Explicit On
Option Strict On
Imports System.Math
PublicClassForm1
'declare the golbal variables here
Dim dblResult AsDouble'contains the result of a calculation
PrivateSub btnPower_Click(sender AsObject, e AsEventArgs) Handles btnPower.Click
'clear the values of all variables
resetCalculator()
dblResult = 0
Me.lblResult.Text = "0"
EndSub
PrivateSub Form1_Load(sender AsObject, e AsEventArgs) HandlesMyBase.Load
'clear the values of all variables
PrivateSub resetCalculator()
Me.lblResult.Text = "0"
dblResult = 0
strLastMathOperator = "Clear"
blnStartNewNumber = True
dblCurrentNumber = 0
EndSub
PrivateSub 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
Else
'append to the current number
lblResult.Text = lblResultResult.Text & strNumber
EndIf
blnStartNewNumber = False
EndSub
buildNumber ("2")
lblResult.Text = lblResult.Text & strNumber
dblCurrentNumber = Convert.ToDouble(lblResult.Text)
Me.lblResult.Text = ""
PrivateSub btn0_Click(sender AsObject, e AsEventArgs) Handles btn0.Click
buildNumber("0")
EndSub
PrivateSub btn1_Click(sender AsObject, e AsEventArgs) Handles btn1.Click
buildNumber("1")
EndSub
PrivateSub btn2_Click(sender AsObject, e AsEventArgs) Handles btn2.Click
buildNumber("2")
EndSub
PrivateSub btn3_Click(sender AsObject, e AsEventArgs) Handles btn3.Click
buildNumber("3")
EndSub
PrivateSub btn4_Click(sender AsObject, e AsEventArgs) Handles btn4.Click
buildNumber("4")
EndSub
PrivateSub btn5_Click(sender AsObject, e AsEventArgs) Handles btn5.Click
buildNumber("5")
EndSub
PrivateSub btn6_Click(sender AsObject, e AsEventArgs) Handles btn6.Click
buildNumber("6")
EndSub
PrivateSub btn7_Click(sender AsObject, e AsEventArgs) Handles btn7.Click
buildNumber("7")
EndSub
PrivateSub btn8_Click(sender AsObject, e AsEventArgs) Handles btn8.Click
buildNumber("8")
EndSub
PrivateSub btn9_Click(sender AsObject, e AsEventArgs) Handles btn9.Click
buildNumber("9")
EndSub
PrivateSub btnSQRT_Click(sender AsObject, e AsEventArgs) Handles btnSQRT.Click
dblCurrentNumber = Sqrt(dblCurrentNumber)
Me.lblResult.Text = Str(dblCurrentNumber)
EndSub
PrivateSub btnPercent_Click(sender AsObject, e AsEventArgs) Handles btnPercent.Click
Me.lblResult.Text = Str(dblCurrentNumber / 100)
dblCurrentNumber = dblCurrentNumber / 100
blnStartNewNumber = True
EndSub
PrivateSub btnPlusMinus_Click(sender AsObject, e AsEventArgs) Handles btnPlusMinus.Click
dblCurrentNmber = dblCurrentNumber * (-1)
Me.lblResult.Text = dblCurrentNumber.ToString()
EndSub
'''
'''Applies the last operator to result using current number
'''
''' the math operation to perform +,-,*,/ or clear
'''
''' <
PrivateSub handleOperator(ByVal strOperation AsString)
handleOperator("Add")
PrivateSub handleOperator(ByVal strOperation AsString)
strLastMathOperator = strOperation
EndSub
SelectCase strLastMathOperator.ToUpper
Case "ADD"
dblResult = dblResult + dblCurrentNumber
Case "SUBTRACT"
dblResult = dblResult - dblCurrentNumber
Case "MULTIPLY"
dblResult = dblResult * dblCurrentNumber
Case "DIVIDE"
dblResult = dblResult / dblCurrentNumber
CaseElse
dblResult = dblCurrentNumber
EndSelect
dblCurrentNumber = dblResult
Me.lblResult.Text = dblCurrentNumber.ToString
Me.blnStartNewNumber = True
btnDecimal.Enabled = True
PrivateSub btnDecimal_Click(sender AsObject, e AsEventArgs) Handles btnDecimal.Click
buildNumber(".")
If blnStartNewNumber Then
buildNumber("0.")
Else
buildNumber(".")
EndIf
btnDecimal.Enabled = False
EndSub
PrivateSub btnMemoryAdd_Click(sender AsObject, e AsEventArgs) Handles btnMemoryAdd.Click
dblMemory = dblMemory + Convert.ToDouble(lblResult.Text)
EndSub
PrivateSub btnMemoryClear_Click(sender AsObject, e AsEventArgs) Handles btnMemoryClear.Click
dblMemory = 0
EndSub
PrivateSub btnMemoryRecall_Click(sender AsObject, e AsEventArgs) Handles btnMemoryRecall.Click
lblResult.Text = dblMemory.ToString
dblCurrentNumber = dblMemory
EndSub
PrivateSub btnMemorySubtract_Click(sender AsObject, e AsEventArgs) Handles btnMemorySubtract.Click
dblMemory = dblMemory - Convert.ToDouble(lblResult.Text)
EndSub
EndClass