with Ada.Text_Io, Ada.Integer_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io;
procedure Td3 is
function Factorielle(N: Positive) return Natural is
begin
if N = 0 then
return 1;
end if;
return N * Factorielle(N - 1);
end Factorielle;
--exo 2
function Puissance(N : Float; Exp : Integer) return Float is
begin
if Exp = 0 then
return 1.0;
end if;
return N * Puissance(N, Exp - 1);
end Puissance;
function Multiplie(A, B : Integer) return Integer is
function Multiplie_Rec(A, B : Integer) return Integer is
begin
if B = 0 then
return 0;
end if;
return A + Multiplie_Rec(A, B - 1);
end Multiplie_Rec;
Res : Integer;
begin
Res := Multiplie_Rec(abs(A), abs(B));
if (A < 0 and B < 0) or (A >= 0 and B >= 0) then
return Res;
else
return -Res;
end if;
end Multiplie;
function Additionne(A, B : Natural) return Natural is
begin
if A = 0 then
if B = 0 then
return 0;
else
return 1 + Additionne(0, B - 1);
end if;
else
return 1 + Additionne(A - 1, B);
end if;
end Additionne;
function Pgcd(A, B : Natural) return Natural is
begin
if B = 0 then
return A;
elsif A >= B then
return Pgcd(A - B, B);
else
return Pgcd(B - A, A);
end if;
end Pgcd;
-- exo 3
function Inverse_v1(S : String) return String is
function Head(S: in String) return String is
begin
return S(S'First..S'First);
end Head;
function Tail(S : in String) return String is
begin
return S((S'First+1)..S'Last);
end Tail;
function "+"(S1, S2: in String) return String is
S : String(1..(S1'Length + S2'length));
begin
S(1..S1'Length) := S1;
S((S1'Length + 1)..S'Last) := S2;
return S;
end "+";
begin
if S = "" then return ""; end if;
return Inverse_V1(Tail(S)) + Head(S);
end Inverse_v1;
function Inverse_v2(S : String) return String is
procedure Inverse_Rec(S : in String; Pos : in Natural; Res : out String) is
begin
if Pos > S'Last then return; end if;
Res(Res'First + (Res'Last - Pos)) := S(Pos);
Inverse_Rec(S, Pos + 1, Res);
end Inverse_Rec;
Res : String(S'Range);
begin
Inverse_Rec(S, S'First, Res);
return Res;
end Inverse_v2;
function EstPalindrome_V1(S : String) return Boolean is
begin
return Inverse_v1(S) = S;
end EstPalindrome_V1;
function EstPalindrome_V2(S : String) return Boolean is
function EstPal_Rec(S : in String; F, L : Integer) return Boolean is
begin
if F >= L then
return True;
end if;
if S(F) /= S(L) then
return False;
end if;
return EstPal_Rec(S, F + 1, L - 1);
end EstPal_Rec;
begin
return EstPal_Rec(S, S'First, S'Last);
end EstPalindrome_V2;
function Compte(S : in String; C : in Character) return Natural is
function Compte_Rec(S : in String;
C : in Character;
Pos : in Natural) return Natural is
begin
if Pos > S'Last then return 0; end if;
if S(Pos) = C then
return 1 + Compte_Rec(S, C, Pos + 1);
else
return Compte_Rec(S, C, Pos + 1);
end if;
end Compte_Rec;
begin
return Compte_Rec(S, C, S'First);
end Compte;
function Recherche(S : in String; C : in Character) return Natural is
function Recherche_Rec(S : in String;
C : in Character;
Pos : in Natural) return Natural is
begin
if Pos > S'Last then return 0;
elsif S(Pos) = C then return Pos;
else return Recherche_Rec(S, C, Pos + 1);
end if;
end Recherche_Rec;
begin
return Recherche_Rec(S, C, S'First);
end Recherche;
N : constant Natural := 5;
type T_Tab is array (1..N) of Integer;
procedure Get(T : out T_Tab) is
procedure Get_Rec(T : out T_Tab; Pos : Natural) is
begin
if Pos > T'Last then return; end if;
Get(T(Pos));
Get_Rec(T, Pos + 1);
end Get_Rec;
begin
Get_Rec(T, T'First);
end Get;
procedure Put(T : in T_Tab) is
procedure Put_Rec(T : in T_Tab; Pos : in Natural) is
begin
if Pos > T'Last then return; end if;
if Pos /= T'First then Put(", "); end if;
Put(T(Pos), 0);
Put_Rec(T, Pos + 1);
end Put_Rec;
begin
Put("T_Tab(");
Put_Rec(T, T'First);
Put(')');
end Put;
function Somme(T : in T_Tab) return Integer is
function Somme_Rec(T : in T_Tab; Pos : in Natural) return Integer is
begin
if Pos > T'Last then return 0; end if;
return T(Pos) + Somme_Rec(T, Pos + 1);
end Somme_Rec;
begin
return Somme_Rec(T, T'First);
end Somme;
function "+"(T1, T2 : in T_Tab) return T_Tab is
procedure Somme_Rec(T1, T2 : in T_Tab; T : out T_Tab; Pos : in Natural) is
begin
if Pos > T'Last then return; end if;
T(Pos) := T1(Pos) + T2(Pos);
Somme_Rec(T1, T2, T, Pos + 1);
end Somme_Rec;
Ttmp : T_Tab;
begin
Somme_Rec(T1, T2, Ttmp, T1'First);
return Ttmp;
end "+";
function EstTrie(T : in T_Tab) return Boolean is
function EstTrie_rec(T : in T_Tab; Pos : in Integer) return Boolean is
begin
if Pos > T'Last then return True; end if;
if T(Pos - 1) > T(Pos) then
return False;
end if;
return EstTrie_Rec(T, Pos + 1);
end EstTrie_Rec;
begin
return EstTrie_Rec(T, T'First + 1);
end EstTrie;
function Min(T : in T_Tab) return Integer is
function Min(A, B : in Integer) return Integer is
begin
if A < B then return A; else return B; end if;
end Min;
function Min_Rec(T : in T_Tab; Pos : in Integer) return Integer is
begin
if Pos = T'Last then return T(Pos); end if;
return Min(T(Pos), Min_Rec(T, Pos + 1));
end Min_Rec;
begin
return Min_Rec(T, T'First);
end Min;
T : T_Tab;
begin
Put("Inverse_V1(""Bonjour"") = ");
Put(Inverse_V1("bonjour")); New_Line;
Put("Inverse_V2(""Bonjour"") = ");
Put(Inverse_V2("bonjour")); New_Line;
Put("EstPalindrome_V1(""coucou"") = ");
Put(Boolean'Image(EstPalindrome_V1("coucou"))); New_Line;
Put("EstPalindrome_V2(""coucou"") = ");
Put(Boolean'Image(EstPalindrome_V2("coucou"))); New_Line;
Put("EstPalindrome_V1(""engagelejeuquejelegagne"") = ");
Put(Boolean'Image(EstPalindrome_V1("engagelejeuquejelegagne"))); New_Line;
Put("EstPalindrome_V2(""engagelejeuquejelegagne"") = ");
Put(Boolean'Image(EstPalindrome_V2("engagelejeuquejelegagne"))); New_Line;
Put("Compte(""bonjour"", 'o') = ");
Put(Compte("bonjour", 'o'), 0); New_Line;
Put("Recherche(""bonjour"", 'o') = ");
Put(Recherche("bonjour", 'o'), 0); New_Line;
Put("Recherche(""bonjour"", 'z') = ");
Put(Recherche("bonjour", 'z'), 0); New_Line;
Put("get(t) = ");
Get(T);
Put("put(t) = ");
Put(T); New_Line;
Put("Somme(t) = ");
Put(Somme(T), 0); New_Line;
Put("T + T = ");
Put(T + T); New_Line;
Put("EstTrie(T) = ");
Put(Boolean'Image(EstTrie(T))); New_Line;
Put("min(T) = ");
Put(Min(T), 0); New_Line;
end Td3;