TUTORIAL PROGRAM KALKULATOR
1.
Masukkan tools yang dibutuhkan seperti
button dan textbox seperti gambar di bawah ini :
2.
Masukkan listing di bawah ini untuk
mendeklarasikan variabel-variabel dan konstanta yang dipakai :
Public Class Form1
Dim Angka, Hasil As Double
Dim rumus As String
Dim a, b As Integer
Const pi As Double = 3.14159265358979
3.
Lalu
masukkan listing untuk button yang digunakan
·
Button
1 (untuk menuliskan angka 1)
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal
e As System.EventArgs)
Handles Button1.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "1"
TextBox2.Text = "1"
Else
TextBox1.Text = TextBox1.Text & "1"
TextBox2.Text = TextBox2.Text & "1"
End If
End Sub
·
Button
2 (untuk menuliskan angka 2)
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal
e As System.EventArgs)
Handles Button2.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "2"
TextBox2.Text = "2"
Else
TextBox1.Text = TextBox1.Text & "2"
TextBox2.Text = TextBox2.Text & "2"
End If
End Sub
·
Button
3 (untuk menuliskan angka 3)
Private Sub
Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button3.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "3"
TextBox2.Text = "3"
Else
TextBox1.Text = TextBox1.Text & "3"
TextBox2.Text = TextBox2.Text & "3"
End If
End Sub
·
Button
4 (untuk menuliskan angka 4)
Private Sub
Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button4.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "4"
TextBox2.Text = "4"
Else
TextBox1.Text = TextBox1.Text & "4"
TextBox2.Text = TextBox2.Text & "4"
End If
End Sub
·
Button
5 (untuk menuliskan angka 5)
Private Sub
Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button5.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "5"
TextBox2.Text = "5"
Else
TextBox1.Text = TextBox1.Text & "5"
TextBox2.Text = TextBox2.Text & "5"
End If
End Sub
·
Button
6 (untuk menuliskan angka 6)
Private Sub
Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button6.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "6"
TextBox2.Text = "6"
Else
TextBox1.Text = TextBox1.Text & "6"
TextBox2.Text = TextBox2.Text & "6"
End If
End Sub
·
Button
7 (untuk menuliskan angka 7)
Private Sub
Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button7.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "7"
TextBox2.Text = "7"
Else
TextBox1.Text = TextBox1.Text & "7"
TextBox2.Text = TextBox2.Text & "7"
End If
End Sub
·
Button
8 (untuk menuliskan angka 8)
Private Sub
Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button8.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "8"
TextBox2.Text = "8"
Else
TextBox1.Text = TextBox1.Text & "8"
TextBox2.Text = TextBox2.Text & "8"
End If
End Sub
·
Button
9 (untuk menuliskan angka 9)
Private Sub
Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button9.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "9"
TextBox2.Text = "9"
Else
TextBox1.Text = TextBox1.Text & "9"
TextBox2.Text = TextBox2.Text & "9"
End If
End Sub
·
Button
10 (untuk menuliskan angka 0)
Private Sub
Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button10.Click
If TextBox1.Text = "0"
Then
TextBox1.Text = "0"
TextBox2.Text = "0"
Else
TextBox1.Text = TextBox1.Text & "0"
TextBox2.Text = TextBox2.Text & "0"
End If
End Sub
·
Button
11 (untuk menuliskan titik)
Private Sub
Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button11.Click
TextBox1.Text = TextBox1.Text & "."
TextBox2.Text = TextBox2.Text & "."
End Sub
Untuk
proses aritmatika menggunakan listing case
dan call yang berguna untuk perhitungan paralel :
·
Button
12 (untuk penjumlahan)
Private Sub
Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button12.Click
TextBox2.Text = TextBox2.Text & "+"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^1/2″"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call Tambah()
End If
rumus
= "+"
TextBox1.Text = ""
End Sub
Private Sub Tambah()
If Angka = "0"
Then
Angka = Val(TextBox1.Text)
Else
Angka = Angka + Val(TextBox1.Text)
End If
rumus
= "+"
TextBox1.Text = ""
End Sub
·
Button
13 (untuk pengurangan)
Private Sub
Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button13.Click
TextBox2.Text = TextBox2.Text & "-"
If Rumus <> ""
Then
Select Case Rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "÷"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "√"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call kurang()
End If
rumus
= "-"
TextBox1.Text = ""
End Sub
Private Sub kurang()
If Angka = "0"
Then
Angka = Val(TextBox1.Text)
Else
Angka = Angka - Val(TextBox1.Text)
End If
rumus
= "-"
TextBox1.Text = ""
End Sub
·
Button
14 (untuk perkalian)
Private Sub
Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button14.Click
TextBox2.Text = TextBox2.Text & "x"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call kali()
End If
rumus = "x"
TextBox1.Text = ""
End Sub
Private Sub kali()
If Angka = "0"
Then
Angka = Val(TextBox1.Text)
Else
Angka = Angka * Val(TextBox1.Text)
End If
rumus
= "x"
TextBox1.Text = ""
End Sub
·
Button
17 (untuk pembagian)
Private Sub
Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button17.Click
TextBox2.Text = TextBox2.Text & "÷"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^1/2″"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call bagi()
End If
rumus
= "÷"
TextBox1.Text = ""
End Sub
Private Sub bagi()
If Angka = "0"
Then
Angka = Val(TextBox1.Text)
Else
Angka = Angka / Val(TextBox1.Text)
End If
rumus
= "/"
TextBox1.Text = ""
End Sub
·
Button
18 (untuk perpangkatan)
Private Sub
Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button18.Click
TextBox2.Text = TextBox2.Text & "^"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
End Select
Else
Call pangkat()
End If
rumus
= "^"
TextBox1.Text = ""
End Sub
Private Sub pangkat()
If Angka = "0"
Then
Angka = Val(TextBox1.Text)
Else
Angka = Angka ^ Val(TextBox1.Text)
End If
rumus
= "^"
TextBox1.Text = ""
End Sub
·
Button
19 (untuk pengakaran)
Private Sub
Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button19.Click
TextBox2.Text = "√" &
TextBox2.Text
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case
"x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
End Select
Else
Call akar()
End If
rumus
= "√"
TextBox1.Text = ""
End Sub
Private Sub akar()
Angka
= Val(TextBox1.Text)
Angka
= Angka ^ 0.5
rumus
= "√"
TextBox1.Text = ""
End Sub
·
Button
20 (untuk menghitung log)
Private Sub
Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button20.Click
TextBox2.Text = TextBox2.Text & "log"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call
faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
End Select
Else
Call Log()
End If
rumus
= "log"
End Sub
Private Sub Log()
Angka
= Val(TextBox1.Text)
Angka
= Math.Log10(Angka)
rumus
= "log"
TextBox1.Text = ""
End Sub
·
Button
21 (untuk menghitung faktorial)
Private Sub
Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button21.Click
TextBox2.Text = TextBox2.Text & "!"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call faktorial()
End If
rumus
= "N!"
End Sub
Private Sub
faktorial()
a = 1
For i = 1 To
Val(TextBox1.Text)
a
= a * i
Next
End Sub
·
Button
22 (untuk menghitung nilai sin)
Private Sub
Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button22.Click
TextBox2.Text = "Sin" &
TextBox2.Text
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call
Tan()
Case "mod"
Call m()
End Select
Else
Call Sin()
End If
rumus
= "Sin"
TextBox1.Text = ""
End Sub
Private Sub Sin()
Angka
= Val(TextBox1.Text)
Angka
= Math.Sin(Angka * (pi / 180))
rumus
= "Sin"
TextBox1.Text = ""
End Sub
·
Button
23 (untuk menghitung nilai cos)
Private Sub
Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button23.Click
TextBox2.Text = "Cos" &
TextBox2.Text
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call Cos()
End If
rumus
= "Cos"
End Sub
Private Sub Cos()
Angka
= Val(TextBox1.Text)
Angka
= Math.Cos(Angka * (pi / 180))
rumus
= "Cos"
TextBox1.Text = ""
End Sub
·
Button
24 (untuk menghitung nilai tan)
Private Sub
Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button24.Click
TextBox2.Text = TextBox2.Text & "Tan"
If rumus <> ""
Then
Select Case rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "/"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "^0.5"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call Tan()
End If
rumus
= "Tan"
TextBox1.Text = ""
End Sub
Private Sub Tan()
Angka
= Val(TextBox1.Text)
Angka
= Math.Tan(Angka * (pi / 180))
rumus
= "Tan"
TextBox1.Text = ""
End Sub
·
Button
25 (untuk menghapus angka di layar)
Private Sub
Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button25.Click
TextBox2.Text = ""
TextBox1.Text = "0"
End Sub
·
Button
16 (untuk memproses perhitungan)
Private Sub
Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button16.Click
If rumus = "+"
Then
Hasil = Angka + Val(TextBox1.Text)
TextBox1.Text = Hasil
ElseIf rumus = "-"
Then
Hasil = Angka - Val(TextBox1.Text)
TextBox1.Text = Hasil
ElseIf rumus = "x"
Then
Hasil = Angka * Val(TextBox1.Text)
TextBox1.Text = Hasil
ElseIf rumus = "/"
Then
Hasil = Angka / Val(TextBox1.Text)
TextBox1.Text = Hasil
ElseIf rumus = "^"
Then
Hasil = Angka ^ Val(TextBox1.Text)
TextBox1.Text = Hasil
ElseIf rumus = "√"
Then
Hasil = Val(TextBox1.Text) ^ 0.5
TextBox1.Text = Hasil
ElseIf rumus = "Cos"
Then
Hasil = Math.Cos(Val(TextBox1.Text) *
(pi / 180))
TextBox1.Text = Hasil
ElseIf rumus = "Sin"
Then
Hasil = Math.Sin(Val(TextBox1.Text) *
(pi / 180))
TextBox1.Text = Hasil
ElseIf rumus = "Tan"
Then
Hasil = Math.Tan(Val(TextBox1.Text) *
(pi / 180))
TextBox1.Text = Hasil
ElseIf rumus = "N!"
Then
TextBox1.Text = Str(a)
ElseIf rumus = "log"
Then
Hasil = Math.Log10(Val(TextBox1.Text))
TextBox1.Text = Hasil
ElseIf rumus = "mod"
Then
Hasil = Angka Mod Val(TextBox1.Text)
TextBox1.Text = Hasil
End If
rumus
= ""
Angka
= 0
End Sub
·
Button
26 (untuk menghitung sisa hasil bagi)
Private Sub
Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button26.Click
TextBox2.Text = TextBox2.Text & "mod"
If Rumus <> ""
Then
Select Case Rumus
Case "+"
Call Tambah()
Case "-"
Call kurang()
Case "x"
Call kali()
Case "÷"
Call bagi()
Case "^"
Call pangkat()
Case "log"
Call Log()
Case "√"
Call akar()
Case "N!"
Call faktorial()
Case "Sin"
Call Sin()
Case "Cos"
Call
Cos()
Case "Tan"
Call Tan()
Case "mod"
Call m()
End Select
Else
Call m()
End If
End Sub
Private Sub m()
Angka
= Val(TextBox1.Text)
If Angka = Val(TextBox1.Text) Then
Else
Angka = Angka Mod Val(TextBox1.Text)
End If
rumus
= "mod"
TextBox1.Text = ""
End Sub
·
Button
15 (untuk proses penghitungan menggunakan nilai minus)
Private Sub
Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button15.Click
TextBox1.Text = TextBox1.Text * -1
TextBox2.Text = TextBox2.Text * -1
End Sub
End Class
KOMPONEN-KOMPONEN TOOLS
KOMPONEN
|
PROPERTIES
|
KETERANGAN
|
Form1
|
Name
Text
|
Form1
|
Label1
|
Name
Text
|
Label1
|
Label2
|
Name
Text
|
Label2
|
Button1
|
Name
Text
|
Button1
1
|
Button2
|
Name
Text
|
Button2
2
|
Button3
|
Name
Text
|
Button3
3
|
Button4
|
Name
Text
|
Button4
4
|
Button5
|
Name
Text
|
Button5
5
|
Button6
|
Name
Text
|
Button6
6
|
Button7
|
Name
Text
|
Button7
7
|
Button8
|
Name
Text
|
Button8
8
|
Button9
|
Name
Text
|
Button9
9
|
Button10
|
Name
Text
|
Button10
10
|
Button11
|
Name
Text
|
Button11
.
|
Button12
|
Name
Text
|
Button12
+
|
Button13
|
Name
Text
|
Button13
-
|
Button14
|
Name
Text
|
Button14
X
|
Button15
|
Name
Text
|
Button15
+/-
|
Button16
|
Name
Text
|
Button16
=
|
Button17
|
Name
Text
|
Button17
÷
|
Button18
|
Name
Text
|
Button18
^
|
Button19
|
Name
Text
|
Button19
√
|
Button20
|
Name
Text
|
Button20
Log
|
Button21
|
Name
Text
|
Button21
N!
|
Button22
|
Name
Text
|
Button22
Sin
|
Button23
|
Name
Text
|
Button23
Cos
|
Button24
|
Name
Text
|
Button24
Tan
|
Button25
|
Name
Text
|
Button25
AC
|
Button26
|
Name
Text
|
Button26
mod
|
Tidak ada komentar:
Posting Komentar