Option Explicit
Dim lastinput As String
Dim NumOp As Integer
Dim PreOp As String
Dim DeFlag
Dim Op1
Dim Op2
Private Sub cmd_Percent_Click()
Dim var As Single
var = Val(lblOut.Caption)
lblOut.Caption = Str(var)
End Sub
Private Sub cmd0_Click()
number (0)
End Sub
Private Sub cmd1_Click()
number (1)
End Sub
Private Sub cmd2_Click()
number (2)
End Sub
Private Sub cmd3_Click()
number (3)
End Sub
Private Sub cmd4_Click()
number (4)
End Sub
Private Sub cmd5_Click()
number (5)
End Sub
Private Sub cmd6_Click()
number (6)
End Sub
Private Sub cmd7_Click()
number (7)
End Sub
Private Sub cmd8_Click()
number (8)
End Sub
Private Sub cmd9_Click()
number (9)
End Sub
Private Sub cmdC_Click()
Form_Load
End Sub
Private Sub cmdCE_Click()
lblOut.Caption = "0."
DeFlag = False
lastinput = "icon"
End Sub
Private Sub cmdDecimal_Click()
If lastinput = "negative" Then
lblOut.Caption = "-0."
ElseIf lastinput <> "number" Then
lblOut.Caption = "0."
End If
DeFlag = True
lastinput = "number"
End Sub
Private Sub cmdDiv_Click()
If lastinput = "number" Then
NumOp = NumOp + 1
End If
If NumOp = 1 Then
Op1 = lblOut.Caption
ElseIf NumOp = 2 Then
Op2 = lblOut.Caption
calculation
End If
If lastinput <> "negative" Then
lastinput = "icon"
PreOp = "/"
End If
End Sub
Private Sub cmdEqual_Click()
If lastinput = "number" Then
NumOp = NumOp + 1
End If
If NumOp = 1 Then
Op1 = lblOut.Caption
ElseIf NumOp = 2 Then
Op2 = lblOut.Caption
calculation
End If
If lastinput <> "negative" Then
lastinput = "icon"
PreOp = "="
End If
End Sub
Private Sub cmdMinus_Click()
If lastinput = "number" Then
NumOp = NumOp + 1
End If
If NumOp = 0 Then
If lastinput <> "negative" Then
lblOut.Caption = "-" + lblOut.Caption
lastinput = "negative"
End If
ElseIf NumOp + 2 Then
Op2 = lblOut.Caption
calculation
End If
If lastinput <> "negative" Then
lastinput = "icon"
PreOp = "-"
End If
End Sub
Private Sub cmdMul_Click()
If lastinput = "number" Then
NumOp = NumOp + 1
End If
If NumOp = 1 Then
Op1 = lblOut.Caption
ElseIf NumOp = 2 Then
Op2 = lblOut.Caption
calculation
End If
If lastinput <> "negative" Then
lastinput = "icon"
PreOp = "x"
End If
End Sub
Private Sub cmdPlus_Click()
If lastinput = "number" Then
NumOp = NumOp + 1
End If
If NumOp = 1 Then
Op1 = lblOut.Caption
ElseIf NumOp = 2 Then
Op2 = lblOut.Caption
calculation
End If
If lastinput <> "negative" Then
lastinput = "icon"
PreOp = "-"
End If
End Sub
Private Sub Form_Load()
lblOut.Caption = "0"
DeFlag = False
NumOp = 0
lastinput = "No"
PreOp = " "
Op1 = 0
Op2 = 0
End Sub
Private Sub number(num As Integer)
Dim temp As String
If lastinput <> "number" Then
lblOut.Caption = "."
DeFlag = False
End If
If DeFlag Then
lblOut.Caption = lblOut.Caption + Str(num)
Else
temp = lblOut.Caption
temp = Left(temp, Len(temp) - 1)
lblOut.Caption = temp + Str(num) + "."
End If
If lastinput = "negative" Then
lblOut.Caption = "-" + lblOut.Caption
End If
lastinput = "number"
End Sub
Private Sub calculation()
Select Case PreOp
Case "+"
Op1 = Val(Op1) + Val(Op2)
Case "-"
Op1 = Val(Op1) - Val(Op2)
Case "x"
Op1 = Val(Op1) * Val(Op2)
Case "/"
If Val(Op2) = 0 Then
MsgBox "Sorry,zero can't be divided", vbCritical, "Calculator"
Else
Op1 = Val(Op1) / Val(Op2)
End If
Case "="
Op1 = Op2
End Select
lblOut.Caption = Op1
NumOp = 1
End Sub
|