Contoh Program Matrix 2 Dimensi Delphi (Tambah, Kurang, Kali)

Dikesempatan kali ini Saya ingin membagikan source kode atau contoh program Delphi penjumlahan, pengurangan, dan perkalian Matrix, langsung saja, sobat bisa menyalin kode berikut dan sesuaikan form nya sesuai dengan tampilan berikut ini. 


Contoh Program Matrix 2 Dimensi Delphi (Tambah, Kurang, Kali)

Berikut ini source kode nya :

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    tabel1: TStringGrid;
    tabel2: TStringGrid;
    tabel3: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Edit4: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure bersih;
    procedure nol;
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i,j,k,l,m,n,o,p,z: integer;
  st:string;

implementation

{$R *.dfm}
procedure TForm1.bersih;
begin
for i := 0 to tabel1.colcount -1 do
begin
for j := 0 to tabel1.rowcount - 1 do
begin
  tabel1.cols[i].Clear;
  tabel1.rows[j].Clear;
end;
for i := 0 to tabel2.colcount -1 do
begin
for j := 0 to tabel2.rowcount - 1 do
begin
  tabel2.cols[i].Clear;
  tabel2.rows[j].Clear;
end;
end;
for i := 0 to tabel3.colcount -1 do
begin
for j := 0 to tabel3.rowcount - 1 do
begin
  tabel3.cols[i].Clear;
  tabel3.rows[j].Clear;
end;
end;

edit1.Text:='0';
edit2.Text:='0';
edit3.Text:='0';
edit4.Text:='0';

end;

end;

procedure Tform1.nol;
begin
  if edit1.Text = '' then
    edit1.Text :='0';
  if edit2.Text = '' then
    edit2.Text :='0';
  if edit3.Text = '' then
    edit3.Text :='0';
  if edit4.Text = '' then
    edit4.Text :='0';

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if (tabel1.RowCount = tabel2.rowcount)and(tabel1.ColCount = tabel2.ColCount) then
begin
tabel3.RowCount:= tabel1.RowCount;
tabel3.colCount:= tabel1.colCount;
for i := 0 to tabel1.colcount -1  do
begin
  for j := 0 to tabel1.rowcount -1 do
  begin
     tabel3.Cells[i,j]:= inttostr(strtoint(tabel1.Cells[i,j])+strtoint(tabel2.Cells[i,j]));
  end;
end;
end

else
showmessage('Untuk penjumlahan Matrix baris dan kolom pada Matrix A dan B harus sama');

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
z:=10;
  for i := 0 to strtoint(edit2.Text)-1 do
    begin
      for j := 0 to strtoint(edit1.Text)-1 do
        begin
          tabel1.Cells[i,j]:=inttostr(random(z));
        end;
    end;
  for i := 0 to strtoint(edit4.Text)-1 do
    begin
      for j := 0 to strtoint(edit3.Text)-1 do
        begin
          tabel2.Cells[i,j]:=inttostr(random(z));
        end;
    end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if (tabel1.RowCount = tabel2.rowcount)and(tabel1.ColCount = tabel2.ColCount) then
begin
tabel3.RowCount:= tabel1.RowCount;
tabel3.colCount:= tabel1.colCount;
for i := 0 to tabel1.colcount-1  do
begin
  for j := 0 to tabel1.rowcount-1 do
  begin
     tabel3.Cells[i,j]:= inttostr(strtoint(tabel1.Cells[i,j])-strtoint(tabel2.Cells[i,j]));
  end;
end;
end

else
showmessage('Untuk pengurangan Matrix baris dan kolom pada Matrix A dan B harus sama');

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
bersih;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
if (tabel1.colCount = tabel2.rowcount) then
begin
tabel3.RowCount:= tabel1.RowCount;
tabel3.colCount:= tabel2.colCount;
for i := 0 to tabel1.rowcount-1  do
begin
  for j := 0 to tabel2.colcount-1 do
  begin
  o:=0;
  st:='';
    for k:= 0 to tabel1.ColCount-1 do
    begin
      m:=strtoint(tabel1.Cells[k,i]);
      n:=strtoint(tabel2.Cells[j,k]);
      o:=(m*n)+o;



    end;
    tabel3.Cells[j,i]:=inttostr(o);
  end;
end;
end

else

showmessage('Untuk perkalian Matrix kolom pada Matrix A dan baris pada Matrix B harus sama');

end;

procedure TForm1.Button6Click(Sender: TObject);
begin
halt;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
nol;
tabel1.RowCount:= strtoint(edit1.Text);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
nol;
tabel1.colCount:= strtoint(edit2.Text);
end;

procedure TForm1.Edit3Change(Sender: TObject);
begin
nol;
tabel2.rowCount:= strtoint(edit3.Text);
end;

procedure TForm1.Edit4Change(Sender: TObject);
begin
nol;
tabel2.colCount:= strtoint(edit4.Text);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
tabel1.ColCount:=2;
tabel1.rowCount:=2;
tabel2.ColCount:=2;
tabel2.rowCount:=2;
tabel3.ColCount:=2;
tabel3.rowCount:=2;

end;

end.


Download source kode atau project lengkapnya disini :

Download



Demikianlah artikel ini Saya buat, semoga dapat bermanfaat.
Newest
Previous
Next Post »