Minggu, 05 Januari 2014

“Calculator” dengan Delphi 2010

Calculator Delphi

FYI: Ternyata banyak sekali blog yang memposting pembuatan aplikasi Calculator ini, baik yang sederhana maupun scientific. Walaupun saya belum mencoba satu persatu, tetapi saya yakin aplikasi yang teman-teman blog postingkan pasti berjalan dengan sempurna. Dan saya juga ingin mengucapkan terima kasih karena dengan masukan dari blog teman-teman, akhirnya aplikasi ini bisa saya selesaikan sesuai dengan keinginan. Dan mohon maaf sekali lagi karena saya tidak menuliskan sumber blog yang menjadi acuan pembuatan aplikasi ini. Dikarenakan informasi yang saya terapkan pada pembuatan aplikasi tidak semua berasal dari blog-blog tersebut. Kerena sebelumnya aplikasi serupa sudah pernah saya buat pada postingan saya sebelumnya. Walaupun menggunakan bahasa pemrograman lain akan tetapi logika pemrograman aplikasi ini sudah saya kuasai. Sehingga saya hanya butuh adaptasi dengan perintah-perintah code’nya saja.

1. Jalankan terlebih dahulu aplikasi Delphi 2010

2. Pada jendela “Welcome Page” pilih  “New Project…

New Project Delphi

3. Pada jendela “New Items” pilih “Delphi projects” lalu pilih “VCL Forms Application” kemudian pilih “OK

New Items Delphi

4. Berikut seting kontrol properti yang terdapat pada aplikasi ini:

LayOut Calculator Delphi

Object Name Properties Setting
TForm1 Form1 Border Style
Caption
Position
bsSingle
Simple Calculator
poScreenCenter
TEdit edtDisplay Alignment
Font
Text
taRightJustify
Tahoma; 12
(Empty)
TButton btnOne Caption 1
TButton btnTwo Caption 2
TButton btnThree Caption 3
TButton btnFour Caption 4
TButton btnFive Caption 5
TButton btnSix Caption 6
TButton btnSeven Caption 7
TButton btnEight Caption 8
TButton btnNine Caption 9
TButton btnZero Caption 0
TButton btnPlusMinus Caption +/-
TButton btnComa Caption ,
TButton btnAdd Caption +
TButton btnSubtract Caption -
TButton btnMultiple Caption *
TButton btnDevide Caption /
TButton btnResult Caption =
TButton btnDiv Caption 1/x
TButton btnPercent Caption %
TButton btnSqrt Caption sqrt
TButton btnClear Caption C
TButton btnCE Caption CE
TButton btnBackspace Caption Backspace
TButton btnOff Caption Off
TButton btnSin Caption Sin
TButton btnCos Caption Cos
TButton btnTan Caption Tan
TButton btnRank Caption x^2

5. Berikut event program aplikasi ini:

unit untCalculator;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    edtDisplay: TEdit;
    btnOne: TButton;
    btnTwo: TButton;
    btnThree: TButton;
    btnFour: TButton;
    btnFive: TButton;
    btnSix: TButton;
    btnSeven: TButton;
    btnEight: TButton;
    btnNine: TButton;
    btnZero: TButton;
    btnClear: TButton;
    btnAdd: TButton;
    btnSubtract: TButton;
    btnMultiple: TButton;
    btnDevide: TButton;
    btnResult: TButton;
    btnPlusMinus: TButton;
    btnComa: TButton;
    btnSqrt: TButton;
    btnPercent: TButton;
    btnCE: TButton;
    btnBackspace: TButton;
    btnSin: TButton;
    btnCos: TButton;
    btnTan: TButton;
    btnRank: TButton;
    btnOff: TButton;
    procedure btnOneClick(Sender: TObject);
    procedure btnTwoClick(Sender: TObject);
    procedure btnThreeClick(Sender: TObject);
    procedure btnFourClick(Sender: TObject);
    procedure btnFiveClick(Sender: TObject);
    procedure btnSixClick(Sender: TObject);
    procedure btnSevenClick(Sender: TObject);
    procedure btnEightClick(Sender: TObject);
    procedure btnNineClick(Sender: TObject);
    procedure btnZeroClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure edtDisplayKeyPress(Sender: TObject; var Key: Char);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnAddClick(Sender: TObject);
    procedure btnSubtractClick(Sender: TObject);
    procedure btnMultipleClick(Sender: TObject);
    procedure btnDevideClick(Sender: TObject);
    procedure btnResultClick(Sender: TObject);
    procedure btnPlusMinusClick(Sender: TObject);
    procedure btnComaClick(Sender: TObject);
    procedure btnSqrtClick(Sender: TObject);
    procedure btnDivClick(Sender: TObject);
    procedure btnPercentClick(Sender: TObject);
    procedure btnOffClick(Sender: TObject);
    procedure btnSinClick(Sender: TObject);
    procedure btnCosClick(Sender: TObject);
    procedure btnTanClick(Sender: TObject);
    procedure btnRankClick(Sender: TObject);
    procedure btnBackspaceClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  PointAdd, PointSubtract, PointMultiple, PointDevide, Result: Double;
  Coice: string;

implementation

{$R *.dfm}

procedure TForm1.btnAddClick(Sender: TObject);
begin
  PointAdd:=StrToFloat(edtDisplay.Text);
  edtDisplay.Clear;
  Coice:='Add';
end;

procedure TForm1.btnBackspaceClick(Sender: TObject);
var
  Character: Integer;
begin
  Character:=Length(edtDisplay.Text);
  edtDisplay.Text:=Copy(edtDisplay.Text,1,Character-1);
end;

procedure TForm1.btnClearClick(Sender: TObject);
begin
  edtDisplay.Clear;
end;

procedure TForm1.btnComaClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+',';
end;

procedure TForm1.btnCosClick(Sender: TObject);
begin
  Result:=StrToFloat(edtDisplay.Text);
  Result:=Result*pi/180;
  edtDisplay.Text:=FloatToStr(Cos(Result));
end;

procedure TForm1.btnDevideClick(Sender: TObject);
begin
  PointDevide:=StrToFloat(edtDisplay.Text);
  edtDisplay.Clear;
  Coice:='Devide';
end;

procedure TForm1.btnDivClick(Sender: TObject);
begin
  Result:=1 / StrToFloat(edtDisplay.Text);
  edtDisplay.Text:=FloatToStr(Result);
end;

procedure TForm1.btnEightClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnEight.Caption;
end;

procedure TForm1.btnFiveClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnFive.Caption;
end;

procedure TForm1.btnFourClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnFour.Caption;
end;

procedure TForm1.btnMultipleClick(Sender: TObject);
begin
  PointMultiple:=StrToFloat(edtDisplay.Text);
  edtDisplay.Clear;
  Coice:='Multiple';
end;

procedure TForm1.btnNineClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnNine.Caption;
end;

procedure TForm1.btnOffClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.btnOneClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnOne.Caption;
end;

procedure TForm1.btnPercentClick(Sender: TObject);
begin
  Result:=StrToFloat(edtDisplay.Text);
  Result:=Result/100;
  edtDisplay.Text:=FloatToStr(Result);
end;

procedure TForm1.btnPlusMinusClick(Sender: TObject);
begin
  edtDisplay.Text:=FloatToStr(-StrToFloat(edtDisplay.Text));
end;

procedure TForm1.btnRankClick(Sender: TObject);
begin
  Result:=StrToFloat(edtDisplay.Text);
  Result:=Sqr(Result);
  edtDisplay.Text:=FloatToStr(Result);
end;

procedure TForm1.btnResultClick(Sender: TObject);
begin
  if Coice='Add' then
  begin
    Result:=PointAdd+StrToFloat(edtDisplay.Text);
    edtDisplay.Text:=FloatToStr(Result);
  end
  else if Coice='Subtract' then
  begin
    Result:=PointSubtract-StrToFloat(edtDisplay.Text);
    edtDisplay.Text:=FloatToStr(Result);
  end
  else if Coice='Multiple' then
  begin
    Result:=PointMultiple*StrToFloat(edtDisplay.Text);
    edtDisplay.Text:=FloatToStr(Result);
  end
  else if Coice='Devide' then
  begin
    Result:=PointDevide / StrToFloat(edtDisplay.Text);
    edtDisplay.Text:=FloatToStr(Result);
  end;
end;

procedure TForm1.btnSevenClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnSeven.Caption;
end;

procedure TForm1.btnSinClick(Sender: TObject);
begin
  Result:=StrToFloat(edtDisplay.Text);
  Result:=Result*pi/180;
  edtDisplay.Text:=FloatToStr(Sin(Result));
end;

procedure TForm1.btnSixClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnSix.Caption;
end;

procedure TForm1.btnSqrtClick(Sender: TObject);
begin
  edtDisplay.Text:=FloatToStr(sqrt(StrToFloat(edtDisplay.Text)));
end;

procedure TForm1.btnSubtractClick(Sender: TObject);
begin
  PointSubtract:=StrToFloat(edtDisplay.Text);
  edtDisplay.Clear;
  Coice:='Subtract';
end;

procedure TForm1.btnTanClick(Sender: TObject);
var
  Temp1, Temp2: Double;
begin
  Result:=StrToFloat(edtDisplay.Text);
  Result:=Result*pi/180;
  Temp1:=Sin(Result);
  Temp2:=Cos(Result);
  Result:=Temp1/Temp2;
  edtDisplay.Text:=FloatToStr(Result);
end;

procedure TForm1.btnThreeClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnThree.Caption;
end;

procedure TForm1.btnTwoClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnTwo.Caption;
end;

procedure TForm1.btnZeroClick(Sender: TObject);
begin
  edtDisplay.Text:=edtDisplay.Text+btnZero.Caption;
end;

procedure TForm1.edtDisplayKeyPress(Sender: TObject; var Key: Char);
begin
  if (key in ['0'..'9',#8,'-','.']) then inherited
  else key:=#0;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  case MessageDlg('Exit program',mtConfirmation,[mbYes, mbNo],0) of
    mrOk: Close;
    mrNo: Action:=caNone;
  end;
end;

end.

6. Pilih tombol “Run” atau teman-teman bisa langsung menekan tombol “F9” pada keyboard

Run Delphi

Label:

1 Komentar:

Pada 16 Mei 2019 pukul 21.15 , Blogger Lia mengatakan...

kok button CE nya gak berfungsi yaa

 

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda