home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / age2.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  3.9 KB  |  128 lines

  1.                                        -- Chapter 31 - Program 5
  2. -- This program will calculate the number of days old you are.
  3. -- It is a rather dumb program, but illustrates some interesting
  4. -- programming techniques.  It checks all input to see that they
  5. -- are in the correct range before continuing.  Since the number
  6. -- of days can easily exceed the limits of type INTEGER, and we
  7. -- cannot count on LONG_INTEGER being available, a fixed point
  8. -- variable is used for the total number of days since Jan 1, 1880.
  9. -- This program also passes a record to a procedure, where it is
  10. -- modified and returned.
  11.  
  12. -- This is a repeat of the program named AGE.ADA from chapter 16
  13. -- of this tutorial.  This program uses the CALENDAR package for
  14. -- the current date so the user does not have to enter today's
  15. -- date.  It also uses some of the subtypes from the CALENDAR
  16. -- package, but not the YEAR_NUMBER, because it does not follow
  17. -- our desired range.
  18.  
  19. with Text_IO, Calendar;
  20. use Text_IO, Calendar;
  21.  
  22. procedure Age2 is
  23.  
  24.    LOW_YEAR    : constant := 1880;
  25.    MAX         : constant := 365.0 * (2100 - LOW_YEAR);
  26.  
  27.    type AGES is delta 1.0 range -MAX..MAX;
  28.  
  29.    Days_Since_1880 : AGES;
  30.    Present_Age     : AGES;
  31.  
  32.    Today           : TIME;          -- Present date and time
  33.    This_Month      : MONTH_NUMBER;
  34.    This_Day        : DAY_NUMBER;
  35.    This_Year       : INTEGER range LOW_YEAR..2100;
  36.    Seconds         : DAY_DURATION;
  37.  
  38.    type DATE is
  39.       record
  40.          Month : MONTH_NUMBER;
  41.          Day   : DAY_NUMBER;
  42.          Year  : INTEGER range LOW_YEAR..2100;
  43.          Days  : AGES;
  44.       end record;
  45.  
  46.    Birth_Day   : DATE;
  47.  
  48.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  49.    use Int_IO;
  50.    package Fix_IO is new Text_IO.Fixed_IO(AGES);
  51.    use Fix_IO;
  52.  
  53.    procedure Get_Date(Date_To_Get : in out DATE) is
  54.       Temp : INTEGER;
  55.    begin
  56.       Put(" month --> ");
  57.       loop
  58.          Get(Temp);
  59.          if Temp in 1..12 then
  60.             Date_To_Get.Month := Temp;
  61.             exit;                       -- month OK
  62.          else
  63.             Put_Line(" Month must be in the range of 1 to 12");
  64.             Put("                    ");
  65.             Put(" month --> ");
  66.          end if;
  67.       end loop;
  68.  
  69.       Put("                    ");
  70.       Put(" day ----> ");
  71.       loop
  72.          Get(Temp);
  73.          if Temp in 1..31 then
  74.             Date_To_Get.Day := Temp;
  75.             exit;                       -- day OK
  76.          else
  77.             Put_Line(" Day must be in the range of 1 to 31");
  78.             Put("                    ");
  79.             Put(" day ----> ");
  80.          end if;
  81.       end loop;
  82.  
  83.       Put("                    ");
  84.       Put(" year ---> ");
  85.       loop
  86.          Get(Temp);
  87.          if Temp in LOW_YEAR..2100 then
  88.             Date_To_Get.Year := Temp;
  89.             exit;                       -- year OK
  90.          else
  91.             Put_Line(" Year must be in the range of 1880 to 2100");
  92.             Put("                    ");
  93.             Put(" year ---> ");
  94.          end if;
  95.       end loop;
  96.       Date_To_Get.Days := 365 * AGES(Date_To_Get.Year - LOW_YEAR)
  97.                   + AGES(31 * Date_To_Get.Month + Date_To_Get.Day);
  98.  
  99.    end Get_Date;
  100.  
  101. begin
  102.             -- Get todays date
  103.    Today := Clock;
  104.    Split(Today, This_Year, This_Month, This_Day, Seconds);
  105.    Days_Since_1880 := 365 * AGES(This_Year - LOW_YEAR)
  106.                            + AGES(31 * This_Month + This_Day);
  107.  
  108.    Put("Enter your birthday;");
  109.    Get_Date(Birth_Day);
  110.    New_Line(2);
  111.  
  112.    Present_Age := Days_since_1880 - Birth_Day.Days;
  113.    if Present_Age < 0.0 then
  114.       Put("You will be born in ");
  115.       Present_Age := abs(Present_Age);
  116.       Put(Present_Age,6,0,0);
  117.       Put_Line(" days.");
  118.     elsif Present_Age = 0.0 then
  119.       Put_Line("Happy birthday, you were just born today.");
  120.     else
  121.       Put("You are now ");
  122.       Put(Present_Age,6,0,0);
  123.       Put_Line(" days old.");
  124.    end if;
  125.  
  126. end Age2;
  127.  
  128.