[学习笔记][C++Primer Plus]使用cout格式化输出字符串
2006-04-30 00:34
1231 查看
cout是比较常用的功能,应该了解它的一些特性。
/******************************************************************** created: 2006/04/29 created: 29:4:2006 23:33 filename: e:\work\C++\输入输出和文件\CoutFormatSample.cpp file path: e:\work\C++\输入输出和文件 file base: CoutFormatSample file ext: cpp author: xuzhong purpose: *********************************************************************/ #include "StdAfx.h" #include ".\coutformatsample.h" using namespace std; // public CCoutFormatSample::CCoutFormatSample(void) { } CCoutFormatSample::~CCoutFormatSample(void) { } void CCoutFormatSample::Run(void) { this->P_17_3(); this->P_17_4(); this->P_17_5(); this->P_17_6(); this->P_17_7(); this->P_17_8(); } // private // 例子在 P615 。修改显示时使用的计数系统 void CCoutFormatSample::P_17_3(void) { cout << _T(" 17.3 修改显示时使用的计数系统") << endl << endl; cout << "Enter an integer : "; int n; // cin >> 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); // 不要影响其它人 cout << endl << endl; } void CCoutFormatSample::P_17_4(void) { cout << _T(" 17.4 调整字段宽度") << endl << 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(); cout << endl << endl; } // cout.fill的用法 void CCoutFormatSample::P_17_5(void) { cout << _T(" 17.5 填充字符") << endl << 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; } // cout.precision void CCoutFormatSample::P_17_6(void) { cout << _T(" 17.6 设置浮点数的显示精度") << endl << 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(); cout << endl << endl; } // cout.setf void CCoutFormatSample::P_17_7(void) { cout << _T(" 17.7 设置精度为2打印末位的0和小数点") << endl << 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); cout << endl << endl; } // cout.setf 高级用法 void CCoutFormatSample::P_17_8() { cout << _T(" 17.7 setf 高级用法") << endl << 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; cout << endl << endl; } |
相关文章推荐
- [学习笔记][C++Primer Plus]使用cout格式化输出字符串,3q xuzhong
- [c++ primer plus]使用cout格式化输出
- C Primer Plus学习笔记----第四章字符串和格式化输入/输出
- [C++ Primer Plus]学习笔记--每次读取一行字符串输入
- 《C++ Primer Plus》第11章 使用类 学习笔记
- 2012/1/25 《C++ Primer Plus》第十一章:使用类 学习笔记
- 2012/1/25 《C++ Primer Plus》第十一章:使用类 学习笔记
- C++PrimerPlus第十一章学习笔记——使用类
- 2012/2/11 《C++ Primer Plus》第十七章:输入、输出和文件 学习笔记
- C++ Primer Plus 学习笔记 第五章 01 for循环 递增运算符和递减运算符 复合语句,逗号运算符 C风格字符串比较、string类字符串比较
- (学习)C Primer Plus(3)——字符串和格式化输入/输出
- 【C语言学习】《C Primer Plus》第4章 字符串和格式化输入/输出
- 2012/2/11 《C++ Primer Plus》第十七章:输入、输出和文件 学习笔记
- [学习笔记][C++Primer Plus]String类的使用
- 《C++ Primer Plus》第17章 输入、输出和文件 学习笔记
- 2012/1/14 《C++ Primer Plus》第六章:分支语句和逻辑操作符 学习笔记
- 使用C++ ostringstream来格式化字符串输出
- C Primer Plus 第4章 字符串和格式化输入输出4.4 printf()和scanf()
- 《C++ Primer Plus》第15章 友元、异常和其他 学习笔记
- 字符串的输入与停止(如何输入空字符串)(C++ primer 学习笔记)