-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExer17_34.cpp
130 lines (130 loc) · 4.88 KB
/
Exer17_34.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <iomanip>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
using std::boolalpha;
using std::noboolalpha;
using std::hex;
using std::dec;
using std::oct;
using std::showbase;
using std::noshowbase;
using std::uppercase;
using std::nouppercase;
using std::setprecision;
using std::scientific;
using std::fixed;
using std::hexfloat;
using std::defaultfloat;
using std::showpoint;
using std::noshowpoint;
using std::setw;
using std::left;
using std::right;
using std::internal;
using std::setfill;
using std::noskipws;
using std::skipws;
using std::setbase;
using std::sqrt;
int main()
{
// format of bool values
cout << "default bool values: " << true << " " << false
<< "\nalpha bool values: " << boolalpha
<< true << " " << false << endl;
bool bool_val = cout.good();
cout << boolalpha // sets the internal state of cout
<< bool_val
<< noboolalpha // resets the internal state to default formatting
<< '\n'
<< endl;
// base for integral values
cout << "default: " << 20 << " " << 1024 << endl;
cout << "octal: " << oct << 20 << " " << 1024 << endl;
cout << "hex: " << hex << 20 << " " << 1024 << endl;
cout << "decimal: " << dec << 20 << " " << 1024 << endl;
cout << endl;
// indicating base on the output
cout << showbase; // show the base when printing integral values
cout << "default: " << 20 << " " << 1024 << endl;
cout << "octal: " << oct << 20 << " " << 1024 << endl;
cout << "hex: " << hex << 20 << " " << 1024 << endl;
cout << "decimal: " << dec << 20 << " " << 1024 << endl;
cout << noshowbase; // reset the state of the stream
cout << uppercase << showbase << hex
<< "printed in hexadecimal: " << 20 << " " << 1024
<< nouppercase << noshowbase << dec << endl;
cout << endl;
// format of floating-point values
// specify precision
// cout.precision reports the current precision value
cout << "Precision: " << cout.precision()
<< ", Value: " << sqrt(2.0) << endl;
// cout.precision(12) asks that 12 digits of precision be printed
cout.precision(12);
cout << "Precision: " << cout.precision()
<< ", Value: " << sqrt(2.0) << endl;
// alternative way to set precision using the setprecision manipulator
cout << setprecision(3);
cout << "Precision: " << cout.precision()
<< ", Value: " << sqrt(2.0) << endl;
cout << setprecision(6);
cout << endl;
// specifying the notation of floating numbers
cout << "default format: " << 100 * sqrt(2.0) << '\n'
<< "scientific: " << scientific << 100 * sqrt(2.0) << '\n'
<< "fixed decimal: " << fixed << 100 * sqrt(2.0) << '\n'
<< "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << '\n' // result has problem, see below
<< "use defaults: " << defaultfloat << 100 * sqrt(2.0)
<< "\n\n";
// printing the decimal point
cout << 10.0 << endl; // prints 10
cout << showpoint << 10.0 // prints 10.0000
<< noshowpoint << endl; // revert to default format for the decimal point
cout << endl;
// padding the output
int i = -16;
double d = 3.14159;
// pad the forst column to use a minimum of 12 positions in the output
cout << "i: " << setw(12) << i << "next col" << '\n'
<< "d: " << setw(12) << d << "next col" << '\n';
// pad the first column and left-justify all columns
cout << left
<< "i: " << setw(12) << i << "next col" << '\n'
<< "d: " << setw(12) << d << "next col" << '\n'
<< right; // restore normal justification
// pad the first column and right-justify all columns
cout << right
<< "i: " << setw(12) << i << "next col" << '\n'
<< "d: " << setw(12) << d << "next col" << '\n';
// pad the first column but put the padding internal to to field
cout << internal
<< "i: " << setw(12) << i << "next col" << '\n'
<< "d: " << setw(12) << d << "next col" << '\n';
// pad the first column, using # as the pad character
cout << setfill('#')
<< "i: " << setw(12) << i << "next col" << '\n'
<< "d: " << setw(12) << d << "next col" << '\n'
<< setfill(' '); // restore the normal pad character
cout << endl;
// controlling input formatting
char ch;
cin >> noskipws; // set cin so that it reads whitespace
while (cin >> ch)
cout << ch;
cin >> skipws; // reset cin to the default state so that it discards whitespace
// setbase
cout << setbase(16)
<< "i: " << i << '\n'
<< "d: " << d << '\n' // floating-point number is not influenced.
<< setbase(10)
<< endl;
return 0;
}
// ******result of cout << "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << '\n'******
// hexadecimal: 0x8.d6bde009b35cp+4(gcc/clang)
// hexadecimal: 0x1.1ad7bcp+7(cl)
// reason is unknown for now.