Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Uppgiftb; use Uppgiftb;
- with Ada.Text_IO; use Ada.Text_IO;
- procedure Uppgifta is
- Date, Next, Prev : Date_Type;
- begin
- loop
- begin
- Get(Date);
- Put(Date);
- Next := Next_Date(Date);
- Put(Next);
- Prev := Previous_Date(Date);
- Put(Prev);
- exit;
- exception
- when Year_Error =>
- Put_Line("Year must be 0 or higher");
- Skip_Line;
- when Month_Error =>
- Put_Line("Month must be between 1 and 12");
- Skip_Line;
- when Day_Error =>
- Put_Line("Day must be between 1 and the amount of days in month");
- when others =>
- Put_Line("Error does not compute"); Skip_Line;
- end;
- end loop;
- end Uppgifta;
- package body Uppgiftb is
- procedure Get(Date : out Date_Type) is
- C : Character;
- begin
- Get(Date.Year);
- if Date.Year < 0 then
- raise Year_Error;
- end if;
- Get(C);
- Get(Date.Month);
- if Date.Month <= 0 or Date.Month > 12 then
- raise Month_Error;
- end if;
- Get(C);
- Get(Date.Day);
- if Date.Month = 2 and Date.Day > 28 then
- raise Day_Error;
- elsif Date.Day <= 0 or Date.Day > 31 then
- raise Day_Error;
- elsif Date.Month = 4 or Date.Month = 6 or Date.Month = 9 or Date.Month = 11 then
- if Date.Day > 30 then
- raise Day_Error;
- end if;
- end if;
- end Get;
- procedure Put(Date : in Date_Type) is
- begin
- Put(Date.Year);
- Put("-");
- if Date.Month < 10 then
- Put("0");
- end if;
- Put(Date.Month, Width => 0);
- Put("-");
- if Date.Day < 10 then
- Put("0");
- end if;
- Put(Date.Day, Width => 0);
- end Put;
- procedure Next_Date(OG : in Date_Type; Date : out Date_Type) is
- begin
- Date := OG;
- if Date.Month = 2 then
- if Date.Day = 28 then
- Date.Day := 1;
- Date.Month := 3;
- else
- Date.Day := Date.Day + 1;
- end if;
- elsif Date.Month = 4 or Date.Month = 6 or Date.Month = 9 or Date.Month = 11 then
- if Date.Day = 30 then
- Date.Day := 1;
- Date.Month := Date.Month + 1;
- else
- Date.Day := Date.Day +1;
- end if;
- else
- if Date.Day = 31 then
- Date.Day := 1;
- Date.Month := Date.Month + 1;
- else
- Date.Day := Date.Day + 1;
- end if;
- if Date.Month = 13 then
- Date.Month := 1;
- Date.Year := Date.Year +1;
- end if;
- end if;
- end Next_Date;
- procedure Previous_Date(OG : in Date_Type; Date : out Date_Type) is
- begin
- Date := OG;
- if Date.Month = 3 then
- if Date.Day = 1 then
- Date.Day := 28;
- Date.Month := 2;
- else
- Date.Day := Date.Day - 1;
- end if;
- elsif Date.Month = 5 or Date.Month = 7 or Date.Month = 10 or Date.Month = 12 then
- if Date.Day = 1 then
- Date.Day := 30;
- Date.Month := Date.Month - 1;
- else
- Date.Day := Date.Day - 1;
- end if;
- else
- if Date.Day = 1 then
- Date.Day := 31;
- Date.Month := Date.Month - 1;
- else
- Date.Day := Date.Day - 1;
- end if;
- if Date.Month = 0 then
- Date.Month := 12;
- Date.Year := Date.Year - 1;
- end if;
- end if;
- end Previous_Date;
- function Next_Date(Date : in Date_Type) return Date_Type is
- Nxtdate : Date_Type;
- begin
- Next_Date(Date,Nxtdate);
- return Nxtdate;
- end Next_Date;
- function Previous_Date(Date : in Date_Type) return Date_Type is
- Predate : Date_Type;
- begin
- Previous_Date(Date,Predate);
- return Predate;
- end Previous_Date;
- function "<"(Left, Right : in Date_Type) return Boolean is
- begin
- if Left.Year = Right.Year then
- if Left.Month = Right.Month then
- if Left.Day < Right.Day then
- return True;
- else
- return False;
- end if;
- elsif Left.Month < Right.Month then
- return True;
- else
- return False;
- end if;
- elsif Left.Year < Right.Year then
- return True;
- else
- return False;
- end if;
- end "<";
- function ">"(Left, Right : in Date_Type) return Boolean is
- begin
- return Right < Left;
- end ">";
- function "="(Left, Right : in Date_Type) return Boolean is
- begin
- return not( Left < Right) and not (Left > Right);
- end "=";
- end Uppgiftb;
- with Ada.Text_IO; use Ada.Text_IO;
- with Altpaket; use Altpaket;
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- procedure Uppgiftf is
- Date1, Date2 : Date_Type;
- Count : Integer := 0;
- Efter: Boolean;
- begin
- Put("Mata in dagens datum: ");
- Get(Date1);
- Put("Mata in ett annat datum: ");
- Get(Date2);
- loop
- if Date2 > Date1 then
- Date1 := Next_Date(Date1);
- Count := Count + 1;
- if Date1 = Date2 then
- Efter := True;
- exit;
- end if;
- elsif Date2 < Date1 then
- Date1 := Previous_Date(Date1);
- Count := Count + 1;
- if Date1 = Date2 then
- Efter := False;
- exit;
- end if;
- end if;
- end loop;
- Put("Det andra datumet är ");
- Put(Count, Width => 0);
- if efter then
- Put(" dagar efter det första");
- else
- Put(" dagar innan det första");
- end if;
- end Uppgiftf;
- package body Altpaket is
- procedure Get(Date : out Date_Type) is
- C : Character;
- begin
- Get(Date(1));
- if Date(1) < 0 then
- raise Year_Error;
- end if;
- Get(C);
- Get(Date(2));
- if Date(2) <= 0 or Date(2) > 12 then
- raise Month_Error;
- end if;
- Get(C);
- Get(Date(3));
- if Date(2) = 2 and Date(3) > 28 then
- raise Day_Error;
- elsif Date(3) <= 0 or Date(3) > 31 then
- raise Day_Error;
- elsif Date(2) = 4 or Date(2) = 6 or Date(2) = 9 or Date(2) = 11 then
- if Date(3) > 30 then
- raise Day_Error;
- end if;
- end if;
- end Get;
- procedure Put(Date : in Date_Type) is
- begin
- Put(Date(1));
- Put("-");
- if Date(2) < 10 then
- Put("0");
- end if;
- Put(Date(2), Width => 0);
- Put("-");
- if Date(3) < 10 then
- Put("0");
- end if;
- Put(Date(3), Width => 0);
- end Put;
- procedure Next_Date(OG : in Date_Type; Date : out Date_Type) is
- begin
- Date := OG;
- if Date(2) = 2 then
- if Date(3) = 28 then
- Date(3) := 1;
- Date(2) := 3;
- else
- Date(3) := Date(3) + 1;
- end if;
- elsif Date(2) = 4 or Date(2) = 6 or Date(2) = 9 or Date(2) = 11 then
- if Date(3) = 30 then
- Date(3) := 1;
- Date(2) := Date(2) + 1;
- else
- Date(3) := Date(3) +1;
- end if;
- else
- if Date(3) = 31 then
- Date(3) := 1;
- Date(2) := Date(2) + 1;
- else
- Date(3) := Date(3) + 1;
- end if;
- if Date(2) = 13 then
- Date(2) := 1;
- Date(1) := Date(1) +1;
- end if;
- end if;
- end Next_Date;
- procedure Previous_Date(OG : in Date_Type; Date : out Date_Type) is
- begin
- Date := OG;
- if Date(2) = 3 then
- if Date(3) = 1 then
- Date(3) := 28;
- Date(2) := 2;
- else
- Date(3) := Date(3) - 1;
- end if;
- elsif Date(2) = 5 or Date(2) = 7 or Date(2) = 10 or Date(2) = 12 then
- if Date(3) = 1 then
- Date(3) := 30;
- Date(2) := Date(2) - 1;
- else
- Date(3) := Date(3) - 1;
- end if;
- else
- if Date(3) = 1 then
- Date(3) := 31;
- Date(2) := Date(2) - 1;
- else
- Date(3) := Date(3) - 1;
- end if;
- if Date(2) = 0 then
- Date(2) := 12;
- Date(1) := Date(1) - 1;
- end if;
- end if;
- end Previous_Date;
- function Next_Date(Date : in Date_Type) return Date_Type is
- Nxtdate : Date_Type;
- begin
- Next_Date(Date,Nxtdate);
- return Nxtdate;
- end Next_Date;
- function Previous_Date(Date : in Date_Type) return Date_Type is
- Predate : Date_Type;
- begin
- Previous_Date(Date,Predate);
- return Predate;
- end Previous_Date;
- function "<"(Left, Right : in Date_Type) return Boolean is
- begin
- if Left(1) = Right(1) then
- if Left(2) = Right(2) then
- if Left(3) < Right(3) then
- return True;
- else
- return False;
- end if;
- elsif Left(2) < Right(2) then
- return True;
- else
- return False;
- end if;
- elsif Left(1) < Right(1) then
- return True;
- else
- return False;
- end if;
- end "<";
- function ">"(Left, Right : in Date_Type) return Boolean is
- begin
- return Right < Left;
- end ">";
- function "="(Left, Right : in Date_Type) return Boolean is
- begin
- return not( Left < Right) and not (Left > Right);
- end "=";
- end Altpaket;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement