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

  1.                         -- Chapter 25 - Programming exercise 3
  2. with Text_IO, Calendar;
  3. use Text_IO, Calendar;
  4.  
  5. procedure CH25_3 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.    Number_Of_Dogs : INTEGER := 0;
  13.  
  14.                           -- The main program adds and deletes
  15.                           -- hot dogs to/from the shelf.
  16.  
  17.    task Five_Dogs;        -- This task adds five hot dogs to stock
  18.  
  19.    task Remove_Five_Dogs; -- This task deletes five from stock
  20.  
  21.    task How_Many_Are_Left; -- This task lists the number on shelf
  22.  
  23.    task Retail_Hot_Dogs is
  24.       entry Stock_With_A_Hot_Dog;  -- This adds a hot dog to stock
  25.       entry Deliver_A_Hot_Dog;     -- This deletes one from stock
  26.    end Retail_Hot_Dogs;
  27.  
  28.    task body Retail_Hot_Dogs is
  29.    begin
  30.       for Index in 1..18 loop
  31.          select
  32.             when Number_Of_Dogs < 8 =>
  33.             accept Stock_With_A_Hot_Dog do
  34.                Number_Of_Dogs := Number_Of_Dogs + 1;
  35.                Put("Add a hot dog to the shelf, number =");
  36.                Put(Number_Of_Dogs,3);
  37.                New_Line;
  38.             end Stock_With_A_Hot_Dog;
  39.          or
  40.             when Number_Of_Dogs > 0 =>
  41.             accept Deliver_A_Hot_Dog do
  42.                Put_Line("Remove a hot dog from the shelf");
  43.                Number_Of_Dogs := Number_Of_Dogs - 1;
  44.             end Deliver_A_Hot_Dog;
  45.          end select;
  46.       end loop;
  47.    end Retail_Hot_Dogs;
  48.  
  49.    task body Five_Dogs is
  50.    begin
  51.       for Index in 1..5 loop
  52.          delay 0.1;
  53.          Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
  54.       end loop;
  55.    end Five_Dogs;
  56.  
  57.    task body Remove_Five_Dogs is
  58.    begin
  59.       for Index in 1..5 loop
  60.          delay 0.6;
  61.          Retail_Hot_Dogs.Deliver_A_Hot_Dog;
  62.       end loop;
  63.    end Remove_Five_Dogs;
  64.  
  65.    task body How_Many_Are_Left is
  66.    Time_And_Date    : TIME;
  67.    Start, Seconds   : DAY_DURATION;
  68.    Year, Month, Day : INTEGER;
  69.    begin
  70.       Time_And_Date := Clock;
  71.       Split(Time_And_Date,Year,Month,Day,Start);
  72.       for Index in 1..10 loop
  73.          delay 0.3;
  74.          Time_And_Date := Clock;
  75.          Split(Time_And_Date,Year,Month,Day,Seconds);
  76.          Put("Elapsed time =");
  77.          Put(Seconds - Start,3,3,0);
  78.          Put(", and there are");
  79.          Put(Number_Of_Dogs,3);
  80.          Put_Line(" hotdogs left on the shelf.");
  81.       end loop;
  82.    end How_Many_Are_Left;
  83.  
  84. begin
  85.    for Index in 1..4 loop
  86.       delay 0.9;
  87.       Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
  88.       Retail_Hot_Dogs.Deliver_A_Hot_Dog;
  89.    end loop;
  90. end CH25_3;
  91.  
  92.  
  93.  
  94.  
  95. -- Result of execution (With no changes)
  96.  
  97. -- Add a hot dog to the shelf, number =  1
  98. -- Add a hot dog to the shelf, number =  2
  99. -- Add a hot dog to the shelf, number =  3
  100. -- Elapsed time =  0.330, and there are  3 hotdogs left on the shelf.
  101. -- Add a hot dog to the shelf, number =  4
  102. -- Add a hot dog to the shelf, number =  5
  103. -- Remove a hot dog from the shelf
  104. -- Elapsed time =  0.660, and there are  4 hotdogs left on the shelf.
  105. -- Add a hot dog to the shelf, number =  5
  106. -- Remove a hot dog from the shelf
  107. -- Elapsed time =  0.990, and there are  4 hotdogs left on the shelf.
  108. -- Remove a hot dog from the shelf
  109. -- Elapsed time =  1.320, and there are  3 hotdogs left on the shelf.
  110. -- Elapsed time =  1.650, and there are  3 hotdogs left on the shelf.
  111. -- Remove a hot dog from the shelf
  112. -- Add a hot dog to the shelf, number =  3
  113. -- Elapsed time =  1.980, and there are  3 hotdogs left on the shelf.
  114. -- Remove a hot dog from the shelf
  115. -- Elapsed time =  2.290, and there are  2 hotdogs left on the shelf.
  116. -- Remove a hot dog from the shelf
  117. -- Elapsed time =  2.620, and there are  1 hotdogs left on the shelf.
  118. -- Elapsed time =  2.950, and there are  1 hotdogs left on the shelf.
  119. -- Add a hot dog to the shelf, number =  2
  120. -- Remove a hot dog from the shelf
  121. -- Remove a hot dog from the shelf
  122. -- Elapsed time =  3.280, and there are  0 hotdogs left on the shelf.
  123. -- Add a hot dog to the shelf, number =  1
  124. -- Remove a hot dog from the shelf
  125.  
  126.