Guest User

Untitled

a guest
Jan 27th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 6.15 KB | None | 0 0
  1. with Ada.Text_IO;
  2. use Ada.Text_IO;
  3. with Ada.Integer_Text_IO;
  4. use Ada.Integer_Text_IO;
  5.  
  6. procedure Calendar_Njm8263 is
  7.    ------------------------------------------------------------------------
  8.    --| This program uses subprograms to explore a problem in adding days
  9.    --| to a date for lab 4.
  10.    --| Originally written by: Alan Garvey
  11.    --| Adapted by: Jon Beck
  12.    --| Modified for this assignment by: Nicholas Moser
  13.    ------------------------------------------------------------------------
  14.  
  15.    Min_Year : constant Natural := 1901;
  16.    Max_Year : constant Natural := 2099;
  17.  
  18.    function Month_Length (
  19.          Month : in     Natural)
  20.      return Natural is
  21.       -- This function returns the number of days in the given month.
  22.       -- For February (Month = 2) it always returns 28, since we can't
  23.       -- tell here if it is a leap year.
  24.    begin -- Month_Length
  25.       -- You should replace the following line with code to correctly
  26.       -- return the number of days.
  27.       -- You should use a case statement in this function
  28.       case Month is
  29.          when 2                =>
  30.             return 28;
  31.          when 4 | 6 | 9 | 11   =>
  32.             return 30;
  33.          when others           =>
  34.             return 31;
  35.       end case;
  36.    end Month_Length;
  37.  
  38.    function Valid_Date (
  39.          Day   : in     Integer;
  40.          Month : in     Integer;
  41.          Year  : in     Integer)
  42.      return Boolean is
  43.       -- This function checks whether a date is valid.  It is limited to
  44.       -- only working for dates in the 20th and 21st centuries, so it makes
  45.       -- some simplifying assumptions about leap years that are not
  46.       -- universally valid.
  47.    begin -- Valid_Date
  48.       if Year < Min_Year or Year > Max_Year or Day < 1 or Day > 31 or
  49.             Month < 1 or Month > 12 then
  50.          return False;
  51.       elsif (Year mod 4 = 0) and (Month = 2) then
  52.          -- true if Day <= 29, false otherwise
  53.          return Day <= 29;
  54.       else
  55.          -- true if Day <= days in month, false otherwise
  56.          return Day <= Month_Length (Month);
  57.       end if;
  58.    end Valid_Date;
  59.  
  60.    procedure Get_Valid_Date (
  61.          Day   :    out Natural;
  62.          Month :    out Natural;
  63.          Year  :    out Natural) is
  64.       -- Get a valid date from the user.  Use the Valid_Date function
  65.       -- to make sure that the date is valid.
  66.       -- Use a loop to make them enter information again if the date is
  67.       -- not valid.
  68.    begin --Get_Valid_Date
  69.       loop
  70.          -- You write code here to read in a day, month & year.
  71.          -- Exit when the given date is valid.  Use Valid_Date to
  72.          -- check the validity of the given date.
  73.          Ada.Text_IO.Put (Item => "Please enter the day: ");
  74.          Ada.Integer_Text_IO.Get (Item => Integer (Day));
  75.          Ada.Text_IO.Put (Item => "Please enter the month: ");
  76.          Ada.Integer_Text_IO.Get (Item => Integer (Month));
  77.          Ada.Text_IO.Put (Item => "Please enter the year: ");
  78.          Ada.Integer_Text_IO.Get (Item => Integer (Year));
  79.          exit when Valid_Date (Day, Month, Year);
  80.       end loop;
  81.    end Get_Valid_Date;
  82.  
  83.    procedure Output_Date (
  84.          Day   : in out Natural;
  85.          Month : in out Natural;
  86.          Year  : in out Natural) is
  87.  
  88.    begin -- Output_Date
  89.       Ada.Integer_Text_IO.Put (
  90.          Item  => Integer (Month),
  91.          Width => 1);
  92.       Ada.Text_IO.Put (Item => "/");
  93.       Ada.Integer_Text_IO.Put (
  94.          Item  => Integer (Day),
  95.          Width => 1);
  96.       Ada.Text_IO.Put (Item => "/");
  97.       Ada.Integer_Text_IO.Put (
  98.          Item  => Integer (Year),
  99.          Width => 1);
  100.       Ada.Text_IO.New_Line (Spacing => 2);
  101.    end Output_Date;
  102.  
  103.    procedure Add_Days (
  104.          Day   : in out Natural;
  105.          Month : in out Natural;
  106.          Year  : in out Natural;
  107.          Days  : in     Natural) is
  108.       -- This procedure modifies Day, Month, and Year to be the original
  109.       -- input date with Days added to it.  Days is a natural, meaning that
  110.       -- we can assume it is never negative.
  111.  
  112.       MonthDays : Natural; -- holds the number of days in a month
  113.  
  114.    begin -- Add_Days
  115.       Day := Day + Days;
  116.       loop
  117.          -- Handle leap years between Min_Year and Max_Year correctly.
  118.          -- This does not work for leap years that end in 00 that
  119.          -- are not divisible by 400.
  120.          if Year mod 4 = 0 and Month = 2 then
  121.             MonthDays := 29;
  122.          else
  123.             MonthDays := Month_Length(Month);
  124.          end if;
  125.  
  126.          -- When Days <= MonthDays, we have a valid date.
  127.          exit when Day <= MonthDays;
  128.          -- Otherwise update days by moving forward a month, subtracting
  129.          -- that number of days from Days.  If we cross a year boundary,
  130.          -- update the year too.
  131.          Day := Day - MonthDays;
  132.          Month := Month + 1;
  133.          if Month > 12 then
  134.             Month := 1;
  135.             Year := Year + 1;
  136.          end if;
  137.       end loop;
  138.    end Add_Days;
  139.  
  140.    User_Day    : Natural; -- the day the user enters, after validation
  141.    User_Month  : Natural;    -- the month the user enters, after validation
  142.    User_Year   : Natural; -- the year the user enters, after validation
  143.    Days_To_Add : Natural; -- the number of days to add to the date
  144.  
  145. begin -- Calendar_Njm8263;
  146.  
  147.    -- Get a valid date from the user.
  148.    Put (Item => "Welcome to the date adding program.");
  149.    New_Line (Spacing => 2);
  150.  
  151.    Get_Valid_Date(
  152.       Day   => User_Day,
  153.       Month => User_Month,
  154.       Year  => User_Year);
  155.  
  156.    Put (Item => "Please enter a positive number of days to add to this date: ");
  157.    Get (Item => Days_To_Add);
  158.  
  159.    -- Add Days_To_Add to the date, changing User_Day, User_Month & User_Year.
  160.    Add_Days (
  161.       Day   => User_Day,
  162.       Month => User_Month,
  163.       Year  => User_Year,
  164.       Days  => Days_To_Add);
  165.  
  166.    -- Output the changed date to the user in a pretty numerical format
  167.    --  such as 12/23/2012.  You need to write this procedure from
  168.    --  scratch.  Remember that a program's output should _always_ end
  169.    --  with a newline.
  170.    Output_Date (
  171.       Day   => User_Day,
  172.       Month => User_Month,
  173.       Year  => User_Year);
  174.  
  175. end Calendar_Njm8263;
Add Comment
Please, Sign In to add comment