Kali ini saya
akan mengajari Anda membuat sebuah Kalkulator Matematika ya mungkin agak
mirip dengan Casio. Langsung saja lihat hasil runningnya di bawah ini.
Kren kan, ya
iyalah. Itu pakai Skin makanya terlihat kren. Pertama, tentunya Anda
harus menyiapkan dan mengatur komponen seperti di atas, OK. Jika sudah
maka lanjutkan ke sintaksis. Anda tinggal perhatikan beberapa tombol dan
sintaksisnya. Kedua, perhatikan di bagian Uses.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Buttons, StdCtrls, ExtCtrls, Math, Mask, MPlayer, sSkinProvider, sSkinManager;
Ok, sudah lihat
kan ada perubahan. Ini karena kita akan menggunakan beberapa fungsi
Matematika. tentunya ini tidak ada di Delphi sebelumnya, jadi harus Anda
tambahkan dulu.
Selanjutnya, buatkan prosedure tersendiri di bagian type, yakni:
procedure BtsinClick(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure AktifkanTombol;
procedure CekInput;
procedure bt1Click(Sender: TObject);
procedure bt0Click(Sender: TObject);
procedure bt2Click(Sender: TObject);
procedure bt3Click(Sender: TObject);
procedure bt4Click(Sender: TObject);
procedure bt5Click(Sender: TObject);
procedure bt6Click(Sender: TObject);
procedure bt7Click(Sender: TObject);
procedure bt8Click(Sender: TObject);
procedure bt9Click(Sender: TObject);
procedure BtBaruClick(Sender: TObject);
procedure Btx2Click(Sender: TObject);
procedure BtakarClick(Sender: TObject);
procedure BtpangkatClick(Sender: TObject);
procedure bthasilClick(Sender: TObject);
procedure BtcosClick(Sender: TObject);
procedure BttanClick(Sender: TObject);
procedure BtexpClick(Sender: TObject);
procedure BtLnClick(Sender: TObject);
procedure Bt1perxClick(Sender: TObject);
procedure BtFaktorialClick(Sender: TObject);
procedure BtkaliClick(Sender: TObject);
procedure btbagiClick(Sender: TObject);
procedure bttambahClick(Sender: TObject);
procedure btkurangClick(Sender: TObject);
procedure btpersenClick(Sender: TObject);
procedure bttitikClick(Sender: TObject);
procedure btbulatClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
Lanjut lagi, perhatikan di bagian Var.
var
Form1: TForm1;
Hasil : Real; //untuk menampung hasil proses
Kosong : Boolean; //untuk pengecekan input data
NoBaru : Boolean; //untuk mengecek hasil proses baru
I,N : Integer; //untuk Variabel indek dalam program
Nah, ini dia sintaksis lengkapnya, perhatikan dengan seksama ya.
Procedure TForm1.AktifkanTombol;
Begin
btpangkat.Enabled:=True;
BtTitik.Enabled:=True;
end;
Procedure TForm1.CekInput;
Begin
AktifkanTombol;
if length(trim(edinput.Text))=0 then
Begin
application.MessageBox('Masukan Angka Dulu...','Cek Input',
MB_IconStop);
edInput.SetFocus;
Kosong:=True;
end
else
Begin
Kosong:=False;
exit;
end;
end;
//Tombol Tekan Angka
procedure TForm1.bt1Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='1';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'1';
end;
procedure TForm1.bt0Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='0';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'0';
end;
procedure TForm1.bt2Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='2';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'2';
end;
//Tombol Angka 3
procedure TForm1.bt3Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='3';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'3';
end;
//Tombol Angka 4
procedure TForm1.bt4Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='4';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'4';
end;
//Tombol Angka 5
procedure TForm1.bt5Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='5';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'5';
end;
//Tombol Angka 6
procedure TForm1.bt6Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='6';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'6';
end;
//Tombol Angka 7
procedure TForm1.bt7Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='7';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'7';
end;
//Tombol Angka 8
procedure TForm1.bt8Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='8';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'8';
end;
//Tombol Angka 9
procedure TForm1.bt9Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='9';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'9';
end;
procedure TForm1.Btx2Click(Sender: TObject);
var Kw:Real;
begin
CekInput;
if kosong=true then
exit
else
begin
Kw:=strToFloat(edinput.Text);
edinput.Text:=FloatToStr(sqr(Kw));
end;
end;
procedure TForm1.BtakarClick(Sender: TObject);
var aKw:Real;
begin
CekInput;
if kosong=true then
exit
else
begin
akw:=strToFloat(edinput.Text);
edinput.Text:=FloatToStr(sqrt(aKw));
end;
end;
procedure TForm1.BtpangkatClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='x^y';
edinput.Text:='';
edinput.SetFocus;
btpangkat.Enabled:=False;
BtTitik.Enabled:=False;
end;
end;
procedure TForm1.bthasilClick(Sender: TObject);
var x,n1,n2:integer;
Bil1,Bil2:Real;
begin
CekInput;
if kosong=true then
exit
else
begin
if length(trim(stCetak.Caption))=0 then exit;
bil1:=strToFloat(stCetak.Caption);
bil2:=strToFloat(EdInput.Text);
n1:=strToint(stCetak.Caption);
n2:=strToint(EdInput.Text);
if StOperasi.Caption='x^y' then
Begin
hasil:=1;
for x:=1 to strToInt(EdInput.Text) do
begin
Hasil:=Hasil*Bil1;
End;
End
else if StOperasi.Caption='*' Then
Hasil:=bil1*bil2
else if StOperasi.Caption='/' Then
Hasil:=bil1/bil2
else if StOperasi.Caption='+' Then
Hasil:=bil1+bil2
else if StOperasi.Caption='-' Then
Hasil:=bil1-bil2
else if StOperasi.Caption='Div' Then
Hasil:=n1 div n2
else if StOperasi.Caption='Mod' Then
Hasil:=n1 mod n2
else if StOperasi.Caption='nPr' Then
Hasil:=bil1+bil2
else if StOperasi.Caption='nCr' Then
Hasil:=bil1-bil2
else
exit;
aktifkanTombol;
StCetak.Caption:=FloatToStr(Bil2);
EdInput.Text:=FloatToStr(Hasil);
NoBaru:=True;
end;
end;
//Tombol Sinus
procedure TForm1.BtsinClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(Sin(Hasil*pi/180));
end;
end;
//Tombol Cos
procedure TForm1.BtcosClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(Cos(Hasil*pi/180));
end;
end;
//Tombol Tan
procedure TForm1.BttanClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(Tan(Hasil*pi/180));
end;
end;
procedure TForm1.BtexpClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(Exp(Hasil));
end;
end;
procedure TForm1.BtLnClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(Ln(Hasil));
end;
end;
procedure TForm1.Bt1perxClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(1/Hasil);
end;
end;
procedure TForm1.BtFaktorialClick(Sender: TObject);
var Hfaktorial:Integer;
begin
CekInput;
if kosong=true then
exit
else
begin
hasil:=1;
HFaktorial:=StrToInt(edinput.Text);
for i:=HFaktorial downto 1 do
begin
hasil:=hasil*i;
edinput.Text:=FloatToStr(Hasil);
end;
end;
end;
procedure TForm1.BtkaliClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='*';
edinput.Text:='';
edinput.SetFocus;
end;
end;
procedure TForm1.btbagiClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='/';
edinput.Text:='';
edinput.SetFocus;
end;
end;
procedure TForm1.bttambahClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='+';
edinput.Text:='';
edinput.SetFocus;
end;
end;
procedure TForm1.btkurangClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='-';
edinput.Text:='';
edinput.SetFocus;
end;
end;
procedure TForm1.btpersenClick(Sender: TObject);
Var Persen:Real;
begin
CekInput;
if kosong=true then
exit
else
begin
Persen:=StrToFloat(EdInput.Text);
EdInput.Text:=FloatToStr(Persen/100);
end;
end;
procedure TForm1.bttitikClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
BtTitik.Enabled:=False;
EdInput.Text:=EdInput.Text+',';
end;
end;
procedure TForm1.btbulatClick(Sender: TObject);
Var Minus:Real;
begin
CekInput;
if kosong=true then
exit
else
begin
Minus:=StrToFloat(EdInput.Text);
EdInput.Text:=FloatToStr(Minus*(0-1));
end;
end;
procedure TForm1.btCosecClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(1/Sin(Hasil*pi/180));
end;
end;
procedure TForm1.btHapusClick(Sender: TObject);
begin
NoBaru:=False;
aktifkanTombol;
StOperasi.Caption:='';
edinput.Text:='';
stcetak.Caption:='';
edinput.SetFocus;
end;
procedure TForm1.btSecClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(1/Cos(Hasil*pi/180));
end;
end;
procedure TForm1.btCotClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
Hasil:=StrToFloat(edinput.Text);
edinput.Text:=floatToStr(1/Tan(Hasil*pi/180));
end;
end;
procedure TForm1.bt000Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='0';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'000';
end;
procedure TForm1.btDivClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='Div';
edinput.Text:='';
edinput.SetFocus;
end;
end;
procedure TForm1.btModClick(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='Mod';
edinput.Text:='';
edinput.SetFocus;
end;
end;
procedure TForm1.bt00Click(Sender: TObject);
begin
if NoBaru=True Then
begin
edinput.Text:='0';
NoBaru:=False;
end
else
edinput.Text:=Edinput.Text+'00';
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
CekInput;
if kosong=true then
exit
else
begin
stcetak.Caption:=edinput.Text;
stOperasi.Caption:='nPr';
edinput.Text:='';
edinput.SetFocus;
btpangkat.Enabled:=False;
BtTitik.Enabled:=False;
end;
end;
klik disini
Tidak ada komentar:
Posting Komentar