home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / WildLifeMonitor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.6 KB  |  96 lines

  1. //: C20:WildLifeMonitor.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include <vector>
  7. #include <map>
  8. #include <string>
  9. #include <algorithm>
  10. #include <iostream>
  11. #include <sstream>
  12. #include <ctime>
  13. using namespace std;
  14.  
  15. class DataPoint {
  16.   int x, y; // Location coordinates
  17.   time_t time; // Time of Sighting
  18. public:
  19.   DataPoint() : x(0), y(0), time(0) {}
  20.   DataPoint(int xx, int yy, time_t tm) :
  21.     x(xx), y(yy), time(tm) {}
  22.   // Synthesized operator=, copy-constructor OK
  23.   int getX() { return x; }
  24.   int getY() { return y; }
  25.   time_t* getTime() { return &time; }
  26. };
  27.  
  28. string animal[] = { 
  29.   "chipmunk", "beaver", "marmot", "weasel",
  30.   "squirrel", "ptarmigan", "bear", "eagle",
  31.   "hawk", "vole", "deer", "otter", "hummingbird",
  32. };
  33. const int asz = sizeof animal/sizeof *animal;
  34. vector<string> animals(animal, animal + asz);
  35.  
  36. // All the information is contained in a 
  37. // "Sighting," which can be sent to an ostream:
  38. typedef pair<string, DataPoint> Sighting;
  39.  
  40. ostream& 
  41. operator<<(ostream& os, const Sighting& s) {
  42.   return os << s.first << " sighted at x= " << 
  43.     s.second.getX() << ", y= " << s.second.getY()
  44.     << ", time = " << ctime(s.second.getTime());
  45. }
  46.  
  47. // A generator for Sightings:
  48. class SightingGen {
  49.   vector<string>& animals;
  50.   static const int d = 100;
  51. public:
  52.   SightingGen(vector<string>& an) :
  53.     animals(an) { srand(time(0)); }
  54.   Sighting operator()() {
  55.     Sighting result;
  56.     int select = rand() % animals.size();
  57.     result.first = animals[select];
  58.     result.second = DataPoint(
  59.       rand() % d, rand() % d, time(0));
  60.     return result;
  61.   }
  62. };
  63.  
  64. typedef multimap<string, DataPoint> DataMap;
  65. typedef DataMap::iterator DMIter;
  66.  
  67. int main() {
  68.   DataMap sightings;
  69.   generate_n(
  70.     inserter(sightings, sightings.begin()),
  71.     50, SightingGen(animals));
  72.   // Print everything:
  73.   copy(sightings.begin(), sightings.end(),
  74.     ostream_iterator<Sighting>(cout, ""));
  75.   // Print sightings for selected animal:
  76.   while(true) {
  77.     cout << "select an animal or 'q' to quit: ";
  78.     for(int i = 0; i < animals.size(); i++)
  79.       cout <<'['<< i <<']'<< animals[i] << ' ';
  80.     cout << endl;
  81.     string reply;
  82.     cin >> reply;
  83.     if(reply.at(0) == 'q') return 0;
  84.     istringstream r(reply);
  85.     int i;
  86.     r >> i; // Converts to int
  87.     i %= animals.size();
  88.     // Iterators in "range" denote begin, one 
  89.     // past end of matching range:
  90.     pair<DMIter, DMIter> range = 
  91.       sightings.equal_range(animals[i]);
  92.     copy(range.first, range.second,
  93.       ostream_iterator<Sighting>(cout, ""));
  94.   }
  95. } ///:~
  96.