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

  1.                                        -- Chapter 16 - 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. with Text_IO;
  13. use Text_IO;
  14.  
  15. procedure Age is
  16.  
  17.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  18.    use Int_IO;
  19.  
  20.    LOW_YEAR    : constant := 1880;
  21.    MAX         : constant := 365.0 * (2100 - LOW_YEAR);
  22.    type AGES is delta 1.0 range -MAX..MAX;
  23.    Present_Age : AGES;
  24.  
  25.    package Fix_IO is new Text_IO.Fixed_IO(AGES);
  26.    use Fix_IO;
  27.  
  28.    type DATE is record
  29.       Month : INTEGER range 1..12;
  30.       Day   : INTEGER range 1..31;
  31.       Year  : INTEGER range LOW_YEAR..2100;
  32.       Days  : AGES;
  33.    end record;
  34.  
  35.    Today       : DATE;
  36.    Birth_Day   : DATE;
  37.  
  38.    procedure Get_Date(Date_To_Get : in out DATE) is
  39.    Temp : INTEGER;
  40.    begin
  41.       Put(" month --> ");
  42.       loop
  43.          Get(Temp);
  44.          if Temp in 1..12 then
  45.             Date_To_Get.Month := Temp;
  46.             exit;                       -- month OK
  47.          else
  48.             Put_Line(" Month must be in the range of 1 to 12");
  49.             Put("                    ");
  50.             Put(" month --> ");
  51.          end if;
  52.       end loop;
  53.  
  54.       Put("                    ");
  55.       Put(" day ----> ");
  56.       loop
  57.          Get(Temp);
  58.          if Temp in 1..31 then
  59.             Date_To_Get.Day := Temp;
  60.             exit;                       -- day OK
  61.          else
  62.             Put_Line(" Day must be in the range of 1 to 31");
  63.             Put("                    ");
  64.             Put(" day ----> ");
  65.          end if;
  66.       end loop;
  67.  
  68.       Put("                    ");
  69.       Put(" year ---> ");
  70.       loop
  71.          Get(Temp);
  72.          if Temp in LOW_YEAR..2100 then
  73.             Date_To_Get.Year := Temp;
  74.             exit;                       -- year OK
  75.          else
  76.             Put_Line(" Year must be in the range of 1880 to 2100");
  77.             Put("                    ");
  78.             Put(" year ---> ");
  79.          end if;
  80.       end loop;
  81.       Date_To_Get.Days := 365 * AGES(Date_To_Get.Year - LOW_YEAR)
  82.                   + AGES(31 * Date_To_Get.Month + Date_To_Get.Day);
  83.  
  84.    end Get_Date;
  85.  
  86. begin
  87.    Put("Enter Today's date; ");
  88.    Get_Date(Today);
  89.    New_Line;
  90.  
  91.    Put("Enter your birthday;");
  92.    Get_Date(Birth_Day);
  93.    New_Line(2);
  94.  
  95.    Present_Age := Today.Days - Birth_Day.Days;
  96.    if Present_Age < 0.0 then
  97.       Put("You will be born in ");
  98.       Present_Age := abs(Present_Age);
  99.       Put(Present_Age,6,0,0);
  100.       Put_Line(" days.");
  101.     elsif Present_Age = 0.0 then
  102.       Put_Line("Happy birthday, you were just born today.");
  103.     else
  104.       Put("You are now ");
  105.       Put(Present_Age,6,0,0);
  106.       Put_Line(" days old.");
  107.    end if;
  108.  
  109. end Age;
  110.  
  111.