Показать сообщение отдельно
  #135  
Старый 17.05.2008, 16:53
MegaPiha MegaPiha вне форума
Активный
 
Регистрация: 24.03.2008
Сообщения: 227
Версия Delphi: Delphi 7
Репутация: 30
По умолчанию

Цитата:
Сообщение от Олиська
пожалуйста!!!второй день ничего путного не выходит!!! двумерный динамический массив (nxn) задать генератором случайных чисел;запись во внешний фаил,чтение из файла,сортировка по возрастанию эл-тов

Можно сделать примерно так.

Код:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    StringGrid3: TStringGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  nxn: array of array of integer;
  n,m:integer;


implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);//сохранение в файл
Var
   s :TStringlist;
   Str:String;
   i,j : Integer;
begin
 with TSaveDialog.Create(Owner) do
   begin
       DefaultExt:='.txt';
       Filter:= 'Файлы проекта|*.txt';
       if not Execute then exit;
       s:=TStringList.Create;
       s.Clear;
       For i:=1 to n do
       begin
           Str := '' ;
           For j:=1 to m do
               str:=str+IntToStr(nxn[j,i])+' ';
           s.Add(Str);
       end;
       S.SaveToFile(FileName);
   end;
   s.Free;
end;


procedure TForm1.Button2Click(Sender: TObject);//чтение из файла
var
   s :TStringlist;
   i,j, posy, posx: Integer;
   Str:String;
Begin
    with TOpenDialog.Create(Owner) do
    begin
       DefaultExt:='.txt';
       Filter:= 'файлы проекта|*.txt';
       if not Execute then exit;
       s:=TStringList.Create;
       S.LoadFromFile(FileName);
       For posy := 1 to S.Count do
       begin
           posx := 0;
           Str := '';
           For i := 1 to Length(S.Strings[posy-1]) do
           begin
              if S.Strings[posy-1][i] <> ' ' then
                  Str := Str + S.Strings[posy-1][i]
              else
              begin
                  StringGrid2.Cells[posx, posy-1] := Str;
                  Str := '';
                  inc(posx);
              end;
            end;

       end;
       S.Free;
    end;
    With StringGrid2 do begin
        colCount:=posx;
        RowCount:=posy;
        For i:=0 to posx-1 do
           ColWidths[i]:=Trunc(Width/posx);
    end;
    n:= StringGrid2.RowCount;
    m:=StringGrid2.ColCount;
    SetLength(nxn,m,n);
    For i:=0 to n-2 do
    For j:=0 to m-1 do
       nxn[i,j]:=StrToInt(StringGrid2.Cells[i,j]);

end;



procedure TForm1.Button4Click(Sender: TObject);//заполнение массива
Var i, j:integer;
begin
   if (Trim(Edit1.Text) <>'') and (Trim(Edit2.Text)<>'') then
   begin
      n:=StrToInt(Edit1.Text);
      m:=StrToInt(Edit2.Text);
      With StringGrid1 do begin
          ColCount:=n;
          RowCount:=m;
          For i:=0 to n-1 do
              ColWidths[i]:=Trunc(Width/n);
      end;
      SetLength(nxn,n+1,m+1);
      Randomize;
      For i:=1 to n do
        For j:=1 to m do
        begin
            nxn[i,j]:=random(10);
            StringGrid1.Cells[i-1,j-1]:=IntToStr(nxn[i,j]);
        end;
   end
   else
   ShowMessage('Не заполнена размерность массива!')
end;




procedure TForm1.Button3Click(Sender: TObject);//сортировка
var s,i,j,k:integer;
begin
    for j:=0 to n-1 do
    for k:=0 to m-2 do
       for i:=0 to m-k-2 do
         if nxn[i,j]>nxn[i+1,j] then
         begin
            S:=nxn[i,j];
            nxn[i,j]:=nxn[i+1,j];
            nxn[i+1,j]:=S;
         end;

   For i:=0 to n-2 do
    For j:=0 to m-1 do
       StringGrid3.Cells[i,j]:=IntToStr(nxn[i,j]);

   With StringGrid3 do begin
        colCount:=n-1;
        RowCount:=m;
        For i:=1 to colCount-1 do
           ColWidths[i]:=Trunc(Width/colCount);
    end;
end;

end.