Pages

Monday, 9 July 2012

Calculator

Scientific Calculator-updated

-> As name suggests, Scientific calculator is much more complex than normal arithmetic calculator.

-> I tried to cover some important part of Scientific calculator in this project.

-> This is what i have done.

Dim memory As Double
Dim operation As String, disp As String
Dim startpos As Double
Dim var As Double
Dim pos1 As Double, x As Double
Dim chache As Double

Private Sub Cmd7_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "7"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "7"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmd0_Click()
Text1.Text = Text1.Text & "0"
Text1.DataField = 1
End Sub

Private Sub Cmd00_Click()
Text1.Text = Text1.Text & "00"
Text1.DataField = 1
End Sub
Private Sub Cmddot_Click()
If Text1.DataField = 0 Then
    Text1.Text = "0."
    Text1.SelStart = Len(Text1.Text)
    Text1.SetFocus
    Text1.DataField = 1
Else
    If Text1.Text = "0." Then GoTo hell
        Text1.Text = Text1.Text & "."
        Text1.DataField = 1
hell:
End If
End Sub

Private Sub Cmdsin_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        Text1.Text = ""
        Text1.SelText = "Sin("
        Text1.SelText = memory
        Text1.SelText = ")"
        Text2.Text = Math.Sin(memory)
        Text2.DataField = 1
    End If
End Sub
Private Sub Cmdcos_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        Text1.Text = ""
        Text1.SelText = "Cos("
        Text1.SelText = memory
        Text1.SelText = ")"
        Text2.Text = Math.Cos(memory)
        Text2.DataField = 1
    End If
End Sub

Private Sub Cmdtan_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        Text1.Text = ""
        Text1.SelText = "Tan("
        Text1.SelText = memory
        Text1.SelText = ")"
        Text2.Text = Math.Tan(memory)
        Text2.DataField = 1
    End If
End Sub

Private Sub Cmddiv_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        operation = "/"
        Text1.SelStart = Len(Text1.Text)
        Text1.SelText = "/ "
        startpos = Len(Text1.Text)
        Text1.SetFocus
    End If
End Sub

Private Sub Cmdsub_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        operation = "-"
        Text1.SelStart = Len(Text1.Text)
        Text1.SelText = "- "
        startpos = Len(Text1.Text)
        Text1.SetFocus
    End If
End Sub

Private Sub Cmdmul_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        operation = "*"
        Text1.SelStart = Len(Text1.Text)
        Text1.SelText = "* "
        startpos = Len(Text1.Text)
        Text1.SetFocus
    End If
End Sub
Private Sub Cmdadd_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.DataField = 1
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        operation = "+"
        Text1.SelStart = Len(Text1.Text)
        Text1.SelText = "+ "
        startpos = Len(Text1.Text)
        Text1.SetFocus
    End If
End Sub

Private Sub Cmd8_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "8"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "8"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmdequal_Click()
If Text1.DataField = 0 Then
    MsgBox "Enter number first", _
    vbOKOnly, _
    "Syntax Error"
    Text1.SetFocus
Else
pos1 = Len(Text1.Text)
x = Val(pos1) - Val(startpos)
var = Right(Text1.Text, x)
If operation = "+" Then
    Text2.Text = memory + var
    Text2.DataField = 1
    ElseIf operation = "-" Then
        Text2.Text = memory - var
        Text2.DataField = 1
        ElseIf operation = "*" Then
            Text2.Text = memory * var
            Text2.DataField = 1
            ElseIf operation = "/" Then
                Text2.Text = memory / var
                Text2.DataField = 1
End If
End If
Text2.SelStart = Len(Text2.Text)
End Sub


Private Sub Cmdinvert_Click()
If Text2.DataField = 0 Then
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        If Text1.Text < 0 Then
            Text1.Text = Text1.Text * -1
        Else
            Text1.Text = "-" & Text1.Text
        End If
    End If
Else
    If Text2.Text < 0 Then
        Text1.Text = Text2.Text * -1
        Text2.Text = Text1.Text
        Text2.DataField = 1
    Else
        Text1.Text = "-" & Text2.Text
        Text2.Text = Text1.Text
        Text2.DataField = 1
    End If
End If
End Sub

Private Sub Cmdclr_Click()
Cls
memory = 0
operation = ""
Text1.Text = ""
Text2.Text = ""
Text2.DataField = 0
Text1.DataField = 0
Text1.SetFocus
End Sub

Private Sub Cmdexit_Click()
End
End Sub

Private Sub Cmdrec_Click()
If Text2.DataField = 0 Then GoTo 1
    Text1.Text = Text2.Text
1:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        Text1.Text = ""
        Text1.SelText = "1/"
        Text1.SelText = memory
        Text2.Text = 1 / memory
        Text2.DataField = 1
    End If
End Sub

Private Sub Cmdlog_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        memory = Text1.Text
        Text1.Text = ""
        Text1.SelText = "Log("
        Text1.SelText = memory
        Text1.SelText = ")"
        Text2.Text = Math.Log(memory) / 2.30258509299405
        Text2.DataField = 1
    End If
End Sub

Private Sub Cmdsqr_Click()
If Text2.DataField = 0 Then GoTo 10
    Text1.Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        Text2.Text = Text1.Text * Text1.Text
        Text2.DataField = 1
    End If
End Sub

Private Sub Cmdweb_Click()
Dim web As String
web = "http://www.afzalali15.blogspot.com"
MsgBox "Calculator version: 2.0.0" & vbCrLf & "Copyright © 2012-Afzal", _
vbOKOnly + vbQuestion, _
"About Calculator"
Call Shell("explorer.exe " & web, vbNormalFocus)
End Sub

Private Sub Cmd9_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "9"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "9"
    Text1.DataField = 1
End If
End Sub
Private Sub Cmd4_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "4"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "4"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmd5_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "5"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "5"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmd6_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "6"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "6"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmd1_Click()
    If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "1"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "1"
    Text1.DataField = 1
End If
End Sub
Private Sub Cmdsqrt_Click()
With Text1
If Text2.DataField = 0 Then GoTo 10
    .Text = Text2.Text
10:
    If Text1.DataField = 0 Then
        MsgBox "Enter number first", _
        vbOKOnly, _
        "Syntax Error"
        Text1.SetFocus
    Else
        If .Text < 0 Then GoTo 11
            memory = .Text
            .Text = ""
            .SelText = "Sqrt("
            .SelText = memory
            .SelText = ")"
            Text2.Text = Math.Sqr(memory)
            Text2.DataField = 1
    End If
End With
GoTo 12
11:
MsgBox "Negative number!", _
vbOKOnly, _
"Error"
12:
End Sub

Private Sub Cmd2_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "2"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "2"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmd3_Click()
If Text2.DataField = 0 Then
    Text1.Text = Text1.Text & "3"
    Text1.DataField = 1
Else
    Text1.SelStart = Len(Text1.Text)
    Text1.Text = Text1.Text & "3"
    Text1.DataField = 1
End If
End Sub

Private Sub Cmdans_Click()
If Text2.DataField = 0 Then GoTo hell
    Text1.Text = Text2.Text
hell:
End Sub

Private Sub Form_Load()
Text1.DataField = 0
Text2.DataField = 0
End Sub

Snapshot of Scientific Calculator.exe

Download source code

(4.2KB)

1 comment:

  1. Any query related to this topic can be discussed over here..
    If u find any error in Calc then please share..

    ReplyDelete