TUTORIAL
PROGRAM KALKULATOR
Salam semangat !! Kali ini, kami kelompok 31
praktikan Teknologi Informatika dari Jurusan Teknik Industri Universitas
Diponegoro akan menjelaskan cara membuat program kalkulator dengan menggunakan
Visual Basic 2010.
Program
ini hanya menggunakan 1 form.
Public Class
Form1
Dim
Angka, Hasil As Double
Dim
proses As String
Dim a,
b, i As Integer
Const
pi As Double =
3.14159265358979
LISTING
DIATAS BERTUJUAN UNTUK MENDEKLARASIKAN ANGKA DAN HASIL SEBAGAI TIPE DATA
DOUBLE, PROSES SEBAGAI TIPE DATA STRING, a, b DAN i SEBAGAI TIPE DATA INTEGER
DAN pi SEBAGAI TIPE DATA DOUBLE YANG BERNILAI 3.14159265358979
Private
Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
Me.Text
= "KALKULATOR"
TextBox1.Enabled = False
TextBox2.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
Button10.Enabled = False
Button12.Enabled = False
Button14.Enabled = False
Button15.Enabled = False
Button16.Enabled = False
Button17.Enabled = False
Button18.Enabled = False
Button19.Enabled = False
Button20.Enabled = False
Button21.Enabled = False
Button22.Enabled = False
Button24.Enabled = False
Button25.Enabled = False
Button26.Enabled = False
Button27.Enabled = False
Button28.Enabled = False
Button29.Enabled = False
End Sub
LISTUNG
INI BERTUJUAN UNTUK MENONAKTIFKAN SEMUA TOMBOL KECUALI TOMBOL ON DAN OFF KETIKA
PROGRAM INI PERTAMA KALI DIBUKA.
Private Sub
Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
If
Label1.Text = "0" Then
Label1.Text = "1"
TextBox2.Text = "1"
Else
Label1.Text = Label1.Text &
"1"
TextBox2.Text = TextBox2.Text
& "1"
End
If
End Sub
Private Sub
Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
If
Label1.Text = "0" Then
Label1.Text = "2"
TextBox2.Text = "2"
Else
Label1.Text = Label1.Text &
"2"
TextBox2.Text = TextBox2.Text
& "2"
End
If
End Sub
Private Sub
Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button3.Click
If
Label1.Text = "0" Then
Label1.Text = "3"
TextBox2.Text = "3"
Else
Label1.Text = Label1.Text &
"3"
TextBox2.Text = TextBox2.Text
& "3"
End
If
End Sub
Private Sub
Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button4.Click
If
Label1.Text = "0" Then
Label1.Text = "4"
TextBox2.Text = "4"
Else
Label1.Text = Label1.Text &
"4"
TextBox2.Text = TextBox2.Text
& "4"
End
If
End Sub
Private Sub
Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button5.Click
If
Label1.Text = "0" Then
Label1.Text = "5"
TextBox2.Text = "5"
Else
Label1.Text = Label1.Text &
"5"
TextBox2.Text = TextBox2.Text & "5"
End
If
End Sub
Private Sub
Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button6.Click
If
Label1.Text = "0" Then
Label1.Text = "6"
TextBox2.Text = "6"
Else
Label1.Text = Label1.Text &
"6"
TextBox2.Text = TextBox2.Text
& "6"
End
If
End Sub
Private Sub
Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button7.Click
If
Label1.Text = "0" Then
Label1.Text = "7"
TextBox2.Text = "7"
Else
Label1.Text = Label1.Text &
"7"
TextBox2.Text = TextBox2.Text
& "7"
End
If
End Sub
Private Sub
Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button8.Click
If
Label1.Text = "0" Then
Label1.Text = "8"
TextBox2.Text = "8"
Else
Label1.Text = Label1.Text &
"8"
TextBox2.Text = TextBox2.Text
& "8"
End
If
End Sub
Private Sub
Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button9.Click
If
Label1.Text = "0" Then
Label1.Text = "9"
TextBox2.Text = "9"
Else
Label1.Text = Label1.Text &
"9"
TextBox2.Text = TextBox2.Text
& "9"
End
If
End Sub
Private Sub
Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button10.Click
If
Label1.Text = "0" Then
Label1.Text = "0"
TextBox2.Text = "0"
Else
Label1.Text = Label1.Text &
"0"
TextBox2.Text = TextBox2.Text
& "0"
End
If
End Sub
Private Sub
Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button12.Click
Label1.Text = Label1.Text & "."
TextBox2.Text = TextBox2.Text &
"."
End Sub
LISTING
DIATAS BERFUNGSI AGAR KETIKA BUTTON DIKLIK MAKA PADA TEXTBOX2 AKAN MUNCUL
TULISAN YANG SESUAI DENGAN APA YANG TERTERA PADA BUTTON
Private Sub
Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button14.Click
If
proses = "+" Then
Hasil = Angka +
Val(Label1.Text)
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "-" Then
Hasil = Angka -
Val(Label1.Text)
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "x" Then
Hasil = Angka *
Val(Label1.Text)
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "/" Then
Hasil = Angka /
Val(Label1.Text)
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "^" Then
Hasil = Angka ^
Val(Label1.Text)
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "√" Then
Hasil = Val(Label1.Text) ^ 0.5
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "Cos" Then
Hasil = Math.Cos(Val(Label1.Text) * (pi / 180))
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf proses
= "Sin" Then
Hasil = Math.Sin(Val(Label1.Text) * (pi / 180))
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "Tan" Then
Hasil = Math.Tan(Val(Label1.Text) * (pi / 180))
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf
proses = "log10" Then
Hasil = Math.Log10(Val(Label1.Text))
TextBox1.Text = Hasil
Label1.Text = Hasil
ElseIf proses = "ln"
Then
Hasil = Math.Log(Val(Label1.Text))
TextBox1.Text = Hasil
Label1.Text = Hasil
End
If
proses = ""
Angka = 0
End Sub
LISTING
DIATAS BERFUNGSI UNTUK MEMANGGIL PERINTAH ARITMATIKA YANG DIMAKSUD, SEHINGGA
KEMUDIAN KELUAR HASIL PERHITUNGAN SAAT BUTTON (=) DITEKAN.
Private Sub
Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button16.Click
TextBox2.Text = TextBox2.Text &
"/"
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call
Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
bagi()
End
If
proses = "/"
Label1.Text = ""
End Sub
Private
Sub bagi()
If
Angka = "0" Then
Angka = Val(Label1.Text)
Else
Angka = Angka /
Val(Label1.Text)
End
If
proses = "/"
Label1.Text = ""
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA PEMBAGIAN.
Private Sub
Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button17.Click
TextBox2.Text = TextBox2.Text &
"-"
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
kurang()
End
If
proses = "-"
Label1.Text = ""
End Sub
Private
Sub kurang()
If
Angka = "0" Then
Angka = Val(Label1.Text)
Else
Angka = Angka -
Val(Label1.Text)
End If
proses = "-"
Label1.Text = ""
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA PENGURANGAN.
Private Sub
Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button18.Click
TextBox2.Text = TextBox2.Text &
"^"
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call
log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
pangkat()
End
If
proses = "^"
Label1.Text = ""
End Sub
Private
Sub pangkat()
If
Angka = "0" Then
Angka = Val(Label1.Text)
Else
Angka = Angka ^
Val(Label1.Text)
End
If
proses = "^"
Label1.Text = ""
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA PANGKAT.
Private Sub
Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button19.Click
TextBox2.Text = TextBox2.Text &
"x"
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call
kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call
log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
kali()
End If
proses = "x"
Label1.Text = ""
End Sub
Private
Sub kali()
If
Angka = "0" Then
Angka = Val(Label1.Text)
Else
Angka = Angka *
Val(Label1.Text)
End
If
proses = "x"
Label1.Text = ""
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA KALI.
Private Sub
Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button20.Click
TextBox2.Text = "log" & TextBox2.Text
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case "Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
log()
End
If
proses = "log10"
TextBox1.Text = ""
End Sub
Private
Sub log()
proses = "log10"
TextBox1.Text = ""
TextBox2.Text = "log" & Label1.Text
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA LOG.
Private Sub
Button28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button28.Click
TextBox2.Text = "√" & Label1.Text
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call
Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
akar()
End
If
proses = "√"
TextBox1.Text = ""
End Sub
Private
Sub akar()
proses = "√"
TextBox1.Text = ""
TextBox2.Text = "√" & Label1.Text
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA AKAR KUADRAT
Private Sub
Button27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button27.Click
TextBox2.Text = TextBox2.Text &
"!"
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case "!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End Select
Else
Call
faktorial()
End
If
TextBox1.Text = ""
proses = "!"
End Sub
Private
Sub faktorial()
a = 1
For
Me.i = 1 To
Val(Label1.Text)
a = a * i
Next
TextBox2.Text = Label1.Text & "!"
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA FAKTORIAL.
Private Sub
Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button24.Click
TextBox2.Text = "Sin" & TextBox2.Text
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case "log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
Sin()
End
If
proses = "Sin"
TextBox1.Text = ""
End Sub
Private
Sub Sin()
proses = "Sin"
TextBox1.Text = ""
TextBox2.Text = "Sin" & Label1.Text
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA SINUS.
Private Sub
Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button25.Click
TextBox2.Text = "Cos" & TextBox2.Text
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call
Ln()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
Cos()
End
If
proses = "Cos"
TextBox1.Text = ""
End Sub
Private
Sub Cos()
proses = "Cos"
TextBox1.Text = ""
TextBox2.Text = "Cos" & Label1.Text
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA COSINUS.
Private Sub
Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button26.Click
TextBox2.Text = "Tan" & TextBox2.Text
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call log()
Case
"ln"
Call Ln()
Case
"^0.5"
Call akar()
Case "!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
End
Select
Else
Call
Tan()
End
If
proses = "Tan"
TextBox1.Text = ""
End Sub
Private
Sub Tan()
proses = "Tan"
TextBox1.Text = ""
TextBox2.Text = "Tan" & Label1.Text
End Sub
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA TANGEN.
Private Sub
Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button22.Click
TextBox1.Text = "0"
TextBox2.Text = ""
Label1.Text = ""
Angka = 0
Hasil = 0
proses = ""
End Sub
LISTING
INI BERTUJUAN AGAR KETIKA DITEKAN BUTTON CLEAR MAKA TULISAN YANG ADA DI LAYAR
KALKULATOR DAPAT TERHAPUS SEMUA.
Private Sub
Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button21.Click
If
TextBox2.Text.Length <> 0 Then
TextBox2.Text =
TextBox2.Text.Remove(TextBox2.Text.Length - 1, 1)
TextBox1.Text =
TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
End
If
End Sub
LISTING
INI BERTUJUAN AGAR KETIKA DITEKAN BUTTON DEL MAKA TULISAN YANG ADA DI LAYAR
KALKULATOR DAPAT TERHAPUS SATU PERSATU.
Private Sub
Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button11.Click
TextBox2.Text = ""
TextBox1.Text = "0"
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Button5.Enabled = True
Button6.Enabled = True
Button7.Enabled = True
Button8.Enabled = True
Button9.Enabled = True
Button10.Enabled = True
Button12.Enabled = True
Button14.Enabled = True
Button15.Enabled = True
Button16.Enabled = True
Button17.Enabled = True
Button18.Enabled = True
Button19.Enabled = True
Button20.Enabled = True
Button21.Enabled = True
Button22.Enabled = True
Button24.Enabled = True
Button25.Enabled = True
Button26.Enabled = True
Button27.Enabled = True
Button28.Enabled = True
Button29.Enabled = True
End Sub
LISTING
INI BERTUJUAN AGAR SEMUA BUTTON KECUALI BUTTON ON DAN OFF MENJADI AKTIF.
Private Sub
Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button13.Click
TextBox2.Text = ""
TextBox1.Text = ""
TextBox2.Enabled = False
TextBox1.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
Button10.Enabled = False
Button12.Enabled = False
Button15.Enabled = False
Button16.Enabled = False
Button17.Enabled = False
Button18.Enabled = False
Button19.Enabled = False
Button20.Enabled = False
Button21.Enabled = False
Button22.Enabled = False
Button24.Enabled = False
Button25.Enabled = False
Button26.Enabled = False
Button27.Enabled = False
Button28.Enabled = False
Button29.Enabled = False
End Sub
LISTING
INI BERTUJUAN AGAR SEMUA BUTTON KECUALI BUTTON ON DAN OFF MENJADI TIDAK AKTIF.
Private Sub
Button29_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button29.Click
TextBox2.Text = "ln" & TextBox2.Text
If
proses <> "" Then
Select
Case proses
Case
"+"
Call tambah()
Case
"-"
Call
kurang()
Case
"x"
Call kali()
Case
"/"
Call bagi()
Case
"^"
Call pangkat()
Case
"log"
Call Ln()
Case
"ln"
Call log()
Case
"^0.5"
Call akar()
Case
"!"
Call faktorial()
Case
"Sin"
Call Sin()
Case
"Cos"
Call Cos()
Case
"Tan"
Call Tan()
End
Select
Else
Call
Ln()
End
If
proses = "ln"
TextBox1.Text = ""
End Sub
Private
Sub Ln()
proses = "ln"
TextBox1.Text = ""
TextBox2.Text = "ln" & Label1.Text
End Sub
End Class
LISTING
TERSEBUT BERTUJUAN AGAR PROGRAM DAPAT MELAKUKAN OPERASI ARITMATIKA (ln)
Tidak ada komentar:
Posting Komentar