Nama : Muhammad Ruqiyaddin
Sekolah : Smk Bina Insan Mulia
Jurusan : Informatika - Rekayasa Perangkat Lunak (RPL)
Ini adalah kalkulator yang saya buat dari hasil pembelajaran Pemerograman Berbasis Desktop,
Kalkulator ini masih sangat sederhana hanya bisa mengoperasikan pertambahan, pengurangan, perkalian
dan pembagian. Walaupun sangat sederhana namun kalkulator ini lumayan sulit membuat nya
karena sebelum membuat kalkulator seperti ini, kita harus mengerti dulu logika nya...
dan begini lah coding nya :
Dim Data1, Data2, Operator, Hasil
Dim HapusScreen As Boolean
Dim TitikDesimal As Boolean
Sub Persiapan()
Operator = ""
Data1 = ""
Data2 = ""
Hasil = ""
HapusScreen = False
TitikDesimal = True
Screen.Text = ""
End Sub
Private Sub Form_Load()
Call Persiapan
Frame1.Caption = ""
Frame2.Caption = ""
Frame3.Caption = ""
End Sub
Sub Evaluasi(Tanda)
If Data1 = "" And Operator = "" Then
If Hasil = "" Then
Data1 = Screen.Text
Else
Data1 = Format(Hasil)
Screen.Text = Data1
End If
ElseIf Data1 <> "" And Operator <> "" And Data2 <> "" Then
Menghitung
Screen.Text = Format(Hasil)
Data1 = Format(Hasil)
Data2 = ""
End If
Operator = Tanda
HapusScreen = False
TitikDesimal = True
Screen.Text = Screen.Text + Tanda
End Sub
Sub HapusData()
Call Persiapan
End Sub
Sub Menghitung()
HapusScreen = True
On Error GoTo ErrorHandler
A = Val(Data1)
B = Val(Data2)
Select Case Operator
Case "+": Hasil = A + B
Case "-": Hasil = A - B
Case "*": Hasil = A * B
Case "/": Hasil = A / B
End Select
Data1 = ""
Data2 = ""
Operator = ""
Screen.Text = Format(Hasil)
Exit Sub
ErrorHandler:
MsgBox vbCrLf & Err.Description, , _
"Calculator mendeteksi ERROR sbb :"
End Sub
Private Sub Angka_Click(Index As Integer)
If HapusScreen Then
Call HapusData
End If
If Operator = "" Then
Data1 = Data1 + Angka(Index).Caption
Else
Data2 = Data2 + Angka(Index).Caption
End If
Screen.Text = Screen.Text + Angka(Index).Caption
End Sub
Private Sub Bagi_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("/")
End Sub
Private Sub Kali_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("*")
End Sub
Private Sub Kurang_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("-")
End Sub
Private Sub Tambah_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("+")
End Sub
Private Sub Hapus_Click()
Call HapusData
End Sub
Private Sub SamaDengan_Click()
If Data1 <> "" And Operator <> "" And Data2 <> "" Then
Call Menghitung
End If
End Sub
Dim HapusScreen As Boolean
Dim TitikDesimal As Boolean
Sub Persiapan()
Operator = ""
Data1 = ""
Data2 = ""
Hasil = ""
HapusScreen = False
TitikDesimal = True
Screen.Text = ""
End Sub
Private Sub Form_Load()
Call Persiapan
Frame1.Caption = ""
Frame2.Caption = ""
Frame3.Caption = ""
End Sub
Sub Evaluasi(Tanda)
If Data1 = "" And Operator = "" Then
If Hasil = "" Then
Data1 = Screen.Text
Else
Data1 = Format(Hasil)
Screen.Text = Data1
End If
ElseIf Data1 <> "" And Operator <> "" And Data2 <> "" Then
Menghitung
Screen.Text = Format(Hasil)
Data1 = Format(Hasil)
Data2 = ""
End If
Operator = Tanda
HapusScreen = False
TitikDesimal = True
Screen.Text = Screen.Text + Tanda
End Sub
Sub HapusData()
Call Persiapan
End Sub
Sub Menghitung()
HapusScreen = True
On Error GoTo ErrorHandler
A = Val(Data1)
B = Val(Data2)
Select Case Operator
Case "+": Hasil = A + B
Case "-": Hasil = A - B
Case "*": Hasil = A * B
Case "/": Hasil = A / B
End Select
Data1 = ""
Data2 = ""
Operator = ""
Screen.Text = Format(Hasil)
Exit Sub
ErrorHandler:
MsgBox vbCrLf & Err.Description, , _
"Calculator mendeteksi ERROR sbb :"
End Sub
Private Sub Angka_Click(Index As Integer)
If HapusScreen Then
Call HapusData
End If
If Operator = "" Then
Data1 = Data1 + Angka(Index).Caption
Else
Data2 = Data2 + Angka(Index).Caption
End If
Screen.Text = Screen.Text + Angka(Index).Caption
End Sub
Private Sub Bagi_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("/")
End Sub
Private Sub Kali_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("*")
End Sub
Private Sub Kurang_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("-")
End Sub
Private Sub Tambah_Click()
If Data1 <> "" Or Hasil <> "" Then Call Evaluasi("+")
End Sub
Private Sub Hapus_Click()
Call HapusData
End Sub
Private Sub SamaDengan_Click()
If Data1 <> "" And Operator <> "" And Data2 <> "" Then
Call Menghitung
End If
End Sub
Tidak ada komentar:
Posting Komentar