Monday, June 14, 2010

STANDARD CALCULATOR

Buzz It
AIM: To create a standard calculator


CODE

Form 1
Option Explicit
Dim res As Double, cleardisp As Boolean, first As Boolean
Dim operation As String, mem As Double
Private Sub Backspace_Click()
If lbldisplay.Caption <> "" Then
lbldisplay.Caption = Mid(lbldisplay.Caption, 1, Len(lbldisplay.Caption) - 1)
End If
End Sub
Private Sub ce_Click()
lbldisplay.Caption = "0"
first = True
End Sub
Private Sub digit_Click(index As Integer)
On Error GoTo error
If cleardisp Then
lbldisplay.Caption = ""
cleardisp = False
End If
lbldisplay.Caption = lbldisplay.Caption & digit(index).Caption
Exit Sub
error:
lbldisplay.Caption = "error!" & Err.Description
End Sub

Private Sub equal_Click()
On Error GoTo error
Select Case operation
Case "+"
res = Str(res + Val(lbldisplay.Caption))
Case "-"
res = Str(res - Val(lbldisplay.Caption))
Case "*"
res = Str(res * Val(lbldisplay.Caption))
Case "/"
res = Str(res / Val(lbldisplay.Caption))
End Select
lbldisplay.Caption = Str(res)
first = True
Exit Sub
error:
lbldisplay.Caption = "error!" & Err.Description
End Sub
Private Sub Form_Load()
cleardisp = True
first = True
lbldisplay.Caption = "0"
End Sub
Private Sub off_Click()
End
End Sub
Private Sub operator_Click(index As Integer)
On Error GoTo error
If first Then
res = lbldisplay.Caption
operation = operator(index).Caption
cleardisp = True
first = False
Exit Sub
End If

Select Case operation
Case "+"
res = Str(res + Val(lbldisplay.Caption))
Case "-"
res = Str(res - Val(lbldisplay.Caption))
Case "*"
res = Str(res * Val(lbldisplay.Caption))
Case "/"
res = Str(res / Val(lbldisplay.Caption))
End Select
operation = operator(index).Caption
lbldisplay.Caption = Str(res)
cleardisp = True
Exit Sub
error:
lbldisplay.Caption = "error!" & Err.Description
End Sub
Private Sub PLUS_MINUS_Click()
On Error GoTo error
lbldisplay.Caption = Str(-1 * Val(lbldisplay.Caption))
first = True
cleardisp = True
Exit Sub
error:
lbldisplay.Caption = "error......!!" & Err.Description
End Sub
Private Sub sqrt_Click()
lbldisplay.Caption = Sqr(lbldisplay.Caption)
first = True
cleardisp = True
End Sub


0 comments:

Post a Comment