with Ada.Text_IO, ada.integer_text_io, Ada.Numerics.Discrete_Random; use Ada.Text_IO, ada.integer_text_io; procedure TD2 is max_val : constant := 99; n : constant := 10; type T_Array is array (1..n) of integer; procedure swap(a,b : in out integer) is v : integer; begin v := a; a := b; b := v; end; procedure Put(a : in T_Array) is begin for i in a'range loop put(a(i), 3); end loop; new_line; end; procedure test(b : Boolean; s : String) is begin put(s); put(" : "); if b then put("ok !"); else put("failed !"); end if; New_Line; end; subtype random_range is integer range 0..max_val; package random_int is new Ada.Numerics.Discrete_Random(random_range); use random_int; procedure genere(a : in out T_Array) is Gen : Generator; begin Reset(Gen); for i in a'Range loop a(i) := Random(Gen); end loop; end; function sorted(a : in T_Array) return Boolean is begin for i in a'first..a'last-1 loop if a(i) > a(i + 1) then return false; end if; end loop; return true; end; procedure tri1(a : in out T_Array) is begin for i in 1..(n - 1) loop for j in 1..(n - i) loop if a(j) > a(j + 1) then swap(a(j), a(j + 1)); end if; end loop; end loop; end; procedure tri2(a : in out T_Array) is lastChange : integer; nextChange : integer; begin lastChange := a'last - 1; while lastchange > 0 loop nextChange := 0; for j in 1..lastChange loop if a(j) > a(j + 1) then nextChange := j; swap(a(j), a(j + 1)); end if; end loop; lastChange := nextChange - 1; end loop; end; procedure tri3(a : in out T_Array) is l, nl : integer; r, nr : integer; begin l := 1; r := a'last; while l < r loop nr := 0; for j in l..r - 1 loop if a(j) > a(j + 1) then nr := j; swap(a(j), a(j + 1)); end if; end loop; r := nr; nl := a'last; for j in reverse l + 1..r loop if a(j) < a(j - 1) then nl := j; swap(a(j), a(j - 1)); end if; end loop; l := nl; end loop; end; -- Exercice 2 function strlen(s : String) return Positive is begin return s'last - s'first + 1; end; function isOnPostion(p, g : String; i : Positive) return boolean is begin if strlen(p) > strlen(g) then return false; end if; for k in p'range loop if p(k) /= g(i + k - p'first) then return false; end if; end loop; return true; end; procedure strIsIn(p, g : in String; pos : out Natural) is begin pos := 0; for i in g'first..g'last - strlen(p) + 1 loop if isOnPostion(p, g, i) then pos := i; return; end if; end loop; end; t, t1, t2, t3 : t_array; pos : natural; begin genere(t); put(t); t1 := t; tri1(t1); put(t1); test(sorted(t1), "tri1"); t2 := t; tri2(t2); put(t2); test(sorted(t2), "tri2"); t3 := t; tri3(t3); put(t3); test(sorted(t3), "tri3"); strIsIn("ab", "aab", pos); test(pos = 2, "strIsIn(""ab"", ""aab"", pos) = 2"); strIsIn("a", "aab", pos); test(pos = 1, "strIsIn(""a"", ""aab"", pos) = 1"); strIsIn("abc", "aab", pos); test(pos = 0, "strIsIn(""abc"", ""aab"", pos) = 0"); strIsIn("ce", "Ce qu'on laisse sur la table fait plus de bien que ce qu'on y prend.", pos); test(pos = 52, "strIsIn(""ce"", ""Ce qu'on laisse sur la table fait plus de bien que ce qu'on y prend."", pos) = 52"); strIsIn("Ce", "Ce qu'on laisse sur la table fait plus de bien que ce qu'on y prend.", pos); test(pos = 1, "strIsIn(""table"", ""Ce qu'on laisse sur la table fait plus de bien que ce qu'on y prend."", pos) = 1"); end TD2; -- 63 39 35 14 30 4 95 72 78 2 -- 2 4 14 30 35 39 63 72 78 95 --tri1 : ok ! -- 2 4 14 30 35 39 63 72 78 95 --tri2 : ok ! -- 2 4 14 30 35 39 63 72 78 95 --tri3 : ok ! --strIsIn("ab", "aab", pos) = 2 : ok ! --strIsIn("a", "aab", pos) = 1 : ok ! --strIsIn("abc", "aab", pos) = 0 : ok ! --strIsIn("ce", "Ce qu'on laisse sur la table fait plus de bien que ce qu'on y prend.", pos) = 52 : ok ! --strIsIn("table", "Ce qu'on laisse sur la table fait plus de bien que ce qu'on y prend.", pos) = 1 : ok !