Advertisement
Guest User

Untitled

a guest
Oct 12th, 2010
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 4.04 KB | None | 0 0
  1. --- libraries
  2. WITH Ada.Text_IO; USE Ada.Text_IO;
  3. WITH Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  4.  
  5. --- main block
  6. PROCEDURE Zadanie2 IS
  7. --- constants
  8.    Lenght_Date_Const: CONSTANT Positive := 11;
  9.    --- variables
  10.    Date: String(1..Lenght_Date_Const);
  11.    Lenght_Date: Integer :=0;
  12.    
  13.    type DateRecord is
  14.       record
  15.          Day: Integer := 0; --pointer to be used to check correctness of given day (range 1-31)
  16.          Month: Integer := 0;   --pointer to be used to check correctness of given month (range 1-12)
  17.          Year: Integer :=0; --pointer containing the year (last for chars from string)
  18.       end record;
  19.    
  20.    type DatePtr is access DateRecord;   DatePtr1: DatePtr;
  21.    
  22.    --- function block
  23.    function CheckDate ( Date: in String) return Boolean is
  24.    begin
  25.       -- here date will be splitted using '-' as regexp and than all three parts will be checked for their correctness
  26.       DatePtr1 := new DateRecord;
  27.       DatePtr1.Day := Integer'Value(Date(Date'First..Date'First+1));
  28.       DatePtr1.Month := Integer'Value(Date(Date'First+3..Date'First+4));
  29.       DatePtr1.Year := Integer'Value(Date(Date'First+6..Date'First+9));
  30.      
  31.       if (DatePtr1.Day <= 0 or else DatePtr1.Day > 31) then
  32.          return false;
  33.       elsif (DatePtr1.Month <= 0 or else DatePtr1.Month > 12) then
  34.          return false;
  35.       end if;
  36.      
  37.       return true;
  38.    end CheckDate;
  39.  
  40.    function isLeap ( year : in Integer) return Boolean is
  41.    begin
  42.       if ( ( (
  43.               ( year mod 4 ) = 0 ) AND ( ( year mod 100 ) /= 0 ) )
  44.           or else ( ( year mod 400 ) = 0 ) ) then
  45.          return true;
  46.       end if;
  47.       return false;     --returns false if above condition is not true
  48.    end isLeap;
  49.    
  50.    FUNCTION Tommorow ( date_ptr : in DatePtr ) RETURN String IS
  51.    BEGIN
  52.       -- this function will calculate the tommorow day along with checking conditions for leap year and checking if we have the last day of month, of year
  53.       -- and so on
  54.       -- checking Leap Year Condition
  55.       if ( isLeap(date_ptr.Year) and date_ptr.Month = 2 and date_ptr.Day = 29) then
  56.          -- so the year is leap, February is present, and there is the last day of it
  57.          date_ptr.Month := date_ptr.Month + 1;
  58.          date_ptr.Day := 1;
  59.          -- missing returning date
  60.       else
  61.          -- the year is not leap
  62.          -- two conditions
  63.          -- 1. for even months
  64.          -- 2. for odd months
  65.          if date_ptr.Month mod 2 = 0 then   --month is even -> has 30 days
  66.             if date_ptr.Day >= 1 or else date_ptr.Day <30 then
  67.                date_ptr.Day := date_ptr.Day + 1;
  68.             else                --the last day of month
  69.                date_ptr.Day := 1;
  70.                if date_ptr.Month = 12 then      --December so year must be incremented also
  71.                   date_ptr.Month := 1;
  72.                   date_ptr.Year := date_ptr.Year + 1;
  73.                else             --Any other even month different than December
  74.                   date_ptr.Month := date_ptr.Month + 1;
  75.                end if;
  76.             end if;
  77.          else                   -- month is odd -> has 31 days
  78.             if date_ptr.Day >= 1 or else date_ptr.Day <31 then
  79.                date_ptr.Day := date_ptr.Day + 1;
  80.             else                --the last day of month
  81.                date_ptr.Day := 1;
  82.                date_ptr.Month := date_ptr.Month + 1;
  83.             end if;
  84.          end if;
  85.          return ("dd-mm-rrrr");     --here should be date concated from scraps (date_ptr.Day etc)
  86.       end if;
  87.    END Tommorow;
  88.  
  89. BEGIN
  90.    Put_Line("Hello - program zadanie6");
  91.    Put("Podaj date, pamietajac ze program akceptuje date w formacie dd-mm-rrrr");
  92.    New_Line(3);
  93.    Put("Data ->"); Get_Line(date,lenght_date);
  94.    New_Line;
  95.    Put("Podales date -> "); Put(Date(1..Lenght_Date));
  96.    New_Line(2); Put("-----------------------"); New_Line(2);
  97.    IF CheckDate(Date) THEN
  98.       Put_Line("Podales prawidlowa date");
  99.       Put("Jutro bedziemy mieli wiec ->"); Put(Tommorow(DatePtr1));
  100.    ELSE
  101.       Put_Line("Data nieprawidlowa, koniec programu");
  102.    END IF;
  103. END;
  104. --- end of main block
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement