注意点及解释在程序中都有体现,主函数中/**/注释掉的的代码模块都分别对应一部分,每个小模块都可以实现。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
/*oct ,dec ,hex*/
/*int a = 10;
cout << a << endl;
cout << oct << a << endl; //八进制输出
cout << dec << a << endl; //十进制输出
cout << hex << a << endl;*/ //十六进制输出
/*setw() setfill()*/
/*char *str = "helloworld";
cout << setw(20) << str << endl; //定义输出20个字符,不足20字符用空格补全(先补空后输出字符串)
cout << str << setw(20) << endl; //先输出字符串,后补空
cout << setfill('*') << setw(20) << str << endl; //1定义输出20个字符,不足用*补全
cout << setfill('*') << str << setw(20) << endl; //2正确(正确的意思是与1输出格式一样)
cout << setw(20) << setfill('*') << str << endl; //3正确
cout << str << setw(20) << setfill('*') << endl; //4错误(需要补全的内容不能放到最后输出)
cout << str << setfill('*') << setw(20) << endl;*/ //5正确
/*double b = 22.000000 / 7.0000;
cout << b << endl;
cout << setprecision(3) << b << endl; //保留三位数字
//cout << setiosflags(ios::scientific) << b << endl; //指数形式输出(注意:setiosflags 与 setprecision 合用)
cout << setiosflags(ios::fixed) << b << endl;*/ //可以控制小数点右边的数字个数( 与 setprecision 合用)
/*unsetf() setf()*/
/*int a = 1000,b = 2000;
cout.unsetf(ios::dec); //取消十进制,建立八进制输出
cout.setf(ios::oct);
cout << a << endl;
cout.unsetf(ios::oct); //取消八进制,建立十六进制输出
cout.setf(ios::hex);
cout << a << endl;
cout << b << endl;*/
/*char *str = "helloworld";
cout.width(20);
cout.setf(ios::left); //left左对齐 right右对齐
cout.fill('*');
cout << str << endl;*/
double b = 22.000000 / 5.0000;
cout.precision(4);
cout << b << endl;
cout.setf(ios::scientific);
cout << b << endl;
cout.setf(ios::fixed);
cout << b << endl;
return 0;
}