2.3. Value of Numbers: separators, bases... - JulTob/Ada GitHub Wiki
We can represent numerics values in different ways.
v: Integer;
...
v := 1000 -- A thousand
v := 1_000; -- Same with a separator
-- Bases
v := 10#255#; -- Base 10, 255
v := 2#1111_1111#; -- Base 2, 255
v := 8#377# -- Base 8, 255
v := 16#FF# -- Base 16 255
La entrada/salida de enteros con signo se hace instanciando la librería genérica Integer_IO (anidada en la Ada.Text_IO), y la de enteros modulares instanciando la librería genérica Modular_IO (anidada también en la Ada.Text_IO). En ambos casos hay que especificar en la instancia el tipo concreto de los elementos que se van a leer o escribir (Num en la especificación). Para valores de tipo Integer y sus subtipos se puede usar, sin necesidad de instanciación, el paquete no genérico Ada.Integer_Text_IO.
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Text_IO; use Text_IO;
procedure prueba is
type Entero_Corto is range -256 .. 255;-- Entero con signo (de -256 a 255)
type Mod_Byte is mod 256; -- Entero modular (con módulo 256,
-- valores de 0 a 255)
package Entero_Corto_IO is new Integer_IO (Entero_Corto);
package Mod_Byte_IO is new Modular_IO (Mod_Byte);
use Entero_Corto_IO, Mod_Byte_IO;
...
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Text_IO; use Text_IO;
...
A : Integer := 255;
F : File_Type;
...
Get (A); -- Lee un valor Integer en la variable A
Get (F, A); -- Lee un valor Integer del fichero F en la variable A
Get (A, 5); -- Lee un valor Integer en la variable A.
-- Se toman exactamente 5 caracteres de la entrada
...
-- procedure Put(File : in File_Type; Item : in Num; Width : in Field := Num'Width; Base : in Number_Base := 10);
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Text_IO; use Text_IO;
...
A : Integer := 255;
F : File_Type;
...
Put (A); -- Escribe | 255| en la salida estándar
Put (F, A); -- Escribe | 255| en el fichero F
Put (F, 125); -- Escribe | 125| en el fichero F
Put (255, Width => 6); -- Escribe | 255| en la salida estándar
Put (255, Base => 2); -- Escribe |2#11111111#| en la salida estándar
Put (255, Width => 0); -- Escribe |255| en la salida estándar
Put (255, Width => 10, Base => 16);
-- Escribe | 16#FF#| en la salida estándar
...
Put
Escribe el literal del valor representado por el parámetro Item.
Puede lanzar las excepciones Mode_Error, Status_Error y Layout_Error.
La entrada/salida de valores reales en coma flotante se realiza instanciando la librería Float_IO (anidada en Text_IO); la de valores reales en coma fija, instanciando la librería Fixed_IO (anidada en Text_IO); y la de valores reales decimales, instanciando la librería Decimal_IO (anidada en Text_IO). En los tres casos hay que especificar en la instancia el tipo concreto de los elementos que se van a leer o escribir (Num en la especificación). Para valores de tipo Float y sus subtipos se puede usar, sin necesidad de instanciación, el paquete no genérico Ada.Float_Text_IO.
generic
type Num is digits <>;
package Float_IO is...
generic
type Num is delta <>;
package Fixed_IO is...
generic
type Num is delta <> digits <>;
package Decimal_IO is...
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Text_IO; use Text_IO;
procedure prueba is
-- Coma flotante
type Real is digits 8;
type Real_Corto is digits 5 range -1.0 .. 1.0;
-- Coma fija
type Real_Fijo is delta 0.125 range -100000.0 .. 100000.0; -- ordinaria
type Tipo_Dec is delta 0.01 digits 6 range -1000.00..1000.00; -- decimal
-- Instanciaciones
package Real_IO is new Float_IO (Real);
package Real_Fijo_IO is new Fixed_IO (Real_Fijo);
package Tipo_Dec_IO is new Decimal_IO (Tipo_Dec);
use Real_IO, Real_Fijo_IO, Tipo_Dec_IO;
...
Get
-- procedure Get(File : in File_Type; Item : out Num; Width : in Field := 0);
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Text_IO; use Text_IO;
...
A : Float;
F : File_Type;
...
Get (A); -- Lee un valor Float en la variable A
Get (F, A); -- Lee un valor Float del fichero F en la variable A
Get (A, 5); -- Lee un valor Float en la variable A.
-- Se toman exactamente 5 caracteres de la entrada
...
Put
procedure Put( File: in File_Type; Item: in Num; Fore: in Field := Num'Fore, Aft : in Field := Num'Aft; Exp : in Field := 0)
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Text_IO; use Text_IO;
...
A : Float := 1352.345;
F : File_Type;
...
Put (A); -- Escribe | 1.35234E+03| en la salida estándar
Put (1352.345, Fore => 6, Aft => 2); -- Escribe | 1.35E+03| en la salida estándar
Put (F, A, Aft => 2); -- Escribe | 1.35E+03| en el fichero F
Put (1352.345, Exp => 0); -- Escribe |1352.34497| en la salida estándar
Put (1352.345, Aft => 3, Exp => 0); -- Escribe |1352.345| en la salida estándar
...