输出流的格式化

输出流的格式化要从两个方面来实现:

一、流操纵算子

     数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符。把它们作为插入操作符<<的输出对象即可。如setiosflags、setw、setfill、setprecision、hex、oct等


#include <iostream>
#include <iomanip>
using namespace std;

//通过操作子的方式进行格式化输出
//宽度控制
//对齐控制
// 填充控制
// 精度控制
// 进制控制
//

int main()
{
	system("chcp 936");
	int n=64;
	double d=123.45;
	double d1=0.012345;

	cout<<"========宽度控制=================="<<endl;
	cout<<n<<'#'<<endl;
	cout<<setw(10)<<n<<'#'<<n<<endl;//宽度控制不会影响下一个输出

	cout<<"========对齐控制=================="<<endl;

	cout<<setw(10)<<setiosflags(ios::left)<<n<<'#'<<endl;//对齐控制会影响下一个输出
	cout<<setw(10)<<n<<'#'<<endl;

	/*cout<<setw(10)<<setiosflags(ios::right)<<n<<'#'<<endl;*///还原对齐方式
	cout<<setw(10)<<resetiosflags(ios::left)<<n<<'#'<<endl;

	cout<<"========填充控制=================="<<endl;

	cout<<setw(10)<<setfill('?')<<n<<'#'<<endl;//填充控制会影响下一个输出
	cout<<setw(10)<<n<<'#'<<endl;
	cout<<setw(10)<<setfill(' ')<<n<<'#'<<endl;///还原原来的填充控制方式


	cout<<"========精度控制=================="<<endl;

      cout<<setprecision(4)<<d<<endl;    //设置有效数字
	  cout<<setprecision(3)<<d1<<endl;

	  cout<<setiosflags(ios::fixed)<<endl;//设置小数点后的有效数字

	  cout<<setprecision(4)<<d<<endl;    
	  cout<<setprecision(3)<<d1<<endl;

	  cout<<"========进制输出=================="<<endl;
	/*  cout <<n<<endl;
	  cout<<resetiosflags(ios::dec);
	  cout<<setiosflags(ios::oct)<<n<<endl;
	  cout<<hex<<n<<endl;
	  */
	   cout<<endl;
	    cout<<n<<endl;
	    cout<<oct<<n<<endl;
	    cout<<hex<<n<<endl;


		cout<<endl;
		cout<<setiosflags(ios::showbase)<<endl;//在进制前加上进制符号
		cout<<dec<<n<<endl;
		cout<<oct<<n<<endl;
		cout<<hex<<n<<endl;

		cout<<endl;
		cout<<setbase(10)<<n<<endl;
		cout<<setbase(8)<<n<<endl;
		cout<<setbase(16)<<n<<endl;

	return 0;
}



二、通过流的成员函数实现

通过调用流的成员函数控制格式,如setf、unsetf、width、fill、precision等。优点是在设置格式同时,可以返回以前的设置,便于恢复原来的设置。

ios类提供成员函数对流的状态进行检测和进行输入输出格式控制等操作

ios类的成员函数:





#include <iostream>
//#include <iomanip>
using namespace std;

//通过流的成员函数进行格式化输出
//宽度控制
//对齐控制
// 填充控制
// 精度控制
// 进制控制
//

int main()
{
	system("chcp 936");
	int n=64;
	double d=123.45;
	double d1=0.012345;

	cout<<"========宽度控制=================="<<endl;
	cout<<n<<'#'<<endl;
	//cout<<setw(10)<<n<<'#'<<n<<endl;//宽度控制不会影响下一个输出
	cout.width(10);
	cout<<n<<'#'<<endl;

	cout<<"========对齐控制=================="<<endl;
	cout.width(10);
	cout.setf(ios::left);
	cout<<n<<'#'<<endl;
	cout.width(10);
	cout<<n<<'#'<<endl;
	//还原对齐方式
	/*cout.width(10);
	cout.setf(ios::right);
	cout<<n<<'#'<<endl;*/
	cout.width(10);
	cout.unsetf(ios::left);
	cout<<n<<'#'<<endl;
	

	cout<<"========填充控制=================="<<endl;
	cout.width(10);
	cout.fill('?');
	cout<<n<<'#'<<endl;

	cout.width(10);	//填充控制会影响下一个输出
	cout<<n<<'#'<<endl;

	cout.width(10);
	cout.fill(' ');
	cout<<n<<'#'<<endl;//还原原来的填充控制方式


	cout<<"========精度控制=================="<<endl;

	cout.precision(4); //设置有效数字
	cout<<d<<endl;
	cout.precision(3);
	cout<<d1<<endl;

	  cout.setf(ios::fixed);//设置小数点后的有效数字
	  cout.precision(4); //
	  cout<<d<<endl;
	  cout.precision(3);
	  cout<<d1<<endl;;

	  cout<<"========进制输出=================="<<endl;
	  cout<<n<<endl;
	  cout.unsetf(ios::dec);
	  cout.setf(ios::oct);
	  cout<<n<<endl;
	   cout.unsetf(ios::oct);
	  cout.setf(ios::hex);
	  cout<<n<<endl;


	  cout.setf(ios::showbase);
	  cout<<n<<endl;
	  cout.unsetf(ios::hex);
	  cout.setf(ios::oct);
	  cout<<n<<endl;
	   cout.unsetf(ios::oct);
	  cout.setf(ios::hex);
	  cout<<n<<endl;

	  cout.unsetf(ios::showbase);
	  cout<<n<<endl;
	  return 0;
}



补充下知识:ios类中的枚举常量




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值