program com;
type complex = record
                  re: real;
                  im: real
               end;
var x, y, z: complex;

procedure add(x, y: complex; var z: complex);
begin
   z.re := x.re + y.re;
   z.im := x.im + y.im
end;

procedure mul(x, y: complex; var z: complex);
begin
   z.re := (x.re * y.re) - (x.im * y.im);
   z.im := (x.re * y.im) + (x.im * y.re)
end;

function modulus(x: complex): real;
begin
   modulus := sqrt(sqr(x.re) + sqr(x.im))
end;

procedure realmul(r: real; x: complex; var z: complex);
begin
   z.re := r*x.re;
   z.im := r*x.im
end;

procedure writecomp(x: complex);
begin
   write(x.re:15:10);
   if x.im >= 0.0 then
      write( ' + ', abs(x.im):13:10, ' j')
   else write( ' - ', abs(x.im):13:10, ' j');
   writeln(', with modulus:', modulus(x):13:10)
end;

procedure readcomp(var z: complex);
begin
   writeln('Input complex number, real and imag parts:');
   readln(z.re, z.im)
end;

procedure compexp(x: complex; var z: complex);
var term: complex;
    i: integer;
begin
   z.re := 1; z.im := 0.0;
   term.re := 1.0; term.im := 0.0;
   i := 1;
   repeat
      writecomp(z);
      mul(x, term, term);
      realmul(1/i, term, term);
      add(z, term, z);
      i := i + 1
   until modulus(term) < 0.000001
end;

begin
   x.re := 0.5; x.im := 0.866025403; (* sqrt(3)/2 *)
   add(x, x, z);
   writecomp(z);
   realmul(2.0, x, z);
   writecomp(z);
   mul(x, x, z);
   writecomp(z);
   readcomp(x);
   compexp(x, z)
end.







