Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
ostream.cpp
Go to the documentation of this file.
1
29#include <climits> // for limits macrors
30#include <iomanip> // for setfill, setprecision, setw
31#include <ios> // for dec, hex, oct, showbase, showpos, etc.
32#include <iostream> // for cout
33#include <sstream> // for istringstream
34
35#include <examples.h>
36
37
38int main ()
39{
40 // create a read/write stringbuf object on tiny char
41 // and attach it to an istringstream object
42 std::istringstream in (std::ios::in | std::ios::out);
43
44 // tie the ostream object to the istringstream object
45 std::ostream out (in.rdbuf ());
46
47 // output integer values in decimal, octal and hexadecimal notation
48
49 // assume 16-bit short, 32-bit int, and 64-bit long or long long
50 const short i16_min = SHRT_MIN;
51 const short i16_max = SHRT_MAX;
52
53 const unsigned short u16_max = USHRT_MAX;
54
55 const int i32_min = INT_MIN;
56 const int i32_max = INT_MAX;
57
58 const unsigned int u32_max = UINT_MAX;
59
60#if INT_MAX < LONG_MAX
61
62 // sizeof (int) < sizeof (long) or there is no type wider than long
63 typedef long Int64;
64 typedef unsigned long UInt64;
65
66#elif LONG_MAX < LLONG_MAX
67
68 // sizeof (long) < sizeof (long long) and sizeof (long) == sizeof (int)
69 typedef long long Int64;
70 typedef unsigned long long UInt64;
71
72#else
73 // no 64-bit wide integer found
74# define NO_INT64
75#endif // INT_MAX < LONG_MAX < LLONG_MAX
76
77#ifndef NO_INT64
78
79 // assuming a 2's complement representation, compute LLONG_MIN,
80 // LLONG_MAX, and ULLONG_MAX (the C99 macros are not part of
81 // C++ '98 or C++ '03 and may not be #defined)
82 const Int64 i64_min = (UInt64 (-1) >> 1) + 1;
83 const Int64 i64_max = UInt64 (-1) >> 1;
84
85 const UInt64 u64_max = UInt64 (-1);
86
87#endif // NO_INT64
88
89 // output the '0' and "0x" prefix to indicate values in octal
90 // and hexadecimal notation
91 out << std::showbase;
92
93 // output the leading plus sign for positive signed integers
94 // (but not for unsigned integers)
95 out << std::showpos;
96
97 out << "int16_t min = "
98 << std::dec << std::setw (6) << i16_min << ' '
99 << std::oct << std::setw (7) << i16_min << ' '
100 << std::hex << i16_min << '\n';
101
102 out << "int16_t max = "
103 << std::dec << std::setw (6) << i16_max << ' '
104 << std::oct << std::setw (7) << i16_max << ' '
105 << std::hex << i16_max << '\n';
106
107 out << "uint16_t max = "
108 << std::dec << std::setw (6) << u16_max << ' '
109 << std::oct << std::setw (7) << u16_max << ' '
110 << std::hex << u16_max << '\n';
111
112
113 out << "int32_t min = "
114 << std::dec << std::setw (11) << i32_min << ' '
115 << std::oct << std::setw (12) << i32_min << ' '
116 << std::hex << i32_min << '\n';
117
118 out << "int32_t max = "
119 << std::dec << std::setw (11) << i32_max << ' '
120 << std::oct << std::setw (12) << i32_max << ' '
121 << std::hex << i32_max << '\n';
122
123 out << "uint32_t max = "
124 << std::dec << std::setw (11) << u32_max << ' '
125 << std::oct << std::setw (12) << u32_max << ' '
126 << std::hex << u32_max << '\n';
127
128#ifndef NO_INT64
129
130 out << "int64_t min = "
131 << std::dec << std::setw (20) << i64_min << ' '
132 << std::oct << std::setw (23) << i64_min << ' '
133 << std::hex << i64_min << '\n';
134
135 out << "int64_t max = "
136 << std::dec << std::setw (20) << i64_max << ' '
137 << std::oct << std::setw (23) << i64_max << ' '
138 << std::hex << i64_max << '\n';
139
140 out << "uint64_t max = "
141 << std::dec << std::setw (20) << u64_max << ' '
142 << std::oct << std::setw (23) << u64_max << ' '
143 << std::hex << u64_max << '\n';
144
145#else // if defined (NO_INT64)
146
147 // hardcoded for platforms with no 64-bit type
148 out << "int64_t min = -9223372036854775808 "
149 << "01000000000000000000000 "
150 << "0x8000000000000000\n";
151
152 out << "int64_t max = +9223372036854775807 "
153 << "0777777777777777777777 "
154 << "0x7fffffffffffffff\n";
155
156 out << "uint64_t max = 18446744073709551615 "
157 << "01777777777777777777777 "
158 << "0xffffffffffffffff\n";
159
160#endif // NO_INT64
161
162 const char sep[] = "----------------------------------------";
163
164 // output a leading portion of sep
165 out.write (sep, 10).put ('\n');
166
167
168 // set the field width to 10, the the padding character to '*'
169 // and and output the floating point pi constant with 2 digits
170 // after the decimal point
171
172 const double pi = 3.14159123;
173 out << std::noshowpos // do not output the leading plus sign
174 << std::setprecision (3) // two digits after the decimal point
175 << std::setfill ('*') // padded to width with the asterisk
176 << std::setw (10) // field width
177 << pi
178 << '\n';
179
180 // output the entire contents of the buffer to standard output
181 std::cout << in.rdbuf ();
182
183 return 0;
184}
const int out
const int in
#define SHRT_MAX
Definition limits.h:64
#define INT_MIN
Definition limits.h:80
#define USHRT_MAX
Definition limits.h:72
#define INT_MAX
Definition limits.h:76
#define UINT_MAX
Definition limits.h:84
#define SHRT_MIN
Definition limits.h:68
int main()
Definition ostream.cpp:38