home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Examples / StdLib / stocks.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  7.0 KB  |  246 lines

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