Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
stocks.cpp
Go to the documentation of this file.
1
30#include <clocale>
31#include <iostream>
32#include <locale>
33
34#include <stocks.h>
35#include <examples.h>
36
37
38// definition of the required facet id
39std::locale::id StockXchange::id;
40
41
43{
44 database::const_iterator begin = companyDatabase.begin ();
45 database::const_iterator end = companyDatabase.end ();
46
47 while (begin < end)
48 delete *begin++;
49}
50
51
52void StockXchange::localTime (std::ostream& os) const
53{
54 const char pat[] = "%c";
55
56 typedef std::time_put<char, iter_type> TimePut;
57 const TimePut& tp = std::use_facet<TimePut>(os.getloc ());
58 iter_type begin (os);
59
60 os << "\t [";
61 tp.put (begin, os, ' ', &tmb, &pat [0], pat + sizeof pat - 1);
62 os << "]\n";
63}
64
65
66void StockXchange::add (const std::string& name, double initPrice)
67{
68 companyDatabase.push_front (new Company (name, initPrice));
69}
70
71
72bool StockXchange::put (std::ostream& os) const
73{
74 std::locale loc = os.getloc ();
75 localTime (os); //display the local time
76
77 typedef std::moneypunct<char, false> MoneyPunct;
78 const MoneyPunct &mpunct = std::use_facet<MoneyPunct>(loc);
79
80 typedef std::num_put<char, iter_type> NumPut;
81 const NumPut &np = std::use_facet<NumPut>(loc);
82
83 os << "\nCompany" << "\t\t\t" << "Initial Price" << "\t"
84 << "Current Price" << "\t" << "Volume\n"
85 << "-------" << "\t\t\t" << "------------" << "\t"
86 << "----------" << "\t" << "______\n\n";
87
88 iter_type itbegin (os);
89
90 database::const_iterator begin = companyDatabase.begin ();
91 database::const_iterator end = companyDatabase.end ();
92
93 while (begin < end) {
94
95 Company *info = *begin++;
96 info->updateStock ();
97
98 os << info->companyName << "\t\t";
99 os << mpunct.curr_symbol ();
100 np.put (itbegin, os, ' ', info->offerPrice);
101 os << "\t\t";
102 os << mpunct.curr_symbol ();
103 np.put (itbegin, os, ' ', info->stockPrice);
104 os << "\t\t";
105 long volume = stock_change[info->changeIndex ()]*1000L;
106 if (volume < 0)
107 volume = -volume;
108 np.put (itbegin, os, ' ', volume);
109 os << '\n';
110 }
111
112 return true;
113}
114
115
116std::ostream & operator<< (std::ostream& os, const StockXchange&)
117{
118 const std::locale loc = os.getloc ();
119 const StockXchange &se_facet = std::use_facet<StockXchange>(loc);
120 se_facet.put (os);
121 return os;
122}
123
124
125int main ()
126{
127 typedef std::pair<StockXchange*, std::locale> sl_pair;
128 typedef std::deque<sl_pair, std::allocator<sl_pair> > Xchange;
129 Xchange sXchange;
130
131 std::ostream os (std::cout.rdbuf ());
132 const std::locale loc (os.getloc ());
133
134 const char *p = std::setlocale (LC_ALL, US_LOCALE);
135 if (!p)
136 std::cerr << "\nNot a valid locale: " << US_LOCALE << '\n';
137 else {
139 nse->add ("Hyper Software", 20.50);
140 nse->add ("Florida Fish", 15.10);
141 nse->add ("Inka Inc", 9.50);
142 nse->add ("Emory Chemicals", 11.00);
143
144 os.imbue (std::locale (std::locale (US_LOCALE), nse));
145 sXchange.push_front (sl_pair (nse, os.getloc ()));
146 os << *nse;
147 }
148
149 p = std::setlocale (LC_ALL, GERMAN_LOCALE);
150 if (!p)
151 std::cerr<< "\nNot a valid locale: " << GERMAN_LOCALE << '\n';
152 else {
154 fse->add ("B\166rsen-Software", 9.75);
155 fse->add ("M\174nchner R\174ck", 19.75);
156
157 os.imbue (std::locale (std::locale (GERMAN_LOCALE), fse));
158 sXchange.push_front (sl_pair (fse, os.getloc ()));
159 os << *fse;
160 }
161
162 p = std::setlocale (LC_ALL, FRENCH_LOCALE);
163 if (!p)
164 std::cerr << "\nNot a valid locale: " << FRENCH_LOCALE << '\n';
165 else {
167 pse->add ("Wines Inc.", 11.50);
168 pse->add ("Eiffel Co.", 11.50);
169
170 os.imbue (std::locale (std::locale (FRENCH_LOCALE), pse));
171 sXchange.push_front (sl_pair (pse, os.getloc ()));
172 os << *pse;
173 }
174
175 p = std::setlocale (LC_ALL, JAPANESE_LOCALE);
176 if (!p)
177 std::cerr << "\nNot a valid locale: " << JAPANESE_LOCALE << '\n';
178 else {
180 tse->add ("Akiro Electronics", 12.30);
181
182 os.imbue (std::locale (std::locale (JAPANESE_LOCALE), tse));
183 sXchange.push_front (sl_pair (tse, os.getloc ()));
184 os << *tse;
185 }
186
187 for (char q = '\0'; ; ) {
188 std::cout << "\nWant to see another quote [enter 'q' to quit] ?";
189 std::cin >> q;
190 if (q != 'q') {
191 Xchange::const_iterator it_begin = sXchange.begin ();
192 Xchange::const_iterator it_end = sXchange.end ();
193 while (it_begin < it_end) {
194 os.imbue ((*it_begin).second);
195 os << (*(*it_begin).first);
196 it_begin++;
197 }
198 }
199 else
200 break;
201 }
202
203 os << '\n';
204
205 os.imbue (loc);
206
207 return 0;
208}
#define q
Definition 17.names.cpp:133
#define os
Definition 17.names.cpp:511
#define loc
Definition 17.names.cpp:492
#define p
Definition 17.names.cpp:132
std::time_put< char > TimePut
const int end
#define LC_ALL
const char * name
Definition codecvt1.cpp:72
int main()
Definition stocks.cpp:125
std::ostream & operator<<(std::ostream &os, const StockXchange &)
Definition stocks.cpp:116
#define JAPANESE_LOCALE
Definition stocks.h:84
#define GERMAN_LOCALE
Definition stocks.h:82
#define FRENCH_LOCALE
Definition stocks.h:83
#define US_LOCALE
Definition stocks.h:81
database companyDatabase
Definition stocks.h:152
static std::locale::id id
Definition stocks.h:146
virtual ~StockXchange()
Definition stocks.cpp:42
virtual bool put(std::ostream &os) const
Definition stocks.cpp:72
virtual void localTime(std::ostream &) const
Definition stocks.cpp:52
std::ostreambuf_iterator< char, std::char_traits< char > > iter_type
Definition stocks.h:138
virtual void add(const std::string &name, double initPrice)
Definition stocks.cpp:66