Kod: Zaznacz cały
--- libraries
WITH Ada.Text_IO; USE Ada.Text_IO;
WITH Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
--- main block
PROCEDURE Zadanie2 IS
--- constants
Lenght_Date_Const: CONSTANT Positive := 11;
--- variables
Date: String(1..Lenght_Date_Const);
Lenght_Date: Integer :=0;
type DateRecord is
record
Day: Integer := 0; --pointer to be used to check correctness of given day (range 1-31)
Month: Integer := 0; --pointer to be used to check correctness of given month (range 1-12)
Year: Integer :=0; --pointer containing the year (last for chars from string)
end record;
type DatePtr is access DateRecord; DatePtr1: DatePtr;
--- function block
function CheckDate ( Date: in String) return Boolean is
begin
-- here date will be splitted using '-' as regexp and than all three parts will be checked for their correctness
DatePtr1 := new DateRecord;
DatePtr1.Day := Integer'Value(Date(Date'First..Date'First+1));
DatePtr1.Month := Integer'Value(Date(Date'First+3..Date'First+4));
DatePtr1.Year := Integer'Value(Date(Date'First+6..Date'First+9));
if (DatePtr1.Day <= 0 or else DatePtr1.Day > 31) then
return false;
elsif (DatePtr1.Month <= 0 or else DatePtr1.Month > 12) then
return false;
end if;
return true;
end CheckDate;
function isLeap ( year : in Integer) return Boolean is
begin
if ( ( (
( year mod 4 ) = 0 ) AND ( ( year mod 100 ) /= 0 ) )
or else ( ( year mod 400 ) = 0 ) ) then
return true;
end if;
return false; --returns false if above condition is not true
end isLeap;
FUNCTION Tommorow ( date_ptr : in DatePtr ) RETURN String IS
BEGIN
-- 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
-- and so on
-- checking Leap Year Condition
if ( isLeap(date_ptr.Year) and date_ptr.Month = 2 and date_ptr.Day = 29) then
-- so the year is leap, February is present, and there is the last day of it
date_ptr.Month := date_ptr.Month + 1;
date_ptr.Day := 1;
-- missing returning date
else
-- the year is not leap
-- two conditions
-- 1. for even months
-- 2. for odd months
if date_ptr.Month mod 2 = 0 then --month is even -> has 30 days
if date_ptr.Day >= 1 or else date_ptr.Day <30 then
date_ptr.Day := date_ptr.Day + 1;
else --the last day of month
date_ptr.Day := 1;
if date_ptr.Month = 12 then --December so year must be incremented also
date_ptr.Month := 1;
date_ptr.Year := date_ptr.Year + 1;
else --Any other even month different than December
date_ptr.Month := date_ptr.Month + 1;
end if;
end if;
else -- month is odd -> has 31 days
if date_ptr.Day >= 1 or else date_ptr.Day <31 then
date_ptr.Day := date_ptr.Day + 1;
else --the last day of month
date_ptr.Day := 1;
date_ptr.Month := date_ptr.Month + 1;
end if;
end if;
return ("dd-mm-rrrr"); --here should be date concated from scraps (date_ptr.Day etc)
end if;
END Tommorow;
BEGIN
Put_Line("Hello - program zadanie6");
Put("Podaj date, pamietajac ze program akceptuje date w formacie dd-mm-rrrr");
New_Line(3);
Put("Data ->"); Get_Line(date,lenght_date);
New_Line;
Put("Podales date -> "); Put(Date(1..Lenght_Date));
New_Line(2); Put("-----------------------"); New_Line(2);
IF CheckDate(Date) THEN
Put_Line("Podales prawidlowa date");
Put("Jutro bedziemy mieli wiec ->"); Put(Tommorow(DatePtr1));
ELSE
Put_Line("Data nieprawidlowa, koniec programu");
END IF;
END;
--- end of main block
Rozwiązałem problem dzieląc podany parametr ,,string'' (ponieważ w takim formacie jest podawana data) na części i zapisując te części do zmiennych typu ,,Integer'', niemniej jednak nie mogłem znaleźć w Google sposobu powrotu ponownie do String, który chciałbym wyświetlić na ekranie
Oczywiście znalazłem funkcję ,,concate'', która mi połączy kilka różnych danych typu ,,string'', ale to co nie działa to samo przetworzenie (jeśli mogę się tak wyrazić) typu ,,Integer'' do ,,String''.
Ma ktoś jakiś pomysł?