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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /**************************************************************************
  4.  *
  5.  * stocks.cpp - Example program of money_punct,num_put,time_put facets.
  6.  *            
  7.  *
  8.  * $Id: stocks.cpp,v 2.0 1997/07/14 11:39:35 sree Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED *
  14.  * The software and information contained herein are proprietary to, and
  15.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  16.  * intends to preserve as trade secrets such software and information.
  17.  * This software is furnished pursuant to a written license agreement and
  18.  * may be used, copied, transmitted, and stored only in accordance with
  19.  * the terms of such license and with the inclusion of the above copyright
  20.  * notice.  This software and information or any other copies thereof may
  21.  * not be provided or otherwise made available to any other person.
  22.  *
  23.  * Notwithstanding any other lease or license that may pertain to, or
  24.  * accompany the delivery of, this computer software and information, the
  25.  * rights of the Government regarding its use, reproduction and disclosure
  26.  * are as set forth in Section 52.227-19 of the FARS Computer
  27.  * Software-Restricted Rights clause.
  28.  * 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  31.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  32.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  33.  * P.O. Box 2328, Corvallis, Oregon 97339.
  34.  *
  35.  * This computer software and information is distributed with "restricted
  36.  * rights."  Use, duplication or disclosure is subject to restrictions as
  37.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  38.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  39.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  40.  * then the "Alternate III" clause applies.
  41.  *
  42.  **************************************************************************/
  43.  
  44. #include "stocks.h"
  45.  
  46. locale::id   StockXchange::id;
  47.  
  48. #if defined(WIN32) || defined(_WIN32)
  49. #define US_LOCALE             "us"
  50. #define UK_LOCALE             "uk"
  51. #define GERMAN_LOCALE         "deu"
  52. #define FRENCH_LOCALE         "fra"
  53. #define JAPANESE_LOCALE       "jpn"
  54. #else
  55. #define US_LOCALE             "en_US"
  56. #define UK_LOCALE             "en"
  57. #define GERMAN_LOCALE         "de"
  58. #define FRENCH_LOCALE         "fr"
  59. #define JAPANESE_LOCALE       "jp"
  60. #endif /* WIN32 */
  61.  
  62. void  StockXchange::localTime(ostream& os) const
  63. {
  64.    struct tm timeb;
  65.    memcpy(&timeb,tmb,sizeof(struct tm));
  66.    char pat[] = "%b %d %X %p";
  67.    const time_put<char,iter_type>& tp = 
  68. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  69.    use_facet<time_put<char,iter_type> >(os.getloc());
  70. #else
  71.    use_facet(os.getloc(),(time_put<char,iter_type>*)0);
  72. #endif
  73.    iter_type begin(os);
  74.    os<<"\t [";
  75.    tp.put(begin,os,' ',&timeb,pat,pat+11);
  76.    os<<"]"<<'\n';    
  77. }
  78.  
  79. void  StockXchange::add(const string& name, double initPrice)
  80. {
  81.    companyDatabase.push_front(new Company(name,initPrice));     
  82. }
  83.  
  84. bool  StockXchange::put(ostream& os) const
  85.    locale loc = os.getloc();
  86.    localTime(os); //display the local time
  87.  
  88.    const moneypunct<char,false>& mpunct =
  89. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  90.          use_facet<moneypunct<char,false> >(loc);
  91. #else
  92.          use_facet(loc,(moneypunct<char,false>*)0);
  93. #endif
  94.  
  95.    const num_put<char,iter_type>& np = 
  96. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  97.          use_facet<num_put<char,iter_type> >(loc);
  98. #else
  99.          use_facet(loc,(num_put<char,iter_type>*)0);
  100. #endif
  101.  
  102.    os<<'\n';
  103.    os<<"Company"<<"\t\t\t"<<"Initial Price"<<"\t"<<"Current Price"<<"\t"<<"Volume"<<endl;
  104.    os<<"-------"<<"\t\t\t"<<"------------"<<"\t"<<"----------"<<"\t"<<"______"<<endl;  
  105.    os<<'\n';
  106.    
  107.    iter_type itbegin(os);//ostream-buf iterator  
  108.    database::const_iterator begin = companyDatabase.begin();
  109.    database::const_iterator end   = companyDatabase.end();
  110.  
  111.    while(begin<end)
  112.       {
  113.         Company *info = *begin++;
  114.         info->updateStock();
  115.         os<<info->companyName<<"\t\t";
  116.         os<<mpunct.curr_symbol();
  117.         np.put(itbegin,os,' ',info->offerPrice);
  118.         os<<"\t\t";
  119.         os<<mpunct.curr_symbol();
  120.         np.put(itbegin,os,' ',info->stockPrice);
  121.         os<<"\t\t";
  122.         np.put(itbegin,os,' ',info->randomChange(info->stockPrice+10000));
  123.         os<<'\n'; 
  124.       }
  125.    return 1;
  126.  
  127. }
  128. ostream & operator<<(ostream& os, const StockXchange&)
  129. {
  130.     locale loc = os.getloc();
  131.     const StockXchange& se_facet =
  132. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  133.          use_facet<StockXchange >(loc);
  134. #else
  135.          use_facet(loc,(StockXchange*)0);
  136. #endif
  137.          se_facet.put(os);
  138.     return os;
  139. }
  140.  
  141.  
  142. int main()
  143. {
  144. #ifndef _RWSTD_NO_NAMESPACE
  145.     using namespace std;
  146. #endif
  147. #if defined(_MSC_VER)
  148.     //to workaround a bug in msvc 5.0
  149.     cin.rdbuf();
  150. #endif
  151.     typedef pair<StockXchange*, locale> sl_pair;
  152.     typedef deque<sl_pair*, allocator<sl_pair*> > Xchange;
  153.     Xchange sXchange;
  154.     
  155.     ostream       os(cout.rdbuf());
  156.  
  157.     //Add some hypothetical companies that went public.
  158.     //("Company name" , "initial stock price")
  159.    
  160.     NewYorkStockXchange *nse = new NewYorkStockXchange;
  161.     nse->add("Hyper Software",20.50);
  162.     nse->add("Florida Fish",15.10);
  163.     nse->add("Inka Inc",9.50);
  164.     nse->add("Emory Chemicals",11.00);
  165.  
  166.     TokyoStockXchange* tse = new TokyoStockXchange;
  167.     tse->add("Akiro Electronics",12.30);
  168.  
  169.     LondonStockXchange* lse   = new LondonStockXchange;   
  170.     lse->add("Royal Beef",7.25);
  171.     lse->add("Island Banks",34.00);
  172.  
  173.     FrankFurtStockXchange* fse   = new FrankFurtStockXchange;   
  174.     fse->add("B\166rsen-Software",9.75);
  175.     fse->add("M\174nchner R\174ck",19.75);
  176.     
  177.     ParisStockXchange* pse   = new ParisStockXchange;   
  178.     pse->add("Wines Inc.",11.50);
  179.     pse->add("Eiffel Co.",11.50);
  180.  
  181.     const char *p=setlocale(LC_ALL,UK_LOCALE);
  182.     if (!p) cerr<<'\n'<<"Not a valid locale: UK_LOCALE"<<endl;
  183.      else{
  184.           os.imbue(locale(locale(UK_LOCALE),lse));
  185.           sXchange.push_front(new sl_pair(pse,os.getloc()));
  186.           os<<*lse;
  187.          }
  188.  
  189.     p = setlocale(LC_ALL,US_LOCALE);
  190.     if (!p) cerr<<'\n'<<"Not a valid locale: US_LOCALE"<<endl;
  191.      else{
  192.           os.imbue(locale(locale(US_LOCALE),nse));
  193.           sXchange.push_front(new sl_pair(nse,os.getloc()));
  194.           os<<*nse;
  195.          }
  196.  
  197.     p = setlocale(LC_ALL,GERMAN_LOCALE);
  198.     if (!p) cerr<<'\n'<<"Not a valid locale: GERMAN_LOCALE"<<endl;
  199.      else{
  200.           os.imbue(locale(locale(GERMAN_LOCALE),fse));
  201.           sXchange.push_front(new sl_pair(fse,os.getloc()));
  202.           os<<*fse;
  203.          }
  204.  
  205.     p = setlocale(LC_ALL,FRENCH_LOCALE);
  206.     if (!p) cerr<<'\n'<<"Not a valid locale: FRENCH_LOCALE"<<endl;
  207.      else{
  208.           os.imbue(locale(locale(FRENCH_LOCALE),pse));
  209.           sXchange.push_front(new sl_pair(pse,os.getloc()));
  210.           os<<*pse;
  211.          }
  212.  
  213.     p = setlocale(LC_ALL,JAPANESE_LOCALE);
  214.     if (!p) cerr<<'\n'<<"Not a valid locale: JAPANESE_LOCALE"<<endl;
  215.      else{
  216.           os.imbue(locale(locale(JAPANESE_LOCALE),tse));
  217.           sXchange.push_front(new sl_pair(tse,os.getloc()));
  218.           os<<*tse;
  219.          }
  220.  
  221.     char q = 0;    
  222.     for(;;) 
  223.      {
  224.        cout<<'\n'<<"Want to see another quote [enter 'q' to quit] ?"; 
  225.        cin>>q;
  226.        if(q!='q')
  227.        {
  228.          Xchange::const_iterator it_begin = sXchange.begin();
  229.          Xchange::const_iterator it_end   = sXchange.end();
  230.          while(it_begin<it_end)
  231.          {
  232.            os.imbue((*it_begin)->second);
  233.            os<<(*(*it_begin)->first);
  234.            it_begin++;
  235.          }
  236.        } else break;
  237.      }   
  238.     return 0;
  239. }
  240.  
  241.  
  242.  
  243.