unit uantelacionoptima;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  xmatdefs, uauxiliares;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }


procedure salavematriz( m: TDAOfDAofNReal; archi: string );
var
  f: textfile;
  k, j: integer;
begin
  assignfile( f, archi );
  rewrite( f );
  write( f, 'Sem' );
  for j:= 0 to 51 do
    write( f, #9, 'lag'+IntToStr( j ) );
  writeln( f);
  for k:= 0 to 51 do
  begin
    write( f, k );
    for j:= 0 to 51 do
      write( f, #9, m[k][j] );
    writeln( f );
  end;
  closefile( f );
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
  r: string;
  kArchi: integer;
  k, j: integer;
  bxn, pxn, sxn: TDAOfDAofNReal;
  nfilas, ncols: integer;
  pal: string;
  jPos: integer;

begin
  setlength( bxn, 52 );
  setlength( pxn, 52 );
  setlength( sxn, 52 );
  for k:= 0 to 51 do
  begin
    setlength( bxn[k], 52 );
    setlength( pxn[k], 52 );
    setlength( sxn[k], 52 );
  end;

  for kArchi:= 0 to 51 do
  begin
    assignfile( f, 'mxxt_'+IntToStr(kArchi)+'.xlt' );
    reset( f );
    readln( f, r ); //
    pal:= nextpal( r );
    nfilas:= nextint( r );
    pal:= nextpal( r );
    ncols:= nextint( r );

    jPos:= 0;

    readln( f, r );
    for k:= 0 to nfilas -1 do
    begin
      readln( f, r );
      if ( k mod 4 ) = 3 then
      begin
        pal:= nextpal( r );
        bxn[kArchi][jPos]:= NextFloat( r );
        pxn[kArchi][jPos]:= NextFloat( r );
        sxn[kArchi][jPos]:= NextFloat( r );
        inc( jPos );
      end;
    end;
    closefile( f );
  end;

  salavematriz( bxn, 'bonete_ao_n34.xlt' );
  salavematriz( pxn, 'palmar_ao_n34.xlt' );
  salavematriz( sxn, 'salto_ao_n34.xlt' );


end;

end.

