Fiche Traduction Algo->ADA

Transcription

Fiche Traduction Algo->ADA
Traduction de la notation algorithmique en Ada
Dans la colonne de gauche, la notation algorithmique, dans la colonne de droite, les traductions Ada
proposées.
Bibliothèques utiles
Pour des entrées/sorties sur des textes :
with Ada.Text_IO; with Textes;
use Ada.Text_IO; use Textes;
Pour des entrées/sorties sur des entiers :
with Ada.Integer_Text_IO;
with Ada.Text_IO;
use Ada.Integer_Text_IO;
use Ada.Text_IO;
Pour des entrées/sorties sur des réels :
with Ada.Float_Text_IO; with Ada.Text_IO;
use Ada.Float_Text_IO; use Ada.Text_IO;
Pour utiliser des pointeurs :
with Memoire;
Pour utiliser des fonctions mathématiques :
with Ada.Numerics,
Ada.Numerics.Elementary_Functions;
use Ada.Numerics,
Ada.Numerics.Elementary_Functions;
Commentaires
{ ceci est un commentaire }
-- ceci est un commentaire
Constantes
LMAX : constante 50 de type entier
LMAX : constant Integer := 50
PI : constante 3.14 de type réel
PI : constant Float := 3.14
Types simples
entier
Integer
entier ≥ 0
Natural
entier > 0
Positive
réel
Float
caractère
Character
booléen
Boolean { les valeurs sont true et false }
Types construits, déclaration de types
T : type ...
type T is ... ;
T : type tableau sur [1..LMAX] de X
type T is array (1..LMAX) of X;
T : type < a : Entier, b : Réel >
type T is record
a : Integer;
b : Float;
end record;
T : type [A, B, ...]
type T is (A, B, ...);
T : type Entier sur [1..LMAX]
subtype T is Integer range 1..LMAX;
T : type Caractère sur [’A’..’Z’]
subtype T is Character range ’A’..’Z’;
UJF - UFR IM2 AG - Licence S&T - INF231 P. Habraken, C. Parent-Vigouroux, P.C. Scholl - juil. 2013 p. 1
Déclaration de variables
type T is ...
x : T;
ch : array (1..N) of Character;
ou bien
ch : String(1..N);
a, b, c : Entier
a, b, c : Integer;
d : Entier ≥ 0
d : Natural;
i : Entier sur [1..LMAX+1]
i : Integer range 1..LMAX+1;
Conversion de types
f : réel ; n : entier
f : Float; n : Integer
f ←− n
f := float(n);
Accès aux n-uplets
T : type < a : Booléen, b : Réel >
type T is record a : Boolean; b : Float;
end record;
toto : T
toto : T;
toto.a ←− vrai
toto.a := true;
toto ←− < faux, 13.7 >
toto := (false, 13.7);
Accès aux tableaux
T : type tableau sur [1..N] de Réel
type T is array (1..N) of Float;
r1, r2 : T
r1, r2 : T;
i : Entier sur 1..N
i : Integer range 1..N;
r1[i] ou r1i
r1(i)
Tableaux non contraints (pour spécifier en paramètres)
TabE : type tableau sur [*..*] d’entier
type TabE is array (integer range <>) of
Integer;
Opérateurs courants
←−
:=
=, 6=
=, /=
<, >, ≤, ≥
<, >, <=, >=
et, ou, non
and, or, not
et puis, ou alors
and then, or else
+, -, *, /, reste
+, -, *, /, mod
Fonctions mathématiques
f, g : Réel
f, g : Float;
...
...
f ←− Racine_Carree(g)
f := Sqrt(g);
f ←− Ln(e)
f := Log(E);
f ←− Log10 (g)
f := Log(g, 10);
f ←− Sinus(π / 2) { angle en radians }
f := Sin(PI / 2);
f ←− Cosinus(45) { angle en degrés } ...
f := Cos(45.0, 360.0); ...
Compositions d’actions
{ rien } c.a.d. l’action vide
null
si C alors A1 sinon A2
if C then A1; else A2; end if;
pour i allant de 1 à N : A
for i in 1..N loop A; end loop;
pour i allant de 1 à N, pas -1 : A
for i in reverse 1..N loop A; end loop;
tantque C : A
while C loop A; end loop;
répéter A jusqu’à C
loop A; exit when C; end loop;
T : type . . .
x:T
ch : tableau sur [1..N] de caractère
UJF - UFR IM2 AG - Licence S&T - INF231 P. Habraken, C. Parent-Vigouroux, P.C. Scholl - juil. 2013 p. 2
Structure selon avec conditions générales
T1, T2 : type
type T1 is ... ; type T2 is ... ;
x : T1, y : T2
x : T1; y : T2;
...
...
selon x, y :
if C1(x, y) then null;
C1(x, y) : { rien }
elsif C2(x, y) then A2; ...
C2(x, y) : A2 . . .
else A4;
sinon : A4
end if;
Structure selon avec conditions de la forme x = constante
T : type [V1, V2, V3, ... ]
type T is (V1, V2, V3, ... );
x:T
x : T;
...
...
selon x :
case x is
x = V1 : A1
when V1 => A1;
x = V2 : A2
when V2 => A2;
x = V3 : A3
when V3 => A3;
sinon : A4
when others => A4;
end case;
Entrées et Sorties sur les entiers
x, y : entier
x, y : Integer;
...
...
Lire(x) ; Lire(y)
Get(x); Get(y);
Ecrire(x)
Put(x);
Ecrire(y)
Put(y, 5); { 5 caractères affichés }
Entrées et Sorties sur les réels
x, y : réel
x, y : Float;
...
...
Lire(x) ; Lire(y)
Get(x); Get(y);
Ecrire(x)
Put(x); { format ee.dddddE±xx }
Ecrire(y)
Put(y, 3, 3, 0); { format eee.ddd }
Entrées et Sorties sur caractères
c : caractère
c : Character;
...
...
Lire(c)
Get(c); Skip_Line;
Ecrire(c)
Put(c);
ALaLigne
New_Line;
Entrées et Sorties sur chaînes de caractères
CH : texte
LMAX : constant Integer := 256;
...
CH : String(1..LMAX); L : Integer;
Lire(CH)
...
Ecrire(CH) ; ALaLigne
Get_Line(CH,L); { L longueur explicite }
Ecrire("Hello World") ; ALaLigne
Put_Line(CH(1..L));
Put_Line("Hello World");
{ voir aussi la bibliothèque Textes }
UJF - UFR IM2 AG - Licence S&T - INF231 P. Habraken, C. Parent-Vigouroux, P.C. Scholl - juil. 2013 p. 3
Fonctions et Actions - Spécification
type T is ... ;
type T’ is ... ;
type T” is ... ;
0
00
f : fonction (X : T, y : T ) −→ T
function f(x : T; y : T’) return T”;
procedure a(x : in T;
a : action (donnée x : T,
0
y : out T’;
résultat y : T ,
00
z : in out T”);
donnée-résultat z : T )
0
00
u : T; v : T’; w : T”;
u : T; v : T ; w : T
Fonctions et Actions - Réalisation
f(x, y) :
function f(x : T; y : T’) return T” is
00
m:T
m : T”;
begin
...
...
retour : m
return m ;
end f;
...
...
w := f(u, v);
w ←− f(u, v)
a(x, y, z) :
procedure a(x : in T;
y ←− x
y : out T’;
z ←− x + z
z : in out T”) is
begin
y := x;
z := x + z;
end a;
...
...
a(u, v, w);
a(u, v, w)
Manipulation de pointeurs
Cel : type ...
type Cel is ... ;
T : type pointeur de Cel
type T is access Cel;
p:T
p : T;
NIL
Null
p↑
p.all
p↑.suc
p.all.suc
Listes chaînées
Cel : type
type Cel;
AdCel : type pointeur de Cel
type AdCel is access Cel;
Cel : type < e : Entier,
type Cel is record
suc : AdCel >
e : Integer;
suc : Adcel;
end record;
T, T0 , T00 : type
UJF - UFR IM2 AG - Licence S&T - INF231 P. Habraken, C. Parent-Vigouroux, P.C. Scholl - juil. 2013 p. 4
Cellule : type ...
AdCellule : type pointeur de Cellule
p : AdCellule
Allouer(p)
...
Liberer(p) ;
Gestion mémoire
with Memoire;
...
type Cellule is ... ;
type AdCellule is access Cellule ;
...
package Allocation_Cellule is
new Memoire(Cellule, AdCellule);
use Allocation_Cellule;
...
p : AdCellule;
allouer(p);
...
liberer(p);
UJF - UFR IM2 AG - Licence S&T - INF231 P. Habraken, C. Parent-Vigouroux, P.C. Scholl - juil. 2013 p. 5