{ File: complric.pas }

program ComplessiEserRic;

{ Permette di scegliere una operazione tra:
  - scrivere sul file di record COMPL.DAT dei numeri complessi dati in input
  - leggere ricorsivamente i numeri complessi contenuti in COMPL.DAT e
    stamparne la somma
  - terminare il programma

  Usa i numeri complessi
  TRAENDONE LA DEFINIZIONE E LE FUNZIONALITA'
  dal file COMPLDEF.PAS  }

{$I COMPLDEF.PAS}

{ Le variazioni rispetto a ComplessiEser sono segnalate da "**" }


type
  TipoFileComplessi = file of Complex;   {**}


var
  zIn, zApp : Complex;
  a, b      : real;
  filegen   : TipoFileComplessi;      {**}
  op        : char;
  i, quanti : integer;


{**}
procedure SommaRic (var c: Complex; var f: TipoFileComplessi);
{ Aggiunge ricorsivamente in c i complessi contenuti in f. }
var
  caux : Complex;
begin
  if not eof(f) then
  begin
    read(f, caux);		{ lettura di un complesso da f }
    SommaC(c, caux, c);		{ c := c+caux }
    SommaRic(c, f);             { somma a c i numeri rimanenti nel file}
  end;
end; { SommaRic }


begin { ComplessiEserRic }
  repeat
    writeln('che vuoi fare? [A/B/F]');
    writeln(' A: scrivere su COMPL.DAT numeri complessi letti in input');
    writeln(' B: stampare la somma dei numeri complessi letti da COMPL.DAT');
    writeln(' F: per terminare');
    readln(op);
    case op of
      'a', 'A' : { mettere dei complessi in COMPL.DAT }
                 begin
                   assign(filegen, 'COMPL.DAT');
                   rewrite(filegen);
                   write('quanti complessi vuoi inserire in COMPL.DAT? ');
                   readln(quanti);
                   for i := 1 to quanti do
                   begin
                     ReadComplex(zIn);
                     write(filegen, zIn)
                   end;
                   close(filegen)
                 end;
      'b', 'B' : { leggere i complessi in COMPL.DAT e stampare la somma } {**}
                 begin
                   assign(filegen, 'COMPL.DAT');
                   reset(filegen);
                   AssegnaC(0, 0, zApp);
                   SommaRic(zApp, filegen);
                   close(filegen);
                   WriteComplex(zApp)
                 end;
      'f', 'F' : writeln('FINE');
      else
        writeln(' SCELTA SCORRETTA ');
    end;
  until (op = 'F') or (op = 'f')
end. { ComplessiEserRic }