home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / icecream.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  7KB  |  263 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * icecream.cpp - priority queue example program. Section 11.3.1
  6.  *
  7.  * $Id: icecream.cpp,v 1.16 1996/09/03 22:26:40 smithey Exp $
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  12.  * ALL RIGHTS RESERVED *
  13.  * The software and information contained herein are proprietary to, and
  14.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  15.  * intends to preserve as trade secrets such software and information.
  16.  * This software is furnished pursuant to a written license agreement and
  17.  * may be used, copied, transmitted, and stored only in accordance with
  18.  * the terms of such license and with the inclusion of the above copyright
  19.  * notice.  This software and information or any other copies thereof may
  20.  * not be provided or otherwise made available to any other person.
  21.  *
  22.  * Notwithstanding any other lease or license that may pertain to, or
  23.  * accompany the delivery of, this computer software and information, the
  24.  * rights of the Government regarding its use, reproduction and disclosure
  25.  * are as set forth in Section 52.227-19 of the FARS Computer
  26.  * Software-Restricted Rights clause.
  27.  * 
  28.  * Use, duplication, or disclosure by the Government is subject to
  29.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  30.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  31.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  32.  * P.O. Box 2328, Corvallis, Oregon 97339.
  33.  *
  34.  * This computer software and information is distributed with "restricted
  35.  * rights."  Use, duplication or disclosure is subject to restrictions as
  36.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  37.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  38.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  39.  * then the "Alternate III" clause applies.
  40.  *
  41.  **************************************************************************/
  42.  
  43. #ifndef _RWSTD_HEADER_REQUIRES_HPP
  44. #include <vector>
  45. #include <queue>
  46. #else
  47. #include <vector.hpp>
  48. #include <queue.hpp>
  49. #endif
  50.  
  51. #ifdef _RW_STD_IOSTREAM
  52. #include <iostream>
  53. #else
  54. #include <iostream.h>
  55. #endif
  56.     
  57. #ifndef _RWSTD_NO_NAMESPACE
  58.   using namespace std;
  59. #endif
  60.  
  61. //
  62. // Execution event in a descrete event driven simulation.
  63. //
  64.  
  65. class event
  66. {
  67.   public:
  68.     //
  69.     // Construct sets time of event.
  70.     //
  71.     event (unsigned int t) : time(t) { };
  72.     //
  73.     // Time is a public data field.
  74.     //
  75.     const unsigned int time;
  76.     //
  77.     // Execute event my invoking this method.
  78.     //
  79.     virtual void processEvent() = 0;
  80. };
  81.  
  82. //
  83. // Needed by some compilers.
  84. //
  85. inline void __destroy (event **) {}
  86.  
  87. struct eventComparison
  88. {
  89.     bool operator () (const event * left, const event * right)
  90.         { return left->time > right->time; }
  91. };
  92.  
  93. //
  94. // Framework for discrete event-driven simulations.
  95. //
  96.  
  97. class simulation
  98. {
  99.   public:
  100.     simulation () : eventQueue(), time(0) {}
  101.     void run ();
  102.     unsigned int time;
  103.     void  scheduleEvent (event * newEvent) { eventQueue.push(newEvent); }
  104.   protected:
  105.     priority_queue<event*, vector<event *,allocator<event*> >, eventComparison > eventQueue;
  106. };
  107.  
  108. void simulation::run ()
  109. {
  110.     while (! eventQueue.empty())
  111.     {
  112.         event * nextEvent = eventQueue.top();
  113.         eventQueue.pop();
  114.         time = nextEvent->time;
  115.         nextEvent->processEvent();
  116.         delete nextEvent;
  117.     }
  118. }
  119.  
  120. //
  121. //  Ice cream store simulation.
  122. //
  123.  
  124. class storeSimulation : public simulation
  125. {
  126.   public:
  127.     storeSimulation() : freeChairs(35), profit(0.0), simulation() { }
  128.         
  129.     bool canSeat (unsigned int numberOfPeople);
  130.     void order   (unsigned int numberOfScoops);
  131.     void leave   (unsigned int numberOfPeople);
  132.     //
  133.     // Data fields.
  134.     //
  135.     unsigned int freeChairs;
  136.     double       profit;  
  137. } theSimulation;
  138.  
  139. class arriveEvent : public event
  140. {
  141.   public:
  142.     arriveEvent (unsigned int time, unsigned int groupSize)
  143.         : event(time), size(groupSize) { }
  144.     virtual void processEvent();
  145.   private:
  146.     unsigned int size;
  147. };
  148.  
  149. class orderEvent : public event
  150. {
  151.   public:
  152.     orderEvent (unsigned int time, unsigned int groupSize)
  153.         : event(time), size(groupSize) { }
  154.     virtual void processEvent();
  155.   private:
  156.     unsigned int size;
  157. };
  158.  
  159. class leaveEvent : public event
  160. {
  161. public:
  162.     leaveEvent (unsigned int time, unsigned int groupSize)
  163.         : event(time), size(groupSize) { }
  164.     virtual void processEvent();
  165. private:
  166.     unsigned int size;
  167. };
  168.  
  169. //
  170. // Return random integer between 0 and n.
  171. //
  172.  
  173. int irand (int n) { return (rand()/10) % n; }
  174.  
  175. void arriveEvent::processEvent ()
  176. {
  177.     if (theSimulation.canSeat(size))
  178.         theSimulation.scheduleEvent(new orderEvent(time + 1 + irand(4), size));
  179. }
  180.  
  181. void orderEvent::processEvent ()
  182. {
  183.     //
  184.     // Each person orders some number of scoops.
  185.     //
  186.     for (int i = 0; i < size; i++)
  187.         theSimulation.order(1 + irand(4));
  188.     //
  189.     // Then we schedule the leave event.
  190.     //
  191.     theSimulation.scheduleEvent(new leaveEvent(time + 1 + irand(10), size));
  192. }
  193.  
  194. void leaveEvent::processEvent () { theSimulation.leave(size); }
  195.  
  196. //
  197. // If sufficient room then seat customers.
  198. //
  199.  
  200. bool storeSimulation::canSeat (unsigned int numberOfPeople)
  201. {
  202.     cout << "Time: " << time;
  203.     cout << " group of " << numberOfPeople << " customers arrives";
  204.     if (numberOfPeople < freeChairs)
  205.     {
  206.         cout << " is seated" << endl;
  207.         freeChairs -= numberOfPeople;
  208.         return true;
  209.     }
  210.     else
  211.     {
  212.         cout << " no room, they leave" << endl;
  213.         return false;
  214.     }
  215. }
  216.  
  217. //
  218. // Service icecream, compute profits.
  219. //
  220.  
  221. void storeSimulation::order (unsigned int numberOfScoops)
  222. {
  223.     cout << "Time: " << time;
  224.     cout << " serviced order for " << numberOfScoops << endl;
  225.     profit += 0.35 * numberOfScoops;
  226. }
  227.  
  228. //
  229. // People leave, free up chairs.
  230. //
  231.  
  232. void storeSimulation::leave (unsigned int numberOfPeople)
  233. {
  234.     cout << "Time: " << time;
  235.     cout << " group of size " << numberOfPeople << " leaves" << endl;
  236.     freeChairs += numberOfPeople;
  237. }
  238.  
  239. int main ()
  240. {
  241.     cout << "Ice Cream Store simulation from Chapter 9"  << endl;
  242.     //
  243.     // Load queue with some number of initial events.
  244.     //
  245.     unsigned int t = 0;
  246.     while (t < 20)
  247.     {
  248.         t += irand(6);
  249.         cout << "pumping queue with event " << t << endl;
  250.         theSimulation.scheduleEvent(new arriveEvent(t, 1 + irand(4)));
  251.     }
  252.     //
  253.     // Run the simulation.
  254.     //
  255.     theSimulation.run();
  256.     cout << "Total profits " << theSimulation.profit << endl;
  257.     
  258.     cout << "End of ice cream store simulation" << endl;
  259.  
  260.     return 0;
  261. }
  262.     
  263.