Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Ada.Text_IO;
- use Ada.Text_IO;
- with Ada.Integer_Text_IO;
- use Ada.Integer_Text_IO;
- procedure Calendar_Njm8263 is
- ------------------------------------------------------------------------
- --| This program uses subprograms to explore a problem in adding days
- --| to a date for lab 4.
- --| Originally written by: Alan Garvey
- --| Adapted by: Jon Beck
- --| Modified for this assignment by: Nicholas Moser
- ------------------------------------------------------------------------
- Min_Year : constant Natural := 1901;
- Max_Year : constant Natural := 2099;
- function Month_Length (
- Month : in Natural)
- return Natural is
- -- This function returns the number of days in the given month.
- -- For February (Month = 2) it always returns 28, since we can't
- -- tell here if it is a leap year.
- begin -- Month_Length
- -- You should replace the following line with code to correctly
- -- return the number of days.
- -- You should use a case statement in this function
- case Month is
- when 2 =>
- return 28;
- when 4 | 6 | 9 | 11 =>
- return 30;
- when others =>
- return 31;
- end case;
- end Month_Length;
- function Valid_Date (
- Day : in Integer;
- Month : in Integer;
- Year : in Integer)
- return Boolean is
- -- This function checks whether a date is valid. It is limited to
- -- only working for dates in the 20th and 21st centuries, so it makes
- -- some simplifying assumptions about leap years that are not
- -- universally valid.
- begin -- Valid_Date
- if Year < Min_Year or Year > Max_Year or Day < 1 or Day > 31 or
- Month < 1 or Month > 12 then
- return False;
- elsif (Year mod 4 = 0) and (Month = 2) then
- -- true if Day <= 29, false otherwise
- return Day <= 29;
- else
- -- true if Day <= days in month, false otherwise
- return Day <= Month_Length (Month);
- end if;
- end Valid_Date;
- procedure Get_Valid_Date (
- Day : out Natural;
- Month : out Natural;
- Year : out Natural) is
- -- Get a valid date from the user. Use the Valid_Date function
- -- to make sure that the date is valid.
- -- Use a loop to make them enter information again if the date is
- -- not valid.
- begin --Get_Valid_Date
- loop
- -- You write code here to read in a day, month & year.
- -- Exit when the given date is valid. Use Valid_Date to
- -- check the validity of the given date.
- Ada.Text_IO.Put (Item => "Please enter the day: ");
- Ada.Integer_Text_IO.Get (Item => Integer (Day));
- Ada.Text_IO.Put (Item => "Please enter the month: ");
- Ada.Integer_Text_IO.Get (Item => Integer (Month));
- Ada.Text_IO.Put (Item => "Please enter the year: ");
- Ada.Integer_Text_IO.Get (Item => Integer (Year));
- exit when Valid_Date (Day, Month, Year);
- end loop;
- end Get_Valid_Date;
- procedure Output_Date (
- Day : in out Natural;
- Month : in out Natural;
- Year : in out Natural) is
- begin -- Output_Date
- Ada.Integer_Text_IO.Put (
- Item => Integer (Month),
- Width => 1);
- Ada.Text_IO.Put (Item => "/");
- Ada.Integer_Text_IO.Put (
- Item => Integer (Day),
- Width => 1);
- Ada.Text_IO.Put (Item => "/");
- Ada.Integer_Text_IO.Put (
- Item => Integer (Year),
- Width => 1);
- Ada.Text_IO.New_Line (Spacing => 2);
- end Output_Date;
- procedure Add_Days (
- Day : in out Natural;
- Month : in out Natural;
- Year : in out Natural;
- Days : in Natural) is
- -- This procedure modifies Day, Month, and Year to be the original
- -- input date with Days added to it. Days is a natural, meaning that
- -- we can assume it is never negative.
- MonthDays : Natural; -- holds the number of days in a month
- begin -- Add_Days
- Day := Day + Days;
- loop
- -- Handle leap years between Min_Year and Max_Year correctly.
- -- This does not work for leap years that end in 00 that
- -- are not divisible by 400.
- if Year mod 4 = 0 and Month = 2 then
- MonthDays := 29;
- else
- MonthDays := Month_Length(Month);
- end if;
- -- When Days <= MonthDays, we have a valid date.
- exit when Day <= MonthDays;
- -- Otherwise update days by moving forward a month, subtracting
- -- that number of days from Days. If we cross a year boundary,
- -- update the year too.
- Day := Day - MonthDays;
- Month := Month + 1;
- if Month > 12 then
- Month := 1;
- Year := Year + 1;
- end if;
- end loop;
- end Add_Days;
- User_Day : Natural; -- the day the user enters, after validation
- User_Month : Natural; -- the month the user enters, after validation
- User_Year : Natural; -- the year the user enters, after validation
- Days_To_Add : Natural; -- the number of days to add to the date
- begin -- Calendar_Njm8263;
- -- Get a valid date from the user.
- Put (Item => "Welcome to the date adding program.");
- New_Line (Spacing => 2);
- Get_Valid_Date(
- Day => User_Day,
- Month => User_Month,
- Year => User_Year);
- Put (Item => "Please enter a positive number of days to add to this date: ");
- Get (Item => Days_To_Add);
- -- Add Days_To_Add to the date, changing User_Day, User_Month & User_Year.
- Add_Days (
- Day => User_Day,
- Month => User_Month,
- Year => User_Year,
- Days => Days_To_Add);
- -- Output the changed date to the user in a pretty numerical format
- -- such as 12/23/2012. You need to write this procedure from
- -- scratch. Remember that a program's output should _always_ end
- -- with a newline.
- Output_Date (
- Day => User_Day,
- Month => User_Month,
- Year => User_Year);
- end Calendar_Njm8263;
Add Comment
Please, Sign In to add comment