/euler001/Euler001b.adb
Ada | 99 lines | 66 code | 12 blank | 21 comment | 6 complexity | 8d52b2a2265bc8c94d82296182e1c0e2 MD5 | raw file
- pragma License ( GPL );
- ------------------------------------------------------------------------------
- -- Author: darkestkhan --
- -- Email: darkestkhan@gmail.com --
- -- License: GNU GPLv3 or any later as published by Free Software Foundation --
- -- ( see license.txt file ) --
- -- Copyright  2011 darkestkhan --
- ------------------------------------------------------------------------------
- -- This Program is Free Software: You Can Redistribute It and/or Modify --
- -- It Under The Terms of The GNU General Public License As Published By --
- -- The Free Software Foundation, Either Version 3 of The License, or --
- -- (at Your Option) Any Later Version. --
- -- --
- -- This Program is Distributed in The Hope That It Will Be Useful, --
- -- But WITHOUT ANY WARRANTY; Without Even The Implied Warranty of --
- -- MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See The --
- -- GNU General Public License for More Details. --
- -- --
- -- You Should Have Received A Copy of The GNU General Public License --
- -- Along with This Program. if not, See <Http://Www.Gnu.Org/Licenses/>. --
- ------------------------------------------------------------------------------
- with Ada.Command_Line;
- with Ada.Task_Identification;
- with Ada.Text_IO;
- with TTY; -- it is in <https://github.com/darkestkhan/tty>
- with Ada.IO_Exceptions;
- procedure Euler001b is
- Argc: constant Integer := Ada.Command_Line.Argument_Count;
- Terminal: TTY.TTY;
- task type Calculate ( Up_To: Integer );
- type Calculate_Array is array ( Positive range <> ) of access Calculate;
- function Sum_Up_To ( Limit: in Integer ) return Integer is
- Sum: Natural := 0;
- I: Integer := 0;
- begin
- while I < Limit loop
- Sum := Sum + i;
- I := I + 3;
- end loop;
- I := 5;
- while I < Limit loop
- if I mod 3 /= 0 then
- Sum := Sum + I;
- end if;
- I := I + 5;
- end loop;
- return Sum;
- end Sum_Up_To;
- task body Calculate is
- Sum: Integer;
- begin
- Sum := Sum_Up_To ( Limit => Up_To );
- Terminal.Write_Line ( "The sum of numbers divisible by 3 or 5, but less than " & Up_To'img & " is " & Sum'img );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- exception
- when Constraint_Error => Terminal.Write ( "The sum in case of " & Up_To'img & " is greater than integer range" );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- end Calculate;
- function Create ( Up_To: Integer ) return access Calculate is
- begin
- return new Calculate ( Up_To => Up_To );
- end Create;
- task type Creator;
- task body Creator is
- Limit: Integer;
- Calculators: Calculate_Array ( 1 .. Argc );
- begin
- if Argc = 0 then
- Terminal.Write ( "Usage: " & Ada.Command_Line.Command_Name & " int [int] [int] ... " );
- Ada.Task_Identification.Abort_Task ( Ada.Task_Identification.Current_Task );
- end if;
- for Index in 1 .. Argc loop
- Integer'Value ( Ada.Command_Line.Argument ( Index ) );
- -- NOTE if given too big argument will raise Ada.IO.Exceptions.Data_Error;
- if Limit < 1 then
- Terminal.Write ( "I can sum only numbers greater than 0 and you gave " & Limit'img );
- else
- Calculators ( Index ) := Create ( Up_To => Limit );
- end if;
- end loop;
- exception
- when Ada.IO_Exceptions.Data_Error => Ada.Text_IO.Put_Line ( "One of given arguments was too big. Aborting. Aborting." );
- end Creator;
- God: Creator;
- begin
- null;
- end Euler001b;