VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



CALCULATOR

by IRTIZA (1 Submission)
Category: Math/Dates
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Wed 26th October 2005
Date Added: Mon 8th February 2021
Rating: (2 Votes)

CALCULATOR

Rate CALCULATOR



Sub textvalue(num As Long)
    On Error GoTo OvrFlowError
    If (Not WhichNo) Then
        If (Dot) Then
            DotVal = DotVal + 1
            temp = num
            For i = 1 To DotVal
                temp = (temp / 10)
            Next
            no1 = no1 + temp
        Else
            temp = no1 * 10
            no1 = temp + num
        End If
        Text1.Text = no1
    Else
        If (Dot) Then
            DotVal = DotVal + 1
            temp = num
            For i = 1 To DotVal
                temp = (temp / 10)
            Next
            no2 = no2 + temp
        Else
            temp = no2 * 10
            no2 = temp + num
        End If
        Text1.Text = no2
    End If
    txtNO1.Text = no1
    txtNO2.Text = no2
    txtOP.Text = op
    txtANS.Text = answer
    Exit Sub
OvrFlowError:
    MsgBox "Overflow occured!" & Chr(13) & "Please restart your job.", vbExclamation, "Error - Ameya's Calculator"
End Sub
Sub calc()
    On Error GoTo aritherror
    Select Case op
    Case "+"
        answer = (no1 + no2)
    Case "-"
        answer = (no1 - no2)
    Case "*"
        answer = (no1 * no2)
    Case "/"
        answer = (no1 / no2)
    Case "%"
        'answer = (no1 % no2)
    Case "&"
        answer = (no1 & no2)
    Case "|"
        answer = (no1 & no2)
    Case "~"
        answer = (Not no1)
    Case "sin"
        answer = (Sin(no1))
    Case "cos"
        answer = (Cos(no1))
    Case "tan"
        answer = (Tan(no1))
    Case "cosec"
        answer = (1 / Sin(no1))
    Case "sec"
        answer = (1 / Cos(no1))
    Case "cot"
        answer = (1 / Tan(no1))
    Case "ln"
        answer = (Log(no1))
    Case "log"
        answer = (Log(no1) / 2.30258509299405)
    Case "^"
        answer = (no1 ^ no2)
    Case "!"
        answer = 1
        For i = 2 To no1
            answer = answer * i
        Next
    End Select
    txtOP.Text = ""
    WhichNo = False
    Text1.Text = answer
    txtNO1.Text = no1
    txtNO2.Text = no2
    txtOP.Text = op
    txtANS.Text = answer
    no1 = answer
    Exit Sub
aritherror:
    MsgBox "Arithmetic error occured!. Possibly Overflow." & Chr(13) & "Please restart your job.", vbExclamation, "Error - Ameya's Calculator"
End Sub
Private Sub error(errorno As Long)
    Select Case errorno
    Case 1
        MsgBox "Divide by zero error!"
    Case 2
        MsgBox "Operator Overflow!"
    End Select
End Sub

Private Sub about_Click()
    frmAbout.Show
End Sub
Private Sub btn1_Click()
    textvalue (1)
End Sub

Private Sub btn2_Click()
    textvalue (2)
End Sub
Private Sub btn3_Click()
    textvalue (3)
End Sub
Private Sub btn4_Click()
    textvalue (4)
End Sub
Private Sub btn5_Click()
    textvalue (5)
End Sub
Private Sub btn6_Click()
    textvalue (6)
End Sub
Private Sub btn7_Click()
    textvalue (7)
End Sub
Private Sub btn8_Click()
    textvalue (8)
End Sub
Private Sub btn9_Click()
    textvalue (9)
End Sub
Private Sub btn0_Click()
    textvalue (0)
End Sub
Private Sub btnADD_Click()
    op = "+"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
    no2 = 0
End Sub
Private Sub btnSUB_Click()
    op = "-"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
    no2 = 0
End Sub
Private Sub btnMUL_Click()
    op = "*"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
    no2 = 0
End Sub
Private Sub btnDIV_Click()
    op = "/"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
    no2 = 0
End Sub
Private Sub btnAND_Click()
    op = "&"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
End Sub
Private Sub btnOR_Click()
    op = "|"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
End Sub
Private Sub btnMOD_Click()
    op = "%"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
End Sub
Private Sub btnNOT_Click()
    op = "~"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnSIN_Click()
    op = ""
    If (chkINV.Value = 1) Then
        op = "a"
    End If
    op = op & "sin"
    If (chkHYP.Value = 1) Then
        op = op & "h"
    End If
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnCOS_Click()
    op = ""
    If (chkINV.Value = 1) Then
        op = "a"
    End If
    op = op & "cos"
    If (chkHYP.Value = 1) Then
        op = op & "h"
    End If
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnTAN_Click()
    op = ""
    If (chkINV.Value = 1) Then
        op = "a"
    End If
    op = op & "tan"
    If (chkHYP.Value = 1) Then
        op = op & "h"
    End If
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnCOSEC_Click()
    op = ""
    If (chkINV.Value = 1) Then
        op = "a"
    End If
    op = op & "cosec"
    If (chkHYP.Value = 1) Then
        op = op & "h"
    End If
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnSEC_Click()
    op = ""
    If (chkINV.Value = 1) Then
        op = "a"
    End If
    op = op & "sec"
    If (chkHYP.Value = 1) Then
        op = op & "h"
    End If
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnCOT_Click()
    op = ""
    If (chkINV.Value = 1) Then
        op = "a"
    End If
    op = op & "cot"
    If (chkHYP.Value = 1) Then
        op = op & "h"
    End If
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnLN_Click()
    If (no1 <= 0) Then
        MsgBox ("logarithm is only defined for positive numbers." & Chr(13) & "Please enter a valid no and then take logarithm.")
    Else
        op = "ln"
        Text1.Text = ""
        DotVal = 0
        Dot = False
        calc
    End If
End Sub
Private Sub btnLOG_Click()
    If (no1 <= 0) Then
        MsgBox ("logarithm is only defined for positive numbers." & Chr(13) & "Please enter a valid no and then take logarithm.")
    Else
        op = "log"
        Text1.Text = ""
        DotVal = 0
        Dot = False
        calc
    End If
End Sub
Private Sub btnPOW_Click()
    op = "^"
    WhichNo = True
    Text1.Text = ""
    DotVal = 0
    Dot = False
End Sub
Private Sub btnPOW2_Click()
    op = "^"
    no2 = 2
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnPOW3_Click()
    op = "^"
    no2 = 3
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnINV_Click()
    op = "^"
    no2 = -1
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnFACTORIAL_Click()
    op = "!"
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnSQRT_Click()
    If (no1 < 0) Then
        MsgBox ("Square Root is not defined for negative numbers.")
    Else
        op = "^"
        no2 = 0.5
        Text1.Text = ""
        DotVal = 0
        Dot = False
        calc
    End If
End Sub
Private Sub btnEXP_Click()
    op = "^"
    no2 = no1
    no1 = 2.30258509299405
    Text1.Text = ""
    DotVal = 0
    Dot = False
    calc
End Sub
Private Sub btnPI_Click()
    If (Not WhichNo) Then
        no1 = 3.14159265358979
    Else
        no2 = 3.14159265358979
    End If
    Text1.Text = "3.14159265358979"
    DotVal = 0
    Dot = False
End Sub
Private Sub btnC_Click()
    no1 = 0
    no2 = 0
    answer = 0
    Text1.Text = "0"
    op = ""
    DotVal = 0
    Dot = False
    WhichNo = False
    txtNO1.Text = no1
    txtNO2.Text = no2
    txtOP.Text = op
    txtANS.Text = answer
End Sub
Private Sub btnCE_Click()
    If (temp) Then
        no1 = 0
    Else
        no2 = 0
    End If
    Text1.Text = ""
    DotVal = 0
    Dot = False
End Sub
Private Sub btnbksp_Click()
    If (Not WhichNo) Then
        If (Len(Str(no1)) > 1) Then
            no1 = FormatNumber(Left(Str(no1), Len(Text1.Text) - 1))
            Text1.Text = no1
        End If
    Else
        If (Len(Str(no2)) > 0) Then
            no2 = FormatNumber(Left(Str(no2), Len(Text1.Text) - 1))
            Text1.Text = no2
        End If
    End If
    If (DotVal > 0) Then
        DotVal = ditval - 1
    End If
    txtNO1.Text = no1
    txtNO2.Text = no2
    txtOP.Text = op
    txtANS.Text = answer
End Sub
Private Sub btnDOT_Click()
    If (Dot = False) Then
        Dot = True
        Text1.Text = Text1.Text & "."
        DotVal = 0
    End If
End Sub
Private Sub btnEqual_Click()
    calc
    no1 = answer
    WhichNo = False
    DotVal = 0
    Dot = False
End Sub
Private Sub Command2_Click()
    MsgBox ("Temp=" & Str(temp) & " No1=" & Str(no1) & " No2=" & Str(no2) & " op=" & op)
End Sub
Private Sub copy_Click()
    Clipboard.SetText (Text1.Text)
End Sub
Private Sub cut_Click()
    Clipboard.SetText (Text1.Text)
    Text1.Text = ""
End Sub
Private Sub exit_Click()
    Unload Me
End Sub
Private Sub Form_Load()
    Dim no1, no2, op, WhichNo, Dot, temp
    no1 = 0
    no2 = 0
    answer = 0
    Dot = False
    DotVal = 0
    WhichNo = False
    btnC_Click
End Sub

Private Sub helptopic_Click()
        MsgBox ("No help found." & Chr(13) & "We are SORRY for the inconvenience!")
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    If (IsNumeric(Text1.Text)) Then
        If (Not WhichNo) Then
            no1 = FormatNumber(Text1.Text)
        Else
            no2 = FormatNumber(Text1.Text)
        End If
    End If
    txtNO1.Text = no1
    txtNO2.Text = no2
    txtOP.Text = op
    txtANS.Text = answer
End Sub


Download this snippet    Add to My Saved Code

CALCULATOR Comments

No comments have been posted about CALCULATOR. Why not be the first to post a comment about CALCULATOR.

Post your comment

Subject:
Message:
0/1000 characters