12 mai 2007
Transformer un unbounded_string en string et vice versa
Il existe deux fonctions, "To_string" et "To_Unbounded_String" dans le paquetage Ada.Strings.Unbounded
function To_String (source : Unbounded_String) return String;
function To_Unbounded_String (source : String) return Unbounded_String;
Utilisation :
.....
My_string : String := "";
My_UString : unbounded_string;
.....
begin
My_String := Ada.Strings.Unbounded.To_String(UString);
My_UString := Ada.Strings.Unbounded.To_Unbounded_String(String);
.....
Renvoyer la longueur d'une unbounded_string
Utiliser la fonction "Length" du paquetage Ada.Strings.Unbounded qui renvoie une String
function Length (Source : Unbounded_String) return Natural;
Utilisation :
U_Chaine : unbounded_string;
Chaine : string := "";
.....
begin
U_Chaine := "12345+6789";
Chaine := Ada.Strings.Unbounded.Length(U_Chaine);
.....
Renvoyer une partie d'une unbounded_string
Utiliser la fonction "Slice" du paquetage Ada.Strings.Unbounded qui renvoie une String
function Slice (Source : Unbounded_String;
Low : Positive;
High : Natural) return String;
Utilisation :
U_Chaine : unbounded_string;
Chaine : string := "";
.....
begin
U_Chaine := "12345+6789" -- récupérons "5+6"
Chaine := Ada.Strings.Unbounded.Slice(U_Chaine, 5, 7);
.....
28 avril 2007
Récupérer un Unbounded_String dans un String sans effet de contrainte
Le problème, lorsque j'ai voulu transformer la chaine contenue dans un Unbounded_string en String, était qu'il fallait contraindre la longueur du string.
Résultat, il fallait saisir une chaine de longueur égale à la contrainte.
Je propose donc, une méthode pour qu'il n'y ait pas de contrainte à indiquer et qu'il soit possible de récupérer la chaine unbounded, quelque soit sa longueur.
Voici le code :
with Ada.Text_Io, Ada.Integer_Text_Io, Ada.Strings.Unbounded, Ada.Strings.Unbounded.Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io, Ada.Strings.Unbounded, Ada.Strings.Unbounded.Text_Io;
procedure Text_Atu is
Chaine : Unbounded_String;
Long_Chaine : Integer;
package Fonctions is
function Explode (Ch : Unbounded_String ) return Integer;
function Contrainte_Chaine (Ch : Unbounded_String ) return String;
end Fonctions;
package body Fonctions is
function Explode (Ch : Unbounded_String ) return Integer is
Long : Integer;
begin
Long := Ada.Strings.Unbounded.Length(Ch);
return Long;
end Explode;
function Contrainte_Chaine (Ch : Unbounded_String ) return String is
Long : Integer := Explode (Ch);
New_Chaine : String (1 .. Long);
begin
New_Chaine := Ada.Strings.Unbounded.To_String(Ch);
return New_Chaine;
end Contrainte_Chaine;
end Fonctions;
use Fonctions;
begin
Chaine := Ada.Strings.Unbounded.Text_Io.Get_Line;
Put("Vous avez saisi : " );
Ada.Strings.Unbounded.Text_Io.Put(Chaine);
New_Line;
Long_Chaine := Explode(Chaine);
Put("La longueur de la chaine est de : ");
Put(Long_Chaine);
New_Line;
Put("Chaine transformee : " );
Put(Contrainte_Chaine(Chaine));
end Text_Atu;
22 avril 2007
Fonctions trigonométriques
Pour utiliser les fonctions trigonométriques, il faut observer deux choses :
1/ il existe dans le paquetage Ada.Numerics.Elementary_Functions, les fonctions trigonométriques de base suivantes :
function Sin (X, Cycle : Float_Type'Base) return Float_Type'Base;
function Cos (X, Cycle : Float_Type'Base) return Float_Type'Base;
function Tan (X, Cycle : Float_Type'Base) return Float_Type'Base;
X correspond à l'angle (ex: 90.0 pour 90°) et Cycle correspond à une quantité de révolution du cercle trigonométrique (1 tour complet = 360.0).
2/ il existe également les fonctions suivantes :
function Sin (X : Float_Type'Base) return Float_Type'Base;
function Cos (X : Float_Type'Base) return Float_Type'Base;
function Tan (X : Float_Type'Base) return Float_Type'Base;
mais là je n'ai pas réussi à les faire fonctionner.
21 avril 2007
Transformer une chaine de caractères en entier
Pour transformer une chaine de caractères (String) en entier (Integer), je vous propose cette formule
Nombre : Constant Integer := Integer'Value(String);
Type string
Le type "String" est obligatoirement contraint.
Il existe un moyen pour faire en sorte qu'il ne le soit pas; utiliser le type
unbounded_string
Vous pouvez jeter un oeil dans le paquetage Ada.Strings.Unbounded





