Voila un coder de morse en ADA, il permet d' illuistrer simplement comment fonctionne ce langage, qui s'avere etre a mon avis interessant mais un peu contraignant au niveau de la programmation. Pour la source voyez la suite ...
--| Programme de codage en Morse
--| Entree : un texte termine par '$'
--| Sortie : le codage en Morse du texte
with Ada.Text_IO; use Ada.Text_IO;
procedure Coder is
Dernier : constant Character := '$'; -- fin du texte en entree
-- Lecture de caracteres, avec remplacement d'une fin de ligne
-- par un espace
procedure Lire (C : out Character) is
begin
if End_Of_Line then
Skip_Line;
C := ' ';
else
Get (C);
end if;
end Lire;
-- Largeur supposee de l'ecran
Largeur : constant Positive := 80;
-- Taille max du code Morse d'un caractere, plus un espace
Taille_Max : constant Positive := 6 + 1;
-- Procedure qui passe eventuellement a la ligne
-- Pos est la positition d'ecriture sur la ligne courante
procedure A_La_Ligne (Pos : in out Natural) is
begin
if Pos + Taille_Max > Largeur then
New_Line;
Pos := 0;
end if;
end A_La_Ligne;
-- Procedure qui affiche S et gere la position d'affichage Pos
procedure Afficher (Pos : in out Natural;
S : in String) is
begin
Put (S);
Pos := Pos + S'Length;
end Afficher;
-- Procedure qui affiche le code Morse de C
-- et gere la position d'affichage Pos
procedure Afficher_Code (Pos : in out Natural;
C : in Character) is
begin
case C is
when 'A' => Afficher (Pos, ".-");
when 'B' => Afficher (Pos, "-...");
when 'C' => Afficher (Pos, "-.-.");
when 'D' => Afficher (Pos, "-..");
when 'E' => Afficher (Pos, ".");
when 'F' => Afficher (Pos, "..-.");
when 'G' => Afficher (Pos, "--.");
when 'H' => Afficher (Pos, "....");
when 'I' => Afficher (Pos, "..");
when 'J' => Afficher (Pos, ".---");
when 'K' => Afficher (Pos, "-.-");
when 'L' => Afficher (Pos, ".-..");
when 'M' => Afficher (Pos, "--");
when 'N' => Afficher (Pos, "-.");
when 'O' => Afficher (Pos, "---");
when 'P' => Afficher (Pos, ".--.");
when 'Q' => Afficher (Pos, "--.-");
when 'R' => Afficher (Pos, ".-.");
when 'S' => Afficher (Pos, "...");
when 'T' => Afficher (Pos, "-");
when 'U' => Afficher (Pos, "..-");
when 'V' => Afficher (Pos, "...-");
when 'W' => Afficher (Pos, ".--");
when 'X' => Afficher (Pos, "-..-");
when 'Y' => Afficher (Pos, "-.--");
when 'Z' => Afficher (Pos, "--..");
when '1' => Afficher (Pos, ".----");
when '2' => Afficher (Pos, "..---");
when '3' => Afficher (Pos, "...--");
when '4' => Afficher (Pos, "....-");
when '5' => Afficher (Pos, ".....");
when '6' => Afficher (Pos, "-....");
when '7' => Afficher (Pos, "--...");
when '8' => Afficher (Pos, "---..");
when '9' => Afficher (Pos, "----.");
when '0' => Afficher (Pos, "-----");
when '.' => Afficher (Pos, ".-.-.-");
when ',' => Afficher (Pos, "--..--");
when '?' => Afficher (Pos, "..--..");
when '/' => Afficher (Pos, "-..-.");
when '+' => Afficher (Pos, ".-.-.");
when ''' => Afficher (Pos, ".----.");
when '=' => Afficher (Pos, "-....-");
when ' ' => Afficher (Pos, " "); -- 1 avant et 1 apres -> 3
when Dernier => Afficher (Pos, "...-.-");
when others => null; -- Cas impossible !
end case;
end Afficher_Code;
Lu : Character;
Pos : Natural := 0;
begin -- Coder
loop
Lire (Lu);
A_La_Ligne (Pos);
Afficher_Code (Pos, Lu);
Afficher (Pos, " "); -- on met toujours un espace apres un code
exit when Lu = Dernier;
end loop;
end Coder;
|