demo_2_io_format_1_b.cpp

来自「对于一个初涉VC++的人来书」· C++ 代码 · 共 44 行

CPP
44
字号

//***************************************************
// 用流对象的成员函数进行格式化输出
//***************************************************

# include <iostream.h>

int main()
{
	double a=123,b=123.456;

	cout<<a<<endl;
	cout<<b<<endl;

	cout.setf(ios::showpos|ios::scientific);
	cout<<a<<endl;
	cout<<b<<endl;
	cout.unsetf(ios::showpos|ios::scientific);

	cout.fill('*');
	cout.setf(ios::left);
	cout.width(10); //设置后仅对下一个输出数据一次性有效
	cout<<a<<endl;
	cout.width(10);
	cout<<b<<endl;
	cout.unsetf(ios::left);

	cout.precision(5);
	cout<<a<<endl;
	cout<<b<<endl;

	return 0;
}

/*
123
123.456
+1.230000e+002
+1.234560e+002
123*******
123.456***
123
123.46
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?