{ File: compl.pas }

program ComplessiEser;

{ Permette di scegliere una operazione tra:
  - scrivere sul file di record COMPL.DAT dei numeri complessi dati in input
  - leggere 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}

var
  zIn, zApp : Complex;	          { per leggere o accumulare i complessi}
  filegen   : file of Complex;
  op        : char;		  { per le scelte }
  i, quanti : integer; 	          { per i cicli }


begin { ComplessiEser }
  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 mettere? ');
                   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);
                  while not eof(filegen) do
                  begin
                    read(filegen, zIn);
                    SommaC(zApp, zIn, zApp);
                  end;
                  WriteComplex(zApp);
                  close(filegen)
                end;
      'f', 'F' : writeln('FINE');
      else
        writeln(' SCELTA SCORRETTA ')
    end
  until (op = 'F') or (op = 'f')
end. { ComplessiEser }