Minggu, 29 April 2012

Pembuatan Kalkulator VB


PEMBUATAN KALKULATOR DENGAN
MICROSOFT VISUAL BASIC 6.0

Dalam pembuatan kalkulator science ini tidak jauh beda dengan pembuatan kalkulator biasa, di sini hanya ada tambahan untuk penghitungan fungsi trigonometri, akar pangkat, logaritma dan ln. Untuk form pembuatannya juga hampir sama dengan kalkulator biasa, bentuk formnya dapat dilihat pada gambar di bawah ini:






Pengaturan Properti:

Listing Kalkulator Scientific:

Dim ans, b, c, hasil, sudut As Double
Dim a As String

Dim i, X As Integer
Dim no1 As Double, cnt1 As Integer
Dim op As String, op1 As String
Private Sub Cmdangka_Click(Index As Integer)
  If Text1.Text = "0" Then
    Text1.Text = ""
    End If
    If X = 0 Then
    Text1.Text = ""
    X = X + 1
    End If
    Text1.Text = Text1.Text + Cmdangka(Index).Caption
    Text2.Text = Text2.Text + Cmdangka(Index).Caption
End Sub


Private Sub Cmdclear_Click()
Select Case Index
    Case 0
        cnt1 = 0
        X = 0
        Text1.Text = ""
        Text2.Text = ""
        Text1.Visible = True
    Case 1
        Text2.Text = Mid(Text2.Text, 1, Len(Text2.Text) - 1)
    End Select
End Sub

Private Sub Cmdkoma_Click()
Text1.Text = Text1.Text + "."
Text2.Text = Text2.Text + "."
End Sub

Private Sub Cmdoff_Click()
Text1.Visible = False
Text2.Visible = False
End Sub

Private Sub Cmdon_Click()
Text1.Visible = True
Text2.Visible = True
End Sub

Private Sub CmdOp_Click(Index As Integer)
X = 0
    If cnt1 > 0 Then
        Select Case op
            Case "+"
                    ans = no1 + Text1.Text
                    Text1.Text = ans
                    no1 = Text1.Text
                    op = CmdOp(Index).Caption
            Case "-"
                    ans = no1 - Text1.Text
                    Text1.Text = ans
                    no1 = Text1.Text
                    op = CmdOp(Index).Caption
            Case "/"
                    ans = no1 / Text1.Text
                    Text1.Text = ans
                    no1 = Text1.Text
                    op = CmdOp(Index).Caption
            Case "*"
                    ans = no1 * Val(Text1.Text)
                    Text1.Text = ans
                    no1 = Text1.Text
                    op = CmdOp(Index).Caption
            Case "^"
                    ans = no1 ^ Text1.Text
                    Text1.Text = ans
                    no1 = Text1.Text
                    op = CmdOp(Index).Caption
        End Select

    Else
    no1 = Val(Text1.Text)
    op = CmdOp(Index).Caption
    cnt1 = cnt1 + 1
    End If
Text2.Text = Text2.Text + CmdOp(Index).Caption
End Sub

Private Sub Cmdplusminus_Click()
Text1.Text = Text1.Text * (-1)
Text2.Text = Text2.Text * (-1)
End Sub

Private Sub CmdSmdgn_Click()
cnt1 = 0
    Select Case op
        Case "+"
                ans = Text1.Text + no1
                If cnt = 0 Then
                  no1 = Text1.Text
                    cnt = cnt + 1
                End If
                Text1.Text = ans
             Case "-"
                If cnt > 0 Then
                  Text1.Text = no1
                  no1 = ans
                End If
                 ans = no1 - Text1.Text
                 no1 = Text1.Text
                 Text1.Text = ans
                cnt = cnt + 1
        Case "*"
                ans = no1 * Text1.Text
                If cnt = 0 Then
                  no1 = Text1.Text
                    cnt = cnt + 1
                End If
        Text1.Text = ans
        Case "/"
                If cnt > 0 Then
                  Text1.Text = no1
                  no1 = ans
                End If
                If Text1.Text = "0" Then
                i = MsgBox("Divide by zero error", vbExclamation +                                      vbOKOnly, "ERROR")
                Else
                ans = no1 / Text1.Text
                 no1 = Text1.Text
                Text1.Text = ans
                cnt = cnt + 1
                End If
 Case "^"
                ans = no1 ^ Text1.Text
                Text1.Text = ans
    End Select
Text1.Text = Text1.Text
Text1.Visible = True
End Sub

Private Sub Command22_Click(Index As Integer)
Dim s1, s2, s3 As String
op = "lain"
s = Val(Text1.Text)
    Select Case Index
        Case 1
            Text2.Text = s & "^(2)"
            Text1.Text = s ^ 2
        Case 2
           Text2.Text = "1/" & s
            If s = 0 Then
                Text1.Text = "            eror"
            ElseIf s <> 0 Then
                Text1.Text = 1 / s
            End If
        Case 3
            Text2.Text = "sqr(" & s & ")"
            Text1.Text = Sqr(s)
        Case 4
           Text2.Text = s & "^(3)"
            Text1.Text = s ^ 3
Case 5
            Text2.Text = "10^" & Text2.Text
            Text1.Text = 10 ^ (Val(Text1.Text))
        Case 6
            Text2.Text = "log(" & s & ")"
             If Val(Text1.Text) > 0 Then
                Text1.Text = Str((Log(Val(Text1.Text)) / Log(10)))
             Else
                Text1.Text = "error"
             End If  
        Case 7
           Text2.Text = "ln" & Text2.Text
           If Val(Text1.Text) > 0 Then
                Text1.Text = Str(Log(Val(Text1.Text)))
            Else
                Text1.Text = "error"
             End If
        Case 8
            Dim n, f, k As Integer
            n = Text1.Text
            f = 1
            k = 1
            Text2.Text = Text2.Text & "!"
            If (n < 0) Then
                Text1.Text = "salah input"
            ElseIf (n = 0) Then
                Text1.Text = 1
            Else
                Do While k <= n
                    f = f * k
                    k = k + 1
                Loop
            Text1.Text = Str(f)
            End If     
    End Select
End Sub

Private Sub Command27_Click(Index As Integer)
s = Val(Text1.Text)
sudut = 180 / 3.14159265358979
Dim a, b, c As Single
    Select Case Index
    Case 0
        Text2.Text = "sin (" & Text2.Text & ")"
        Text1.Text = Sin(s / sudut)
    Case 1
        Text2.Text = "cos (" & Text2.Text & ")"
        If s = 90 Or s = 270 Then
            Text1.Text = 0
        Else
            Text1.Text = Cos(s / sudut)
        End If
    Case 2
        If s = 90 Or s = 270 Then
            Text1.Text = "error"
        Else
            a = Sin(s / sudut)
            b = Cos(s / sudut)
            c = a / b
            Text1.Text = c
        End If
        Text2.Text = "tan (" & Text2.Text & ")"
       
    Case 3
        Text2.Text = "arc tan (" & Text2.Text & ")"
        Text1.Text = Str((Atn(Val(Text1.Text)) / (3.14159265358979 / 180)))
End Select
End Sub

Setelah program selesai dibuat, maka program tersebut dapat diformat ke dalam bentuk exe (executable file) sehingga program dapat dijalankan pada windows tanpa harus membuka program visual basic.
Ø  Klik menu file - make Project1.exe