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

  1.                         -- Chapter 24 - Programming exercise 1
  2. with Text_IO; use Text_IO;
  3. with Calendar; use Calendar;
  4.  
  5. procedure CH24_1 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.    package Fix_IO is new Text_IO.Fixed_IO(DAY_DURATION);
  10.    use Fix_IO;
  11.  
  12.    Year,Month,Day : INTEGER;
  13.    Start,Seconds  : DAY_DURATION;
  14.    Time_And_Date  : TIME;
  15.  
  16. -- This procedure outputs the time in an Hour:Minute:Second format
  17. --   using a FLOAT type for splitting the time into the various
  18. --   fields.  It works fine for simple time logging but lacks the
  19. --   accuracy required for finer time.  If finer time is required,
  20. --   it will be necessary to keep the time in the fixed point format,
  21. --   and use lots of type conversions to get the three fields, and
  22. --   the fractional second field if needed.  An alternative method
  23. --   would be to use a floating point type with more significant
  24. --   digits to maintain the accuracy, but it would require more time
  25. --   to execute.  This is definitely an application for the fixed
  26. --   point data type.
  27.    procedure Output_Time(Time_In_Seconds : DAY_DURATION) is
  28.    Time : FLOAT;
  29.    Hours, Minutes, Seconds : INTEGER;
  30.    begin
  31.       Time := FLOAT(Time_In_Seconds);
  32.       Hours := INTEGER((Time - 30.0 * 60.0) / (60.0 * 60.0));
  33.       Time := Time - FLOAT(Hours) * 60.0 * 60.0;
  34.       Minutes := INTEGER((Time - 30.0) / 60.0);
  35.       Seconds := INTEGER(Time - 0.5) mod 60;
  36.       Put(Hours,3);
  37.       Put(":");
  38.       Put(Minutes,2);
  39.       Put(":");
  40.       Put(Seconds,2);
  41.    end Output_Time;
  42.  
  43. begin
  44.  
  45.    Time_And_Date := Clock;
  46.    Split(Time_And_Date,Year,Month,Day,Seconds);
  47.    Output_Time(Seconds);
  48.    Put_Line(" Begin 3.14 second delay");
  49.  
  50.    delay 3.14;
  51.  
  52.    Time_And_Date := Clock;
  53.    Split(Time_And_Date,Year,Month,Day,Seconds);
  54.    Output_Time(Seconds);
  55.    Put_Line(" End of 3.14 second delay");
  56.  
  57.    Time_And_Date := Clock;
  58.    Split(Time_And_Date,Year,Month,Day,Start);  -- get starting time
  59.  
  60.    for Index in 1..9 loop
  61.       Put("The date and time are now");
  62.       Time_And_Date := Clock;
  63.       Split(Time_And_Date,Year,Month,Day,Seconds);
  64.       Put(Month,3);
  65.       delay 0.2;
  66.       Put(Day,3);
  67.       delay 0.1;
  68.       Put(Year,5);
  69.       delay 0.1;
  70.       Put(Seconds - Start,8,3,0);
  71.       New_Line;
  72.       delay 0.6;
  73.    end loop;
  74.  
  75.    Put_Line("Begin non-accumulative timing loop here.");
  76.  
  77.    Time_And_Date := Clock;
  78.    Split(Time_And_Date,Year,Month,Day,Start);  -- get starting time
  79.    for Index in 1..9 loop
  80.       Time_And_Date := Clock;
  81.       Split(Time_And_Date,Year,Month,Day,Seconds);
  82.       Put("The elapsed time is");
  83.       Put(Seconds - Start,8,3,0);
  84.       New_Line;
  85.       delay Day_Duration(Index) - (Seconds - Start);
  86.    end loop;
  87.  
  88.    Put(" The current time is ");
  89.    Time_And_Date := Clock;
  90.    Split(Time_And_Date,Year,Month,Day,Seconds);
  91.    Output_Time(Seconds);
  92. end CH24_1;
  93.  
  94. -- Result of Execution
  95.  
  96. --  9:54:13 Begin 3.14 second delay
  97. --  9:54:16 End of 3.14 second delay
  98. -- The date and time are now  7 22 1988        0.000
  99. -- The date and time are now  7 22 1988        1.090
  100. -- The date and time are now  7 22 1988        2.140
  101. -- The date and time are now  7 22 1988        3.180
  102. -- The date and time are now  7 22 1988        4.230
  103. -- The date and time are now  7 22 1988        5.270
  104. -- The date and time are now  7 22 1988        6.320
  105. -- The date and time are now  7 22 1988        7.360
  106. -- The date and time are now  7 22 1988        8.400
  107. -- Begin non-accumulative timing loop here
  108. -- The elapsed time is       0.000
  109. -- The elapsed time is       1.100
  110. -- The elapsed time is       2.030
  111. -- The elapsed time is       3.020
  112. -- The elapsed time is       4.040
  113. -- The elapsed time is       5.030
  114. -- The elapsed time is       6.020
  115. -- The elapsed time is       7.010
  116. -- The elapsed time is       8.000
  117. -- The current time is  9:54:35
  118.