Farklı bir çözüm olabilir:
Kod: Tümünü seç
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DiziDoldur(const aSutun, aSatir: Integer): Variant;
var
I, aXPos, aYPos, aDirection: Integer;
aChangeDirection: Boolean;
begin
Result := VarArrayCreate([0, aSatir - 1, 0, aSutun - 1], varVariant);
aXPos := 0;
aYPos := 0;
aDirection := 1;
for I := 0 to aSutun * aSatir - 1 do
begin
Result[aYPos, aXPos] := I + 1;
aChangeDirection := FALSE;
case aDirection of
1: if aXPos = aSutun - 1 then
aChangeDirection := TRUE
else
if VarToStr(Result[aYPos, aXPos + 1]) <> '' then aChangeDirection := TRUE;
2: if aYPos = aSatir - 1 then
aChangeDirection := TRUE
else
if VarToStr(Result[aYPos + 1, aXPos]) <> '' then aChangeDirection := TRUE;
3: if aXPos = 0 then
aChangeDirection := TRUE
else
if VarToStr(Result[aYPos, aXPos - 1]) <> '' then aChangeDirection := TRUE;
4: if aYPos = 0 then
aChangeDirection := TRUE
else
if VarToStr(Result[aYPos - 1, aXPos]) <> '' then aChangeDirection := TRUE;
end;
if aChangeDirection then aDirection := (aDirection mod 4) + 1;
case aDirection of
1: Inc(aXPos);
2: Inc(aYPos);
3: Dec(aXPos);
4: Dec(aYPos);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
V: Variant;
I, J: Integer;
St1, St2, aFormatFloatStr: string;
begin
V := DiziDoldur(StrToInt(Edit1.Text), StrToInt(Edit2.Text));
aFormatFloatStr := DupeString('0', Length(IntToStr(StrToInt(Edit1.Text) * StrToInt(Edit2.Text))));
St2 := '';
for I := 0 to VarArrayHighBound(V, 1) do
begin
St1 := '';
for J := 0 to VarArrayHighBound(V, 2) do
begin
if J > 0 then St1 := St1 + ', ';
St1 := St1 + FormatFloat(aFormatFloatStr, V[I, J]);
end;
St2 := St2 + St1 + sLineBreak;
end;
Memo1.Lines.Text := St2;
end;
end.
Kod: Tümünü seç
object Form1: TForm1
Left = 192
Top = 122
Width = 1003
Height = 511
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 Button1: TButton
Left = 240
Top = 176
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Edit1: TEdit
Left = 216
Top = 80
Width = 121
Height = 21
TabOrder = 1
Text = '5'
end
object Edit2: TEdit
Left = 216
Top = 112
Width = 121
Height = 21
TabOrder = 2
Text = '3'
end
object Memo1: TMemo
Left = 376
Top = 72
Width = 425
Height = 273
ScrollBars = ssBoth
TabOrder = 3
end
end