VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This is Calculator with all operations

by Muhammad Omar Muftakhar (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 4th June 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This is Calculator with all operations

Rate This is Calculator with all operations



'Name:Simple Calculator and even simplier code
'Date created: 17/03/2004

Option Explicit 'Explicitly declare all variables

'Global Variables
Dim iCounter As Integer
Dim first As Double
Dim second As Double
Dim result As Double
Dim haveFirst As Boolean
Dim haveSecond As Boolean
Dim sF As Boolean
Dim haveMathOp As Boolean
Dim mathOp As Integer

Function getResults(ByVal first, ByVal second, ByVal mathOp) As Double
On Error GoTo endMe

'get math operation(s)
Select Case CStr(mathOp)
Case "10": getResults = first + second
Case "11": getResults = first - second
Case "12": getResults = first * second
Case "13": getResults = first / second
Case "17": getResults = Sqr(first)
Case "18": getResults = Exp(first)
End Select

finish:
Exit Function

endMe:
MsgBox Err.Description
GoTo finish

End Function

Private Sub Command1_Click(Index As Integer)

'get first/second input #'s and validate
Select Case CStr(Index)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
If Text1.Text = 0 And Index <> 9 Then
Text1.Text = Index + 1
ElseIf Text1.Text = 0 And Index = 9 Then
Text1.Text = 0
ElseIf Text1.Text <> 0 And Index = 9 Then
Text1.Text = Text1.Text & 0
Else
Text1.Text = Text1.Text & (Index + 1)
End If
Case "10", "11", "12", "13":
haveFirst = True: first = Text1.Text: Text1.Text = 0: mathOp = Index
Case "14":
If Not haveFirst Then
haveFirst = True: Text1.Text = 0
Else
haveSecond = True: second = Text1.Text: Text1.Text = 0
End If
Case "15": GoTo freeMem
Case "16":
If Not haveFirst Then
first = 0
Text1.Text = 0
ElseIf Not haveSecond Then
second = 0
Text1.Text = 0
Else
Text1.Text = 0
End If
Case "17", "18": sF = True: first = Text1.Text: mathOp = Index: GoTo specFunc
End Select

'If div error (by zero) goto user defined error code
If haveSecond And second = 0 And mathOp = 13 Then
GoTo denomError
End If

'return results and reset second bool variable and second variable to continue
'calculating based on previous result
If haveFirst And haveSecond Then
specFunc:
If sF Then
sF = False
result = getResults(first, second, mathOp)
Else
result = getResults(first, second, mathOp)
haveSecond = False
second = 0
End If
Text1.Text = result
End If

'End label
endMe:
Exit Sub

'Since variables are global, they need to be freed from memory, do so here
freeMem:
result = 0
Text1.Text = "0"
haveFirst = False
haveSecond = False
first = 0
second = 0
GoTo endMe

'Error check
denomError:
MsgBox "ERROR" & Chr(13) & Chr(13) & _
"Denominator is not valid in division operation. Please try again", vbCritical, "Input Error"
haveSecond = False
second = 0
GoTo endMe
End Sub

Private Sub Command1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command1(Index)
.FontUnderline = True
.FontBold = True
End With
End Sub

Private Sub Command1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
With Command1(Index)
.FontUnderline = False
.FontBold = False
End With
End Sub

Private Sub Form_Load()

'format form controls and global boolean variables
haveMathOp = False
haveFirst = False
haveSecond = False
sF = False

frmcalculator.Caption = "Simple Calculator"


With Text1
.Text = "0"
.Alignment = 1
.FontBold = True
.BorderStyle = 1
.Appearance = 0
.Enabled = False
End With

For iCounter = 0 To Command1.Count - 1

If iCounter < 9 Then
Command1(iCounter).Caption = iCounter + 1
ElseIf iCounter = 9 Then
Command1(iCounter).Caption = "0"
Else

Select Case CStr(iCounter)
Case "10": Command1(iCounter).Caption = "+"
Case "11": Command1(iCounter).Caption = "-"
Case "12": Command1(iCounter).Caption = "*"
Case "13": Command1(iCounter).Caption = "/"
Case "14": Command1(iCounter).Caption = "="
Case "15": Command1(iCounter).Caption = "C"
Case "16": Command1(iCounter).Caption = "CE"
Case "17": Command1(iCounter).Caption = "Sqr"
Case "18": Command1(iCounter).Caption = "Exp"
End Select

End If
Next iCounter

End Sub

Private Sub mnuExit_Click()
Unload Me
End Sub


Download this snippet    Add to My Saved Code

This is Calculator with all operations Comments

No comments have been posted about This is Calculator with all operations. Why not be the first to post a comment about This is Calculator with all operations.

Post your comment

Subject:
Message:
0/1000 characters