with Ada.Text_Io;
use Ada;
with Ada.Integer_Text_Io;
procedure Main is
-- Ici on définit l'intervale dans lequel on accepte la somme maximale.
type This_Float_Type is new Float range -1_000_000.0..1_000_000.0;
-- on defini un sous type local.
type Sub_Sum_Type is new This_Float_Type Range This_Float_type'First..This_Float_type'Last;
--Pour les entrée sorties textuelle de valeurs du type local.
package Sub_Sum_io is new Ada.Text_Io.Float_Io(Sub_Sum_Type);
-- le minimum d'addition possible.
Fraction_First : Sub_Sum_Type := 10.0;
-- un sous-type représentant l'intervale de la fraction maximale sommable.
subtype Sub_Total_Type is Sub_Sum_Type range -(Sub_Sum_Type'last/Fraction_first)..Sub_Sum_Type'last/Fraction_First;
-- la fraction maximale sommable.
Fraction_Total_Max : Sub_Sum_Type := Sub_Total_Type'Last;
function Self (Operator : in String; Total : in Sub_Sum_Type; Sub_Total : in Sub_Total_Type) return Sub_Sum_Type is
Current_Char : Natural := 0;
begin
if Operator ' Length > 0 then
Current_Char := Operator'First;
else
return Total + 0.0;
end if;
-- la c'est en prevision de la suite du code.
if Operator'Last > Operator'First then
for I in Operator'First+1..Operator'Last loop
case Operator(Current_Char) is
when '+' =>
if (((-(Total) - Sub_Total) + Sub_Sum_Type'Last/Fraction_first) < 0.0) then
return Self (Operator(Operator'First..I), Total, Sub_Sum_Type'min(Sub_Total_Type'Last, Sub_Total - Sub_Total / Fraction_first));
else
return Sub_Total;
end if;
when '=' =>
return TOtal + 0.0;
when others =>
null;
end case;
end loop;
else
-- ici on traite notre cas à un caractère dans l'opérateur attendu.
case Operator(Current_Char) is
when '+' =>
-- Si l'addition dépasse on r'appelle la fonction avec une sous-fraction de fraction.
if (((-(Total) - Sub_Total) + Sub_Sum_Type'Last) < 0.0) then
return Self (Operator, Total, Sub_Sum_Type'min(Sub_Total_Type'Last, Sub_Total - Sub_Total / Fraction_first));
else
-- Si non.. Si ça passe on retounre le sous-total sommable.
return Sub_Total;
end if;
when '=' =>
return Total + 0.0;
when others =>
null;
end case;
end if;
return 0.0;
end Self;
Sum : Sub_Sum_Type := 0.0;
Sub_Total : Sub_Total_Type := 501.0;
-- Test.
begin
Text_Io.Put_line("Initialization..." );
Text_Io.Put("Operator '=' :" );
sub_Sum_Io.Put(Self("=", Sum, Sub_Total));
Text_Io.Put("; Operator '+' :" );
sub_Sum_Io.Put(Self("+", Sum, Sub_Total));
while not (Sum >= 1000_000.0) loop
Text_Io.New_Line;
Text_Io.Put("Operator '=' :" );
Sum := Self("=", Sum, Sub_Total);
sub_Sum_Io.Put(Sum);
Text_Io.Put("; Operator '+' :" );
Sub_Total := Self("+", Sum, Sub_Total);
sub_Sum_Io.Put(Sub_Total);
Text_Io.Put("; Total : " );
Sum := Sum + (Sub_Total);
sub_Sum_Io.Put(sum);
end loop;
end Main;