home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Examples / StdLib / stocks.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  7.8 KB  |  246 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.  ***************************************************************************
  9.  *
  10.  * (c) Copyright 1994, 1998 Rogue Wave Software, Inc.
  11.  * ALL RIGHTS RESERVED
  12.  *
  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. #include "stocks.h"
  44.  
  45. locale::id   StockXchange::id;
  46.  
  47. #if defined(WIN32) || defined(_WIN32)
  48. #define US_LOCALE             "us"
  49. #define UK_LOCALE             "uk"
  50. #define GERMAN_LOCALE         "deu"
  51. #define FRENCH_LOCALE         "fra"
  52. #define JAPANESE_LOCALE       "jpn"
  53. #else
  54. #define US_LOCALE             "en_US"
  55. #define UK_LOCALE             "en_UK"
  56. #define GERMAN_LOCALE         "de"
  57. #define FRENCH_LOCALE         "fr"
  58. #define JAPANESE_LOCALE       "jp"
  59. #endif /* WIN32 */
  60.  
  61. void  StockXchange::localTime(ostream& os) const
  62. {
  63.    struct tm timeb;
  64.    memcpy(&timeb,tmb,sizeof(struct tm));
  65.    char pat[] = "%b %d %X %p";
  66.    const time_put<char,iter_type>& tp = 
  67. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  68.    use_facet<time_put<char,iter_type> >(os.getloc());
  69. #else
  70.    use_facet(os.getloc(),(time_put<char,iter_type>*)0);
  71. #endif
  72.    iter_type begin(os);
  73.    os<<"\t [";
  74.    tp.put(begin,os,' ',&timeb,pat,pat+11);
  75.    os<<"]"<<'\n';    
  76. }
  77.  
  78. void  StockXchange::add(const string& name, double initPrice)
  79. {
  80.    companyDatabase.push_front(new Company(name,initPrice));     
  81. }
  82.  
  83. bool  StockXchange::put(ostream& os) const
  84.    locale loc = os.getloc();
  85.    localTime(os); //display the local time
  86.  
  87.    const moneypunct<char,false>& mpunct =
  88. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  89.          use_facet<moneypunct<char,false> >(loc);
  90. #else
  91.          use_facet(loc,(moneypunct<char,false>*)0);
  92. #endif
  93.  
  94.    const num_put<char,iter_type>& np = 
  95. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  96.          use_facet<num_put<char,iter_type> >(loc);
  97. #else
  98.          use_facet(loc,(num_put<char,iter_type>*)0);
  99. #endif
  100.  
  101.    os<<'\n';
  102.    os<<"Company"<<"\t\t\t"<<"Initial Price"<<"\t"<<"Current Price"<<"\t"<<"Volume"<<endl;
  103.    os<<"-------"<<"\t\t\t"<<"------------"<<"\t"<<"----------"<<"\t"<<"______"<<endl;  
  104.    os<<'\n';
  105.    
  106.    iter_type itbegin(os);//ostream-buf iterator  
  107.    database::const_iterator begin = companyDatabase.begin();
  108.    database::const_iterator end   = companyDatabase.end();
  109.  
  110.    while(begin<end)
  111.       {
  112.         Company *info = *begin++;
  113.         info->updateStock();
  114.         os<<info->companyName<<"\t\t";
  115.         os<<mpunct.curr_symbol();
  116.         np.put(itbegin,os,' ',info->offerPrice);
  117.         os<<"\t\t";
  118.         os<<mpunct.curr_symbol();
  119.         np.put(itbegin,os,' ',info->stockPrice);
  120.         os<<"\t\t";
  121.         np.put(itbegin,os,' ',info->randomChange(info->stockPrice+10000));
  122.         os<<'\n'; 
  123.       }
  124.    return 1;
  125.  
  126. }
  127. ostream & operator<<(ostream& os, const StockXchange&)
  128. {
  129.     locale loc = os.getloc();
  130.     const StockXchange& se_facet =
  131. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  132.          use_facet<StockXchange >(loc);
  133. #else
  134.          use_facet(loc,(StockXchange*)0);
  135. #endif
  136.          se_facet.put(os);
  137.     return os;
  138. }
  139.  
  140.  
  141. int main()
  142. {
  143. #ifndef _RWSTD_NO_NAMESPACE
  144.     using namespace std;
  145. #endif
  146. #if defined(_MSC_VER)
  147.     //to workaround a bug in msvc 5.0
  148.     cin.rdbuf();
  149. #endif
  150.     typedef pair<StockXchange*, locale> sl_pair;
  151.     typedef deque<sl_pair*, allocator<sl_pair*> > Xchange;
  152.     Xchange sXchange;
  153.     
  154.     ostream       os(cout.rdbuf());
  155.  
  156.     //Add some hypothetical companies that went public.
  157.     //("Company name" , "initial stock price")
  158.    
  159.     NewYorkStockXchange *nse = new NewYorkStockXchange;
  160.     nse->add("Hyper Software",20.50);
  161.     nse->add("Florida Fish",15.10);
  162.     nse->add("Inka Inc",9.50);
  163.     nse->add("Emory Chemicals",11.00);
  164.  
  165.     TokyoStockXchange* tse = new TokyoStockXchange;
  166.     tse->add("Akiro Electronics",12.30);
  167.  
  168.     LondonStockXchange* lse   = new LondonStockXchange;   
  169.     lse->add("Royal Beef",7.25);
  170.     lse->add("Island Banks",34.00);
  171.  
  172.     FrankFurtStockXchange* fse   = new FrankFurtStockXchange;   
  173.     fse->add("B\166rsen-Software",9.75);
  174.     fse->add("M\174nchner R\174ck",19.75);
  175.     
  176.     ParisStockXchange* pse   = new ParisStockXchange;   
  177.     pse->add("Wines Inc.",11.50);
  178.     pse->add("Eiffel Co.",11.50);
  179.  
  180.     const char *p=setlocale(LC_ALL,UK_LOCALE);
  181.     if (!p) cerr<<'\n'<<"Not a valid locale: UK_LOCALE"<<endl;
  182.      else{
  183.           os.imbue(locale(locale(UK_LOCALE),lse));
  184.           sXchange.push_front(new sl_pair(pse,os.getloc()));
  185.           os<<*lse;
  186.          }
  187.  
  188.     p = setlocale(LC_ALL,US_LOCALE);
  189.     if (!p) cerr<<'\n'<<"Not a valid locale: US_LOCALE"<<endl;
  190.      else{
  191.           os.imbue(locale(locale(US_LOCALE),nse));
  192.           sXchange.push_front(new sl_pair(nse,os.getloc()));
  193.           os<<*nse;
  194.          }
  195.  
  196.     p = setlocale(LC_ALL,GERMAN_LOCALE);
  197.     if (!p) cerr<<'\n'<<"Not a valid locale: GERMAN_LOCALE"<<endl;
  198.      else{
  199.           os.imbue(locale(locale(GERMAN_LOCALE),fse));
  200.           sXchange.push_front(new sl_pair(fse,os.getloc()));
  201.           os<<*fse;
  202.          }
  203.  
  204.     p = setlocale(LC_ALL,FRENCH_LOCALE);
  205.     if (!p) cerr<<'\n'<<"Not a valid locale: FRENCH_LOCALE"<<endl;
  206.      else{
  207.           os.imbue(locale(locale(FRENCH_LOCALE),pse));
  208.           sXchange.push_front(new sl_pair(pse,os.getloc()));
  209.           os<<*pse;
  210.          }
  211.  
  212.     p = setlocale(LC_ALL,JAPANESE_LOCALE);
  213.     if (!p) cerr<<'\n'<<"Not a valid locale: JAPANESE_LOCALE"<<endl;
  214.      else{
  215.           os.imbue(locale(locale(JAPANESE_LOCALE),tse));
  216.           sXchange.push_front(new sl_pair(tse,os.getloc()));
  217.           os<<*tse;
  218.          }
  219.  
  220.     char q = 0;    
  221.     for(;;) 
  222.      {
  223.        cout<<'\n'<<"Want to see another quote [enter 'q' to quit] ?"; 
  224.        cin>>q;
  225.        if(q!='q')
  226.        {
  227.          Xchange::const_iterator it_begin = sXchange.begin();
  228.          Xchange::const_iterator it_end   = sXchange.end();
  229.          while(it_begin<it_end)
  230.          {
  231.            os.imbue((*it_begin)->second);
  232.            os<<(*(*it_begin)->first);
  233.            it_begin++;
  234.          }
  235.        } else break;
  236.      }   
  237.     return 0;
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.