Membuat Operasi Matriks
Konsep : Array dan StringGrid
Membuat array satu dimensi
x:array[1..n] of tipedata
keterangan : array yang terdiri dari 1 baris n kolom
contoh : 
x:array[1..6] of integer
1 
 | 
2 
 | 
3 
 | 
4 
 | 
5 
 | 
6 
 | 
Y:array[0..5] of integer
0 
 | 
1 
 | 
2 
 | 
3 
 | 
4 
 | 
5 
 | 
Membuat array dua dimensi 
Syntax : x:array[1..n,1..m] of tipedata
Keterangan : array yang terdiri dari n baris m kolom
Contoh :
X:array[1..4,1..4] of integer
1,1 
 | 
1,2 
 | 
1,3 
 | 
1,4 
 | 
2,1 
 | 
2,2 
 | 
2,3 
 | 
2,4 
 | 
3,1 
 | 
3,2 
 | 
3,3 
 | 
3,4 
 | 
4,1 
 | 
4,2 
 | 
4,3 
 | 
4,4 
 | 
Y: array[0..3,1..5] of integer
0,1 
 | 
0,2 
 | 
0,3 
 | 
0,4 
 | 
0,5 
 | 
1,1 
 | 
1,2 
 | 
1,3 
 | 
1,4 
 | 
1,5 
 | 
2,1 
 | 
2,2 
 | 
2,3 
 | 
2,4 
 | 
2,5 
 | 
3,1 
 | 
3,2 
 | 
3,3 
 | 
3,4 
 | 
3,5 
 | 
STRINGGRID
Stringgrid1.cells[kolom,baris];
0,0 
 | 
0,1 
 | 
0,2 
 | 
0,3 
 | 
1,0 
 | 
1,1 
 | 
1,2 
 | 
1,3 
 | 
2,0 
 | 
2,1 
 | 
2,2 
 | 
2,3 
 | 
3,0 
 | 
3,1 
 | 
3,2 
 | 
3,3 
 | 
Stringgrid1.cells[2,3];
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls, XPMan;
type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Button1: TButton;
    Button2: TButton;
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
    GroupBox2: TGroupBox;
    Button3: TButton;
    Button4: TButton;
    StringGrid3: TStringGrid;
    Label5: TLabel;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    XPManifest1: TXPManifest;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var a, b : integer;
begin
label8.Visible:=false;
if (strtoint(edit1.Text)>100) or (strtoint(edit2.Text)>100) or (strtoint(edit3.Text)>100) or (strtoint(edit4.Text)>100) then
begin
ShowMessage('Jumlah Baris atau Kolom melebihi dari batas program');
edit1.SetFocus;
end;
if (strtoint(edit1.Text)<=100) and (strtoint(edit2.Text)<=100) and (strtoint(edit3.Text)<=100) and (strtoint(edit4.Text)<=100) then
begin
stringgrid1.RowCount:=strtoint(edit1.Text)+1;
stringgrid1.ColCount:=strtoint(edit2.Text)+1;
stringgrid2.RowCount:=strtoint(edit3.Text)+1;
stringgrid2.ColCount:=strtoint(edit4.Text)+1;
stringgrid1.Width:=31*(strtoint(edit2.Text)+1);
stringgrid1.Height:=31*(strtoint(edit1.Text)+1);
stringgrid2.Width:=31*(strtoint(edit4.Text)+1);
stringgrid2.Height:=31*(strtoint(edit3.Text)+1);
stringgrid2.Left:=60+31*(strtoint(edit2.Text)+1);
stringgrid1.Visible:=true;
stringgrid2.Visible:=true;
stringgrid3.Visible:=false;
label6.Visible:=true;
label7.Visible:=true;
label6.Left:=24;
label7.Left:=60+31*(strtoint(edit2.Text)+1);
for a:=1 to 100 do
  for b:=1 to 100 do
  begin
  stringgrid1.Cells[a,b]:='0';
  stringgrid2.Cells[a,b]:='0';
  end
end
end;
procedure TForm1.Button3Click(Sender: TObject);
var
i, j : integer;
matriks_a : array[1..100,1..100] of integer;
matriks_b : array[1..100,1..100] of integer;
hasil : array[1..100,1..100] of integer;
begin
if (Edit1.Text<>Edit3.Text) or (Edit2.Text<>Edit4.Text) then
begin
  ShowMessage('Jumlah Baris kedua Matriks harus sama dan Jumah Kolom kedua matriks harus sama');
  edit1.SetFocus;
end;
if (Edit1.Text=Edit3.Text) and (Edit2.Text=Edit4.Text) then
  begin
    Stringgrid3.Visible:=true;
    stringgrid3.RowCount:=strtoint(edit1.Text)+1;
    stringgrid3.ColCount:=strtoint(edit2.Text)+1;
    stringgrid3.Width:=31*(strtoint(edit2.Text)+1);
    stringgrid3.Height:=31*(strtoint(edit1.Text)+1);
    stringgrid3.Left:=96+2*31*(strtoint(edit2.Text)+1);
    label8.Visible:=true;
    label8.Left:=96+2*31*(strtoint(edit2.Text)+1);
    label8.Caption:='Matriks A + Matriks B';
    for i:=1 to strtoint(edit1.Text) do
    begin
      StringGrid1.Cells[0,i]:='B'+inttostr(i);
      StringGrid2.Cells[0,i]:='B'+inttostr(i);
      StringGrid3.Cells[0,i]:='B'+inttostr(i);
    for j:=1 to strtoint(edit2.Text) do
    begin
      StringGrid1.Cells[j,0]:='K'+inttostr(j);
      StringGrid2.Cells[j,0]:='K'+inttostr(j);
      StringGrid3.Cells[j,0]:='K'+inttostr(j);
      matriks_a[i,j]:=strtoint(stringgrid1.Cells[j,i]);
      matriks_b[i,j]:=strtoint(stringgrid2.Cells[j,i]);
      hasil[i,j]:=matriks_a[i,j]+matriks_b[i,j];
      Stringgrid3.Cells[j,i]:=inttostr(hasil[i,j]);
    end;
    end;
  end
end;
procedure TForm1.FormCreate(Sender: TObject);
var a, b : integer;
begin
edit1.Text:='1';
edit2.Text:='1';
edit3.Text:='1';
edit4.Text:='1';
label6.Visible:=false;
label7.Visible:=false;
label8.Visible:=false;
stringgrid1.Visible:=false;
stringgrid2.Visible:=false;
for a:=1 to 100 do
  for b:=1 to 100 do
  begin
  stringgrid1.Cells[a,b]:='0';
  stringgrid2.Cells[a,b]:='0';
  end
end;
procedure TForm1.Button2Click(Sender: TObject);
var a, b : integer;
begin
stringgrid1.Visible:=false;
stringgrid2.Visible:=false;
stringgrid3.Visible:=false;
edit1.SetFocus;
Edit1.Text:='1';
Edit2.Text:='1';
Edit3.Text:='1';
Edit4.Text:='1';
label6.Visible:=false;
label7.Visible:=false;
label8.Visible:=false;
for a:=1 to 100 do
  for b:=1 to 100 do
  begin
  stringgrid1.Cells[a,b]:='0';
  stringgrid2.Cells[a,b]:='0';
  end
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
close;
end;
end.

No comments:
Post a Comment