program tab3;

{ TASKS 1,2 }

uses crt;

const n : integer = 80;

var f1,f2:text;
    s,s2:string;
    i,i2:integer;
    code:integer;
    sl:array [1..100] of integer;  {sirky jednotlivych sloupcu}
    countsl,indexsl,max:integer;
    konradek:boolean;
    space:byte;
    w:array [1..14,1..14] of string;  {napisy v jednotlivych bunkach}
    x,y,wx,wy:integer;
    l,k:integer;
    fl:array [1..14,1..14] of byte;  {priznak napisu - zarovnavani}
    {0 = left}
    {1 = right}
    {2 = center}
    oldfl:byte;

procedure konec (s:string); {chyba}
begin
   writeln;
   writeln(s);
   writeln;
   halt;
end;

label pom;

begin {main}
   clrscr;
   for i:=1 to 100 do sl[i]:=0;
   indexsl:=1;
   countsl:=1;
   wy:=1;
   writeln('3TAB');
   writeln;
   if paramcount = 3 then begin   {testovani vstupnich parametru}
      s:=paramstr(1);
      if s[1]='-' then begin
         delete(s,1,1);
         val(s,n,code);
      end;
      s:=paramstr(2);
      assign(f1,s);
      {$I-}
      reset(f1);
      {$I+}
      if ioresult <> 0 then konec('Error: Nenalezen vstupni soubor.');
      writeln('Input File : ',s);
      s:=paramstr(3);
      assign(f2,s);
      rewrite(f2);
      writeln('Output File : ',s);
   end else begin
      s:=paramstr(1);
      assign(f1,s);
      {$I-}
      reset(f1);
      {$I+}
      if ioresult <> 0 then konec('Error: Nenalezen vstupni soubor.');
      s:=paramstr(2);
      writeln('Input File : ',s);
      assign(f2,s);
      rewrite(f2);
      writeln('Output File : ',s);
   end;
   repeat  {Zpracovani celeho souboru -> ulozeni dat do poli w,fl a sl}
      readln(f1,s);
      while ((s[1]='<') and (s[2]='T') and (s[3]='R') and (s[4]='>')) and
            not(length(s)=0) do begin
         wx:=1;
         i:=5;
         indexsl:=1;
      pom:
         konradek:=false;
         if ((s[i]='<') and (s[i+1]='T') and (s[i+2]='H')) or
            ((s[i]='<') and (s[i+1]='T') and (s[i+2]='D')) then begin
            inc(i,3);
            if (s[i]='>') then begin {pri <TH> se zarovana doleva}
               oldfl:=0;
               inc(i);
            end else begin
               if s[i+1]='A' then begin  {ALIGN=?}
                  inc(i,7);
                  case s[i] of
                     'R':begin   {RIGHT}
                        inc(i,6); oldfl:=1;
                     end;
                     'L':begin   {LEFT}
                        inc(i,5); oldfl:=0;
                     end;
                     'C':begin   {CENTRE}
                        inc(i,7); oldfl:=2;
                     end;
                  end;
               end;
            end;
            s2:='';
            max:=0;
            space:=0;
            repeat
               if (s[i]='&') then begin
                  case s[i+1] of  {spec. pripady : &,<,> }
                     'l':begin
                        inc(i,3);
                        s2:=s2+'<';
                     end;
                     'g':begin
                        inc(i,3);
                        s2:=s2+'>';
                     end;
                     'a':begin
                        inc(i,4);
                        s2:=s2+'&';
                     end;
                  end;
                  inc(max);
                  inc(space);
               end else begin
                  if (s[i]=' ') then begin
                     if space=0 then begin
                        s2:=s2+s[i];
                        inc(i);
                        inc(max);
                        inc(space);
                     end else inc(i);  {mezera se opakuje}
                  end else begin
                     s2:=s2+s[i];
                     inc(i);
                     inc(max);
                     if max>255 then begin
                        close(f1);
                        close(f2);
                        konec('Error: Table is too long to fit line.');
                     end;
                     space:=0;
                  end;
               end;
               if i>length(s) then begin  {prechod na dalsi radek}
                  readln(f1,s);
                  i:=1;
                  konradek:=true;
               end;
            until (s[i]='<');
            if max>sl[indexsl] then sl[indexsl]:=max;
            if indexsl>countsl then countsl:=indexsl;
            inc(indexsl);
            fl[wx,wy]:=oldfl;
            w[wx,wy]:=s2;
            inc(wx);
         end
         else begin
            i:=length(s)+1;
            konradek:=true;
         end;
         if not(konradek) then goto pom
         else if (i<length(s)) then goto pom;
         inc(wy);
      end;
   until eof(f1);

   for y:=1 to wy do begin  {vykresleni}
      l:=0;
      for x:=1 to countsl do begin  {horizontalni linka}
         s2:='+';
         for i:=1 to sl[x]+2 do s2:=s2+'-';
         if x=countsl then s2:=s2+'+';
         write(f2,s2); write(s2);
         inc(l,length(s2));
      end;
      if l>n then begin
         close(f1);
         close(f2);
         konec('Error: Table is too long to fit line.');
      end;
      writeln(f2); writeln;
      if y<wy then begin
         for x:=1 to countsl do begin
            s2:=w[x,y];
            case fl[x,y] of                                {zarovavani}
{left}         0:while length(s2)<sl[x] do s2:=s2+' ';
{right}        1:while length(s2)<sl[x] do s2:=' '+s2;
{centre}       2:begin
                  k:=0;
                  while length(s2)<sl[x] do begin
                     if k=0 then begin
                        s2:=' '+s2; k:=1;
                     end else begin
                        s2:=s2+' '; k:=0;
                     end;
                  end;
               end;
            end;
            if x<countsl then begin
               write(f2,'| '+s2+' '); write('| '+s2+' ');
            end
            else begin
               writeln(f2,'| '+s2+' |'); writeln('| '+s2+' |');
            end;
         end;
      end;
   end;
   close(f1);
   close(f2);
   writeln;
   writeln('Press ENTER');
   readln;
end.