streamflags.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 39 行

CPP
39
字号
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    const double PI = acos(-1);  // arccos(-1) = PI radians
	const int MAX = 10;          // max precision used in demo
	int k;
	// set right justified, fixed floating format

    cout.setf(ios_base::right, ios_base::adjustfield);
	cout.setf(ios_base::fixed, ios_base::floatfield);

	cout << "fixed, right justified, width 10, precision varies\n" << endl;

	for(k=0; k < MAX; k++)
	{   cout.precision(k);
	    cout << k << "\t+";
		cout.width(MAX);
		cout << PI << "+" << endl;
	}
	 
    // use different fill characters

	int fillc = cout.fill();
	cout.precision(2);
	cout << "\nshow fill char, precision is 2\n" << endl;
	for(k='a'; k <= 'd'; k++)
	{   cout << "old fill = '" << char(fillc) << "' +";
        cout.width(MAX);
		cout.fill(k);
		cout << PI << "+" << endl;
	    fillc = cout.fill();
	}

	return 0;
}

⌨️ 快捷键说明

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