with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_text_Io; use Ada.Integer_Text_Io; with traitement_pile; use traitement_pile; procedure td2 is -- specification -- {preconditions : - } -- {postconditions : - } pile : t_pile; choix : integer; element : integer; element_retour : integer; procedure inverser(pile : in out t_pile) is -- specification -- {preconditions : - } -- {postconditions : les elements de la pile sont inverses} pile_sauvegarde : t_pile; pile_sauvegarde2 : t_pile; begin initialiser_pile(pile_sauvegarde); initialiser_pile(pile_sauvegarde2); for i in 1..pile.sommet loop depiler(pile,element_retour); empiler(pile_sauvegarde,element_retour); end loop; for i in 1..pile_sauvegarde.sommet loop depiler(pile_sauvegarde,element_retour); empiler(pile_sauvegarde2, element_retour); end loop; for i in 1..pile_sauvegarde2.sommet loop depiler(pile_sauvegarde2,element_retour); empiler(pile, element_retour); end loop; end; begin new_line; put("/**************************************+"); new_line; put("+ TD2 : LES PILES +"); new_line; put("+**************************************/"); new_line; new_line; initialiser_pile(pile); while choix /= 0 loop put("1- Empiler un élément"); new_line; put("2- Depiler la pile"); new_line; put("3- Tester si la pile est vide"); new_line; put("4- Tester si la pile est pleine"); new_line; put("5- Donner la taille de la pile"); new_line; put("6- Bonus : inverser la pile"); new_line; put("7- Réinitialiser la pile"); new_line; put("0- Quitter le programme"); new_line; get(choix); new_line; if choix = 1 then put("Entrez l'élément à empiler"); new_line; get(element); if pile_pleine(pile) then put("la pile est pleine"); else empiler(pile,element); put("manipulation effectuée avec succès"); end if; new_line; new_line; end if; if choix = 2 then if pile_vide(pile) then put("la pile est vide"); else depiler(pile, element_retour); put("La pile a été dépilée. L'élément "); put(element_retour); put(" a été supprimé."); end if; new_line; new_line; end if; if choix = 3 then if pile_vide(pile) then put("la pile est vide"); new_line; new_line; else put("la pile n'est pas vide"); new_line; new_line; end if; end if; if choix = 4 then if pile_pleine(pile) then put("la pile est pleine"); new_line; new_line; else put("la pile n'est pas pleine"); new_line; new_line; end if; end if; if choix = 5 then put("la taille de la pile est"); put(taille_pile(pile)); new_line; new_line; end if; if choix = 6 then inverser(pile); put("pile inversée"); new_line; new_line; end if; if choix = 7 then initialiser_pile(pile); put("pile initialisée"); new_line; new_line; end if; end loop; end td2;
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_text_Io; use Ada.Integer_Text_Io; with traitement_pile; package body traitement_pile is procedure initialiser_pile(pile : in out t_pile) is -- specification -- {preconditions : - } -- {postconditions : la pile entree en parametre est initialisee (sommet et longueur établis à 0 } begin pile.longueur := 0; pile.sommet := 0; end initialiser_pile; procedure empiler(pile : in out t_pile; element : in integer) is -- specification -- {preconditions : - } -- {postconditions : l'élément est empilé sur la pile } begin pile.contenu(pile.sommet+1) := element; pile.longueur := pile.longueur+1; pile.sommet := pile.sommet+1; end empiler; procedure depiler(pile : in out t_pile; element_retour : out integer) is -- specification -- {preconditions : - } -- {postconditions : la pile est depilée } begin element_retour := pile.contenu(pile.sommet); pile.longueur := pile.longueur-1; pile.sommet := pile.sommet-1; end depiler; function pile_vide(pile : t_pile) return boolean is -- specification -- {preconditions : - } -- {postconditions : renvoie vrai si la pile est vide et faux si elle ne l'est pas } begin if pile.longueur = 0 then return true; else return false; end if; end pile_vide; function pile_pleine(pile : t_pile) return boolean is -- specification -- {preconditions : - } -- {postconditions : renvoie vrai si la pile est pleine et faux si elle ne l'est pas } begin if pile.longueur = 5 then return true; else return false; end if; end pile_pleine; function taille_pile(pile : t_pile) return integer is -- specification -- {preconditions : - } -- {postconditions : renvoie la taille de la pile } begin return pile.longueur; end taille_pile; end traitement_pile;
package traitement_pile is type t_tableau is array(0..5) of integer; type t_pile is record contenu : t_tableau; longueur : integer; sommet : integer; end record; procedure initialiser_pile(pile : in out t_pile); procedure empiler(pile : in out t_pile; element : in integer); procedure depiler(pile : in out t_pile; element_retour : out integer); function pile_vide(pile : t_pile) return boolean; function pile_pleine(pile : t_pile) return boolean; function taille_pile(pile : t_pile) return integer; end traitement_pile;