forked from SolerHo/cpp-Primer-Plus-6e-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetf2.cpp
54 lines (49 loc) · 1.71 KB
/
setf2.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
// 使用setf的第二个版本:两个参数的控制符
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
// use left justification, show the plus sign, show trailing
// zeros,with a precision of 3
cout.setf(ios_base::left, ios_base::adjustfield); // 左对齐
cout.setf(ios_base::showpos); // 在正数前面加上 +
cout.setf(ios_base::showpoint); // 显示末尾的小数点
cout.precision(3); // 精度为3位
// use e-notation and save old format setting
// 存储一个变量,方便可以重置回原来的变量控制格式
ios_base::fmtflags old = cout.setf(ios_base::scientific,ios_base::floatfield);
cout << "Left Justification : \n";
long n;
for (n = 1; n <= 41; n += 10)
{
cout.width(4);
cout << n << "|";
cout.width(12);
cout << sqrt(double(n)) << "|\n";
}
// change to internal justification
cout.setf(ios_base::internal, ios_base::adjustfield); // 符号或者基数前缀左对齐,值右对齐
// restore default floating point display styles
cout.setf(old,ios_base::floatfield);
cout << "Internal Justification : \n";
for (n = 1; n <= 41; n += 10)
{
cout.width(4);
cout << n << "|";
cout.width(12);
cout << sqrt(double(n)) << "|\n";
}
// use right justification, fixed notation
cout.setf(ios_base::right,ios_base::adjustfield); // 使用右对齐
cout.setf(ios_base::fixed,ios_base::floatfield); // 使用定点计数法
cout << "Right justification : \n";
for (n = 1;n <= 41; n += 10)
{
cout.width(4);
cout << n << "|";
cout.width(12);
cout << sqrt(double(n)) << "|\n";
}
return 0;
}