richie3366

sommelettres v1

Oct 8th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.95 KB | None | 0 0
  1. ----------------------------------------------------------------
  2. --  *  sommeslettres.adb
  3. --  *  Role : calculer la somme des lettres d'un mot (avec ' '=0, a=1, b=2, ..., z=26) en affichant un tableau detaillé
  4. --  *  Exemple : Screenshot : http://puu.sh/1czL9
  5. ----------------------------------------------------------------
  6.  
  7. with entrees_sorties; use entrees_sorties;  -- librairie de l'université
  8. with Ada.Strings.Fixed; use Ada.Strings.Fixed; -- permet de convertir une chaîne de caractères en minuscules
  9. with Ada.Strings.Maps.Constants; use Ada.Strings.Maps.Constants; -- ça aussi.
  10.  
  11. procedure sommelettres is
  12.        
  13.     tableLettres : String := " abcdefghijklmnopqrstuvwxyz"; -- la liste des lettres dans l'ordre alpha
  14.     motEntre : String(1..255);   -- le mot entré devra être compris entre 1 et 255 lettres
  15.     i : Integer;
  16.     len : Integer;
  17.     somme : Integer := 0;
  18.    
  19.     procedure drawLongLine(lineLength : Integer)    is -- permet de tracer les lignes du tableau récap.
  20.         k :Integer := 0;
  21.     begin
  22.         put(" |");
  23.         while(k < lineLength) loop
  24.             if((k+1) < lineLength) then
  25.                 put("----");
  26.             else
  27.                 put("---");
  28.             end if;
  29.             k := k + 1;
  30.         end loop;
  31.         put('|');
  32.         new_line;
  33.     end drawLongLine;
  34.    
  35.     procedure drawSpaces(nbSpaces : Integer) is -- écrit n "espaces"
  36.         m : Integer;
  37.     begin
  38.    
  39.         m := 0;
  40.         while(m < nbSpaces) loop
  41.             put(' ');
  42.             m := m + 1;
  43.         end loop;  
  44.    
  45.     end drawSpaces;
  46.    
  47.     procedure drawSep(spaces : Integer) is -- écrit un "|" entouré de n "espaces"
  48.  
  49.     begin
  50.         drawSpaces(spaces);
  51.         put('|');
  52.         drawSpaces(spaces);
  53.     end drawSep;
  54.    
  55.  
  56.    
  57.     function getValeurLettre(c : Character) return Integer is -- retourne la valeur numérique d'une lettre
  58.         j : Integer := 1;
  59.         toReturn : Integer := 0;
  60.     begin
  61.         while(j <= tableLettres'Length) loop
  62.             if(c = tableLettres(j)) then
  63.                 toReturn := j - 1;
  64.             end if;
  65.             j := j+1;
  66.         end loop;      
  67.         return toReturn;
  68.        
  69.     end getValeurLettre;
  70.    
  71. begin
  72.    
  73.     put("Entrez le mot a calculer : ");
  74.     get_line(motEntre, len);                -- le mot entré est stocké dans "motEntre" et sa taille l'est dans "len"
  75.    
  76.     motEntre := Translate(motEntre, Lower_Case_Map); -- la fonction "Translate" de "Ada.Strings.Fixed" permet de convertir en minuscules une chaîne de caractères.
  77.    
  78.     new_line;
  79.    
  80.     i := 1;
  81.     drawLongLine(len);
  82.     drawSep(1);
  83.     while (i <= len) loop -- écriture de la première ligne du tableau => lettres du mot
  84.        
  85.         put(motEntre(i));
  86.         drawSep(1);
  87.         i := i + 1;
  88.     end loop;
  89.    
  90.     new_line;
  91.     i := 1;
  92.     drawSep(1);
  93.     while (i <= len) loop -- écriture de la seconde ligne du tableau => valeurs des lettres du mot
  94.         somme := somme + getValeurLettre(motEntre(i)); -- calcul progressif de la somme
  95.         put(getValeurLettre(motEntre(i)), 0);
  96.         if(getValeurLettre(motEntre(i)) >= 10) then
  97.             put("| ");
  98.         else
  99.             drawSep(1);
  100.         end if;
  101.         i := i + 1;
  102.     end loop;
  103.     new_line;
  104.    
  105.     drawLongLine(len);
  106.    
  107.     new_line;
  108.     put("Somme : "); -- affichage de la somme en fin de script.
  109.     put(somme, 0)
  110.    
  111. end sommelettres;
Advertisement
Add Comment
Please, Sign In to add comment