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

  1. /**************************************************************************
  2.  *
  3.  * stocks.h - Stocks program.
  4.  *
  5.  * $Id: stocks.h,v 1.15 1997/07/14 11:32:01 sree Exp $
  6.  *
  7.  ***************************************************************************
  8.  *
  9.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  10.  * ALL RIGHTS RESERVED *
  11.  * The software and information contained herein are proprietary to, and
  12.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  13.  * intends to preserve as trade secrets such software and information.
  14.  * This software is furnished pursuant to a written license agreement and
  15.  * may be used, copied, transmitted, and stored only in accordance with
  16.  * the terms of such license and with the inclusion of the above copyright
  17.  * notice.  This software and information or any other copies thereof may
  18.  * not be provided or otherwise made available to any other person.
  19.  *
  20.  * Notwithstanding any other lease or license that may pertain to, or
  21.  * accompany the delivery of, this computer software and information, the
  22.  * rights of the Government regarding its use, reproduction and disclosure
  23.  * are as set forth in Section 52.227-19 of the FARS Computer
  24.  * Software-Restricted Rights clause.
  25.  * 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  28.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  29.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  30.  * P.O. Box 2328, Corvallis, Oregon 97339.
  31.  *
  32.  * This computer software and information is distributed with "restricted
  33.  * rights."  Use, duplication or disclosure is subject to restrictions as
  34.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  35.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  36.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  37.  * then the "Alternate III" clause applies.
  38.  *
  39.  **************************************************************************/
  40.  
  41. #include <locale>
  42. #include <string>
  43. #include <deque>
  44. #include <stdexcept>
  45. #include <iostream>
  46. #include <sstream>
  47.  
  48. #include <locale.h> 
  49.  
  50. #ifndef _RWSTD_NO_NAMESPACE
  51. using namespace std;
  52. #endif
  53.  
  54. //Stock exchange displays stock quote at various
  55. //exchanges distributed globally. Stock prices varies
  56. //at random(so much for real life!).
  57.  
  58. //Objective of the example is to show money,
  59. //number,date and time formated using facets
  60.  
  61. static size_t rand_   = 0;  //to manipulate radomness of stocks
  62. static struct tm* tmb = 0;  //to use by 'localTime()'
  63.   
  64. struct Company
  65. {
  66.   public:
  67.     //
  68.    Company(const string&s, double p):companyName(s),                                                                offerPrice(p){} 
  69.  
  70.    //In actuality a company should not manipulate stocks.
  71.    //For simplicity let the company play with its own stock.
  72.  
  73.    void updateStock()
  74.    {
  75.       double change =offerPrice+randomChange(offerPrice);
  76.       if(randomChange(marketOutlook())%2)
  77.                 stockPrice+=change;
  78.           else
  79.                 stockPrice-=change;
  80.       if(stockPrice<0)stockPrice=-(stockPrice);
  81.    }
  82.    unsigned long randomChange (unsigned long i) 
  83.    { 
  84.        if(i)return rand() % i;
  85.        else return rand() % 2;
  86.    }
  87.    string companyName;
  88.    double offerPrice; //initial offer price
  89.    double stockPrice; //current market price
  90.   private:
  91.    string companyNews;//Make use of messaging (if at all it works !)
  92.    size_t marketOutlook(){ return rand_++;}
  93. };
  94.  
  95. class StockXchange: public locale::facet
  96. {
  97.   public:
  98.      //
  99.    typedef basic_ostream<char,char_traits<char> > outStream;
  100.    typedef ostreambuf_iterator<char, char_traits<char> > iter_type;
  101.    typedef deque<Company*,allocator<Company*> > database;
  102.      
  103.    StockXchange(size_t refs=0):locale::facet(refs){}
  104.    StockXchange(const  StockXchange& se)
  105.                             {companyDatabase = se.companyDatabase;}
  106.    virtual ~StockXchange(){}
  107.    
  108.    static locale::id id;
  109.    virtual  bool      put(ostream& os) const;
  110.    virtual  void      add(const string& name, double initPrice);
  111.    virtual  void      localTime(ostream&) const;
  112.  
  113.   protected:
  114.    database companyDatabase;  
  115.    friend StockXchange::outStream& operator<<
  116.    (StockXchange::outStream&, const StockXchange&);
  117.  
  118.   private: 
  119.    locale::id &get_id (void) const { return id; }
  120. };
  121.  
  122. class TokyoStockXchange : public StockXchange
  123. {
  124.    public:
  125.       //
  126.     TokyoStockXchange(size_t refs=0)
  127.                      :StockXchange(refs){}
  128.    
  129.     virtual  void      localTime(ostream& os) const
  130.      {
  131.        time_t tm = time(NULL);
  132.        tmb = gmtime(&tm); 
  133.        StockXchange::localTime(os);
  134.      }
  135.  
  136.     virtual  bool  put(ostream& os) const 
  137.      { 
  138.        os<<'\n';
  139.        os<<"######## TOKYO STOCK EXCHANGE #########"<<endl;
  140.        if(StockXchange::put(os)) return 1;
  141.        else return 0;
  142.      }
  143. }; 
  144.  
  145. class LondonStockXchange : public StockXchange
  146. {
  147.    public:
  148.       //
  149.  
  150.     LondonStockXchange(size_t refs=0)
  151.                       :StockXchange(refs){}
  152.    
  153.     virtual  void      localTime(ostream& os) const
  154.      {
  155.        time_t tm = time(NULL);
  156.        tmb = gmtime(&tm); 
  157.        StockXchange::localTime(os);
  158.      }
  159.  
  160.     virtual  bool   put(ostream& os) const 
  161.      { 
  162.        os<<'\n';
  163.        os<<"######## LONDON STOCK EXCHANGE #########"<<endl;
  164.        if(StockXchange::put(os)) return 1;
  165.        else return 0;
  166.      }
  167. }; 
  168.  
  169. class FrankFurtStockXchange : public StockXchange
  170. {
  171.    public:
  172.       //
  173.     FrankFurtStockXchange(size_t refs=0)
  174.                          :StockXchange(refs){}
  175.  
  176.     virtual  void      localTime(ostream& os) const
  177.      {
  178.        time_t tm = time(NULL);
  179.        tmb = gmtime(&tm); 
  180.        StockXchange::localTime(os);
  181.      }
  182.  
  183.     virtual  bool      put(ostream& os) const 
  184.      { 
  185.        os<<'\n';
  186.        os<<"######## FRANKFURTER WERTPAPIERB\366RSE #########"<<endl;
  187.        if(StockXchange::put(os)) return 1;
  188.        else return 0; 
  189.      }
  190. };
  191.  
  192. class NewYorkStockXchange : public StockXchange
  193. {
  194.    public:
  195.       //
  196.     NewYorkStockXchange(size_t refs=0)
  197.                        :StockXchange(refs){}
  198.  
  199.     virtual  void      localTime(ostream& os) const
  200.      {
  201.        time_t tm = time(NULL);
  202.        tmb = localtime(&tm); 
  203.        StockXchange::localTime(os);
  204.      }
  205.  
  206.     virtual  bool    put(ostream& os) const 
  207.      { 
  208.        os<<'\n';
  209.        os<<"######## NEW YORK STOCK EXCHANGE ########"<<endl;
  210.        if(StockXchange::put(os)) return 1;
  211.        else return 0; 
  212.      }
  213. }; 
  214.  
  215. class ParisStockXchange : public StockXchange
  216. {
  217.    public:
  218.       //
  219.     ParisStockXchange(size_t refs=0)
  220.                      :StockXchange(refs){}
  221.  
  222.     virtual  void      localTime(ostream& os) const
  223.      {
  224.        time_t tm = time(NULL);
  225.        tmb = gmtime(&tm); 
  226.        StockXchange::localTime(os);
  227.      }
  228.  
  229.     virtual  bool   put(ostream& os) const
  230.      { 
  231.        os<<'\n';
  232.        os<<"######## BOURSE DE PARIS #########"<<endl;
  233.        if(StockXchange::put(os)) return 1;
  234.        else return 0;     
  235.      }
  236. };
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.