3 boyutlu statik ve dinamic array

Yazdığınız makaleleri ve üyelerimizin işine yarayacağını düşündüğünüz kodlarınızı gönderebilirsiniz. Bu foruma soru sormayın!
Cevapla
Kullanıcı avatarı
kelaynak2
Üye
Mesajlar: 135
Kayıt: 30 Haz 2003 02:18

3 boyutlu statik ve dinamic array

Mesaj gönderen kelaynak2 »

Aşağıdaki kod, 3 boyutlu statik ve dinamik arrayın oluşturulması, statik arraydan dinamik arraya bilgi aktarılması konusunu açıklar. 3 boyutlu arrayda özellikle hangisi satır, hangisi sutun gibi konular stringgrid kullanılarak gösterilmiştir.
Button1 statik arraydaki bilginin uzun yoldan stringgride aktarılmasını
button2 statik arraydan bilginin kısa yoldan aktarılmasını
buton3 dynamik arraydan bilginin kısa yoldan string gride aktarılmasını göstermektedir.
Her üç tuşa da bastığınızda string griddeki bilgiler değişmemektedir.

Kod: Tümünü seç

unit Unit1;

interface

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

  type
  Tinputarray=array [1..3,1..4,1..2] of char;
  //Tinputarray=array[1..3] of array[1..4] of array[1..2] of char;//yukarıdaki tanımla aynı.
  Toutputarray=array of array of array of char; //dinamik array.

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    SG: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
 outputum:Toutputarray;
const
inputum:Tinputarray=(
                     (('a','b'), //sutunlar 2 adet('a','b') sondaki sayı
                     ('c','d'),  //satırlar 4 adet(a-h arası)ortadaki sayı
                     ('e','f'),
                     ('g','h')),

                     (('j','k'),//3B arrayın 2.satırı.ilk sayi
                     ('l','m'),
                     ('n','o'),
                     ('p','r')),

                     (('s','t'),//3B arrayın 3.satırı.ilk sayi
                     ('u','p'),
                     ('q','x'),
                     ('y','z'))
                     );

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
ch:string;
x:integer;
begin
sg.Cells[0,0]:='3B Array 1.Satır';
sg.Cells[1,0]:='Array 1.Sutun:';
sg.Cells[2,0]:='Array 2.Sutun:';
for x:=1 to 4 do
sg.Cells[0,x]:=inttostr(x)+'.satır:';
sg.Cells[0,5]:='3B Array 2.Satır';
for x:=1 to 4 do
sg.Cells[0,x+5]:=inttostr(x)+'.satır:';
sg.Cells[0,10]:='3B Array 3.Satır';
for x:=1 to 4 do
sg.Cells[0,x+10]:=inttostr(x)+'.satır:';

ch:=inputum[1][1][1];
sg.Cells[1,1]:=ch;
ch:=inputum[1][1][2];
sg.Cells[2,1]:=ch;   //Cell (sutun,satir)

ch:=inputum[1][2][1];
sg.Cells[1,2]:=ch;
ch:=inputum[1][2][2];
sg.Cells[2,2]:=ch;

ch:=inputum[1][3][1];
sg.Cells[1,3]:=ch;
ch:=inputum[1][3][2];
sg.Cells[2,3]:=ch;

ch:=inputum[1][4][1];
sg.Cells[1,4]:=ch;
ch:=inputum[1][4][2];
sg.Cells[2,4]:=ch;

//Arrayın 2.satırına geçtik.
ch:=inputum[2][1][1];
sg.Cells[1,6]:=ch;
ch:=inputum[2][1][2];
sg.Cells[2,6]:=ch;   //Cell (sutun,satir)

ch:=inputum[2][2][1];
sg.Cells[1,7]:=ch;
ch:=inputum[2][2][2];
sg.Cells[2,7]:=ch;

ch:=inputum[2][3][1];
sg.Cells[1,8]:=ch;
ch:=inputum[2][3][2];
sg.Cells[2,8]:=ch;

ch:=inputum[2][4][1];
sg.Cells[1,9]:=ch;
ch:=inputum[2][4][2];
sg.Cells[2,9]:=ch;

//arrayın 3. satırına geçtik.
ch:=inputum[3][1][1];
sg.Cells[1,11]:=ch;
ch:=inputum[3][1][2];
sg.Cells[2,11]:=ch;   //Cell (sutun,satir)

ch:=inputum[3][2][1];
sg.Cells[1,12]:=ch;
ch:=inputum[3][2][2];
sg.Cells[2,12]:=ch;

ch:=inputum[3][3][1];
sg.Cells[1,13]:=ch;
ch:=inputum[3][3][2];
sg.Cells[2,13]:=ch;

ch:=inputum[3][4][1];
sg.Cells[1,14]:=ch;
ch:=inputum[3][4][2];
sg.Cells[2,14]:=ch;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
ch:string;
x,y,z,s:integer;
begin
s:=0;
for x:=low(inputum)to high(inputum) do begin//en büyük satır arrayın ilk parametresi
for y:=low(inputum[x])to high(inputum[x]) do begin //satır içindeki satırlar, 2.parametre
for  z:=low(inputum[x][y]) to high(inputum[x][y]) do begin //sütun.
ch:=inputum[x][y][z];
sg.Cells[z,y+s]:=ch;
sg.Cells[z,0]:='Array '+inttostr(z)+'.Sutun:';//Gerçi aynı şeyi x*y kadar yazıyor.
end;
 sg.Cells[0,y+s]:=inttostr(y)+'.satır:';
end;
sg.Cells[0,S]:='3B Array '+inttostr(x)+'.Satır';
s:=s+high(inputum[x])+1;//1 arada boşluk olması için.
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
ch:char;
x,y,z,s:integer;
begin
SetLength(outputum,3,4,2);//indexi 0..2 olan 3 satır ve indexi
                            // ; 0..3 olan 4 satır ve index 0..1 olan 2 sütün oluştur.


for x:=low(inputum)to high(inputum) do
for y:=low(inputum[x])to high(inputum[x]) do
for  z:=low(inputum[x][y]) to high(inputum[x][y])do
outputum[x-1][y-1][z-1]:=inputum[x][y][z];//dynamik arraylar daima 0 indexli başlar.

s:=0;
for x:=0 to high(outputum) do begin//en büyük satır arrayın ilk parametresi
for y:=0 to high(outputum[x]) do begin //satır içindeki satırlar, 2.parametre
for  z:=0 to high(outputum[x][y]) do begin //sütun.
ch:=outputum[x][y][z];
sg.Cells[z+1,y+s+1]:=ch;
sg.Cells[z+1,0]:='Array '+inttostr(z+1)+'.Sutun:';//Gerçi aynı şeyi x*y kadar yazıyor.
end;
sg.Cells[0,y+s+1]:=inttostr(y+1)+'.satır:';
end;
sg.Cells[0,S]:='3B Array '+inttostr(x+1)+'.Satır'; //+1 dynamik arraydan dolayı
s:=s+high(outputum[x])+1+1;//+1 arada boşluk olması için.+1 dynamik arraydan dolayı
end;
end;

end.

Form kodu:

Kod: Tümünü seç

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 488
  Height = 439
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 480
    Height = 41
    Align = alTop
    TabOrder = 0
  end
  object Panel2: TPanel
    Left = 0
    Top = 371
    Width = 480
    Height = 41
    Align = alBottom
    TabOrder = 1
    object Button1: TButton
      Left = 72
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Button1'
      TabOrder = 0
      OnClick = Button1Click
    end
    object Button2: TButton
      Left = 200
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Button2'
      TabOrder = 1
      OnClick = Button2Click
    end
    object Button3: TButton
      Left = 312
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Button3'
      TabOrder = 2
      OnClick = Button3Click
    end
  end
  object Panel3: TPanel
    Left = 0
    Top = 41
    Width = 480
    Height = 330
    Align = alClient
    TabOrder = 2
    object SG: TStringGrid
      Left = 1
      Top = 1
      Width = 478
      Height = 328
      Align = alClient
      ColCount = 3
      DefaultColWidth = 80
      DefaultRowHeight = 20
      RowCount = 15
      TabOrder = 0
    end
  end
end
Cevapla