[c++ primer plus]使用cout格式化输出
2007-10-22 10:44
567 查看
1.进制
cout << _T(" 17.3 修改显示时使用的计数系统") << endl;
int n;
n = 255;
dec(cout); // cout << dec;
cout << "dec : " << n << endl;
oct(cout); // cout << oct;
cout << "oct : " << n << endl;
hex(cout); // cout << hex ;
cout << "hex : " << n << endl;
dec(cout); // 不要影响其它人
[align=left] [/align]
[align=left]2. 输出宽度[/align]
cout << _T(" 17.4 调整字段宽度") << endl;
int w = cout.width(30);
cout << "default field width = " << w << ":" << endl;
cout.width(5);
cout << "N" << ": ";
cout.width(8);
cout << "N * N" << ":" << endl;
for(long i=1; i<=100; i*=10)
{
cout.width(5);
cout << i << ": ";
cout.width(8);
cout << i * i << ":" << endl;
}
cout.width();
3. 填充字符
cout << _T(" 17.5 填充字符") << endl;
cout.fill('*');
int w = cout.width(30);
cout.width(5);
cout << "N" << ": ";
cout.width(8);
cout << "N * N" << ":" << endl;
for(long i=1; i<=100; i*=10)
...{
cout.width(5);
cout << i << ": ";
cout.width(8);
cout << i * i << ":" << endl;
}
cout.width();
cout << endl << endl;
4. 浮点数显示精度
cout << _T(" 17.6 设置浮点数的显示精度") << endl;
float f1 = 23.3232;
float f2 = 1.9 + 8.0 / 9.0;
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
// 设置精度为2
cout.precision(2);
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
// 解除设定
cout.precision();
5. setf用法
cout << _T(" 17.7 设置精度为2打印末位的0和小数点") << endl;
float f1 = 23.3232;
float f2 = 1.9 + 8.0 / 9.0;
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
// 设置精度为2打印末位的0和小数点
cout.setf(ios_base::showpoint);
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
// 解除设定
cout.setf(ios_base::unitbuf);
6. setf高级用法
cout << _T(" 17.7 setf 高级用法") << endl;
cout.setf(ios_base::showpos);
cout << 63 << endl;
cout.setf(ios_base::uppercase);
cout << "A string" << endl;
cout.setf(ios_base::showbase);
cout << 437 << endl;
cout.setf(ios_base::boolalpha);
cout << true << endl;
cout << false << endl;
相关文章推荐
- [学习笔记][C++Primer Plus]使用cout格式化输出字符串,3q xuzhong
- [学习笔记][C++Primer Plus]使用cout格式化输出字符串
- c++primer plus第十七章-cin输入、cout输出和文件I/O
- 如何在C++中使用cout进行高级的格式化输出操作
- [c++ primer plus]使用标准流格式化输入
- 使用cout进行格式化输出(1)
- C++零食:wprintf 中使用%I64d格式化输出LONGLONG
- 使用cout格式化输出字符串
- C Primer Plus 5th 翻译 第四章:字符串和格式化输入/输出(二)
- 2012/2/11 《C++ Primer Plus》第十七章:输入、输出和文件 学习笔记
- c++ primer plus 习题5.7(使用new为结构创建动态数组)
- C++中格式化cout输出
- 《C++ Primer Plus(第六版)》(21)(第十一章 使用类 编程题答案2)
- C++ cout格式化输出
- C++ cout 格式化输出方法
- c++ cout 格式化输出浮点数、整数及格方法
- 《C++ Primer Plus(第六版)》(39)(第十七章 输入、输出和文件 笔记2)
- 《C++ Primer Plus》第11章 使用类 学习笔记
- 【C++ Primer plus】【第十一章】使用类
- 【《C++ Primer Plus》读书笔记】第11章 使用类