-- put et get
with Ada.Text_Io, Ada.Float_Text_Io;
use Ada.Text_Io, Ada.Float_Text_Io;
-- abs
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
procedure Exo01a is
-- exo 1a
type Complexe is record
Reel : Float;
Imag : Float;
end record;
-- exo 1b
function Add(C1, C2 : Complexe) return Complexe is
S : Complexe;
begin
S.Reel := C1.Reel + C2.Reel;
S.Imag := C1.Imag + C2.Imag;
return S;
end;
-- Nous pouvons aussi redéfinir l'opérateur + pour les complexes, ce
-- qui permet d'écrire z1 + z2 :
function "+"(C1, C2 : Complexe) return Complexe is
begin
return (Reel => C1.Reel + C2.Reel, Imag => C1.Imag + C2.Imag);
end;
-- exo 1b bonus(x)
function "-"(C1, C2 : Complexe) return Complexe is
begin
return C1 + (Reel => -C2.Reel, Imag => -C2.Imag);
end;
-- exo 1c
function "*"(C1, C2 : Complexe) return Complexe is
begin
return (C1.Reel * C2.Reel - C1.Imag * C2.Imag,
C1.Imag * C2.Reel + C1.Reel * C2.Imag);
end;
-- exo 1d
function Module(C : Complexe) return Float is
begin
return (Sqrt(C.Reel ** 2 + C.Imag ** 2));
end;
-- exo 1e
procedure Get(C : out Complexe) is
begin
Get(C.Reel);
Get(C.Imag);
end;
-- exo 1f
procedure Put(C : in Complexe) is
begin
Put('(');
Put(C.Reel, 0, 3, 0);
if C.Imag >= 0.0 then
Put(" + ");
else
Put(" - ");
end if;
Put(abs(C.Imag), 0, 3, 0);
Put("i)");
end;
-- exo 1f bonus
procedure Put_Line(C : in Complexe) is
begin
Put(C); New_Line;
end;
-- exo 1g
function "**"(C : Complexe; N : Natural) return Complexe is
Res : Complexe := (1.0, 0.0);
begin
for I in 1..N loop
Res := Res * C;
end loop;
return Res;
end;
C1 : Complexe := (1.5, 0.0);
C2 : Complexe := (1.5, 2.5);
begin
Put(C1); Put(" + "); Put(C2); Put(" = "); Put_Line(C1 + C2);
Put(C1); Put(" - "); Put(C2); Put(" = "); Put_Line(C1 - C2);
Put(C1); Put(" * "); Put(C2); Put(" = "); Put_Line(C1 * C2);
Put(C1); Put(" ** 0 = "); Put_Line(C1 ** 0);
Put(C2); Put(" ** 0 = "); Put_Line(C2 ** 0);
Put(C1); Put(" ** 1 = "); Put_Line(C1 ** 1);
Put(C2); Put(" ** 1 = "); Put_Line(C2 ** 1);
Put(C1); Put(" ** 2 = "); Put_Line(C1 ** 2);
Put(C2); Put(" ** 2 = "); Put_Line(C2 ** 2);
Put(C1); Put(" ** 3 = "); Put_Line(C1 ** 3);
Put(C2); Put(" ** 3 = "); Put_Line(C2 ** 3);
Put('|'); Put(C1); Put("| = "); Put(Module(C1)); New_Line;
Put('|'); Put(C2); Put("| = "); Put(Module(C2)); New_Line;
end;