C++中使用cout进行格式化

(1)修改显示时所用的计数系统

1. 概述:使用dec等控制符

dec //置基数为10,相当于"%d"
hex //置基数为16,相当于"%X"
oct //置基数为8,相当于"%o" 

2. 性质:作用永久贯穿整个程序,直到格式状态设置为其他函数

3. 例子

#include <iostream>
using namespace std;
int main()
{
	cout << 12 << endl;//默认十进制
	cout << dec << 12 << endl;//十进制
	cout << hex << 12 << endl;//十六进制
	cout << oct << 12 << endl;//八进制
	hex(cout);
	cout << 12 << endl;//这种方式同样可行,但不常用
	//cout << bin << 12 << endl; 是错误表达,cout不能输出二进制
	//输出为:12 <\n> 12 <\n> c <\n> 14 <\n> c <\n>
}

(2)调整字段宽度

①方法一:width成员函数

1. 概述

//函数原型
int width();//返回当前字段宽度设置的值
int width( int n);//返回当前字段宽度设置的值,并将字段宽度设置为n
//使用
int w = cout.width(12);//不要忘了这是cout的成员函数
cout.width(12);//也可以不用整型数借接住

2. 性质

*默认的字段宽度为0

*输出默认为右对齐,空格被插入到输出的左侧

*width()只会影响下一个输出,不会持久作用

*编译器不会截短数据,在字符长超过宽度时宽度会扩展

3. 例子

#include <iostream>
int main()
{
    using std::cout;
    int w = cout.width(30);
    cout << "default field width = " << w << ":\n";
    //输出为        default field width = 0:
    cout.width(5);
    cout << "N" << ':';//输出为    N:
    cout.width(8);
    cout << "N * N" << ":\n";//输出为   N * N:
    //总的输出为    N:   N * N:
    return 0;
}

②方法二:setw格式控制符

1. 概述

//使用格式
cout << setw(n) << "blabla" << endl;

2. 性质

*输出默认为右对齐,空格被插入到输出的左侧

*width()只会影响下一个输出,不会持久作用

*编译器不会截短数据,在字符长超过宽度时宽度会扩展

*需要包含头文件#include <iomanip> 和名称空间std

3. 例子

#include <iostream>
#include <iomanip> //别忘了这个
using namespace std;
int main()
{
    cout << setw(4) << "runoob" << endl;
    //输出为runoob,后面的 runoob 字符长度大于 4,不起作用
    cout << setw(14) << "runoob" << endl;
    //输出为        runoob
    cout << "runoob" << setw(14) << "runoob" << endl;
    //输出为runoob        runoob
    return 0;
}

(3)填充字符

方法:fill成员函数

1. 概述

cout.fill(c);//设填充字符为c

2. 性质

*作用贯穿整个程序,直到下一次设置

*实质上就是将调整字段宽度的填充从空格变成了填充字符

3. 例子

#include <iostream>
using namespace std;
int main()
{
	cout.fill('@');
	cout.width(9);
	cout  << 1 << '$' << endl;
	//输出为@@@@@@@@1$,用cout.fill('@');里面的字符补全cout.width(9)产生的空格
	cout.width(6);
	cout << 987 << '$' << endl;
	//输出为@@@987$原来的cout.fill('@');依然起作用
	cout.width(3);
	cout <<  100 << '$' << endl;
	//输出为100$,当cout.width(9)不起作用时,cout.fill('@');的作用自然不会显现
	return 0;
}

方法:setfill控制符

1. 概述

cout << setfill(c);//设填充字符为c

2. 性质

*基本同方法一

*但需要包含头文件#include <iomanip> 和名称空间std

3. 例子

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	cout << setfill('@') << setw(9) << 1 << '$' << endl;
	cout << setw(9) << setfill('@') << 1 << '$' << endl;
	//输出为@@@@@@@@1$,用setfill里面的字符补全setw产生的空格,且二者可以颠倒
	cout << setw(6) << 987 << '$' << endl;
	//输出为@@@987$原来的setfill依然起作用
	cout << setw(3) << 100 << '$' << endl;
	//输出为100$,当setw(3)不起作用时,setfill的作用自然不会显现
	cout << setw(9) << setfill('#') << 1 << '$' << endl;
	//输出为########1$,填充字符可更改
	return 0;
}

(4)设置浮点数显示精度

①方法一:precision成员函数

1.概述

C++打印浮点数默认的时保留六位有效数字,但省略0,通过如下操作可以改变浮点数的精度(有效数字),但依然会省略0

 cout.precision(n); //输出精度为n

2.性质

作用贯穿整个程序,直到下一次设置

3.例子

#include <iostream>
int main()
{
    using std::cout;
    float price1 = 20.40;
    float price2 = 1.9 + 8.0 / 9.0;
    cout << price1 << "\n";
    cout << price2 << "\n";
    //输出为20.4 <\n> 2.78889
    cout.precision(2);
    cout << price1 << "\n";
    cout << price2 << "\n";
    // 输出为20 <\n> 2.8
    return 0;
}

方法:格式控制符setprecision

1. 概述

用法和性质和precision成员函数几乎一样,但格式不一样,如下

cout << std::setprecision(2) << blabla;

2. 性质

*作用贯穿整个程序,直到下一次设置

*需要包含头文件#include <iomanip> 和名称空间std

3. 例子

#include <iostream>
#include <iomanip>
int main()
{
    using std::cout;
    float price1 = 20.40;
    float price2 = 1.9 + 8.0 / 9.0;
    cout << price1 << "\n";
    cout << price2 << "\n";
    //输出为20.4 <\n> 2.78889
    cout << std::setprecision(2) << price1 << "\n";
    cout << std::setprecision(2) << price2 << "\n";
    // 输出为20 <\n> 2.8
    return 0;
}

(5)设置浮点数小数点后的精度

1. 概述

如下语句会导致接下来的输出中数据是定点计数法,精度指的是小数点后面的位数,而不是总位数

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

 一般会配合精度控制使用

cout.setf(ios_base::fixed, ios_base::floatfield);
cout << setprecision(3) << 2.34598 << endl;
//输出为2.346,保留小数点后三位,四舍五入

2. 性质

该语句的逻辑在于,ios_base提供了setf()函数,而fixed和floatfieldios_base中定义的静态常量

3. 例子(详见(6)中的第二个例子)

(6)打印末尾的0和小数点

1. 概述

如下语句会导致输出中数据的小数点显现,但后在数据右边补零直至数据的位数为C++莫仍的六位

cout.setf(ios_base::showpoint);

 一般会配合精度控制使用,显示小数点且补零到合适精度

2. 性质

该语句的逻辑在于,ios_base提供了setf()函数,而showpoint为ios_base中定义的静态常量

3. 例子

#include <iostream>
int main()
{
    using std::cout;
    using std::ios_base;//注意这个
    float price1 = 20.40;
    float price2 = 1.9 + 8.0 / 9.0;
    cout.setf(ios_base::showpoint);
    cout << price1 << "\n";
    cout << price2 << "\n";
    //输出为20.4000 <\n> 2.78889
    cout.precision(2);
    cout << price1 << "\n";
    cout << price2 << "\n";
    //输出为20. <\n> 2.8
    return 0;
}

4. 例子(对(4)(5)(6)的一个总结)

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	cout << setprecision(3) << 2.34598 << endl;
	//输出为2.35,保留三位有效数字,四舍五入
	cout.setf(ios_base::fixed, ios_base::floatfield);
	//使用定点计数法,精度指的是小数点后面的位数, 而不是总位数
	cout << setprecision(3) << 2.34598 << endl;
	//输出为2.346,保留小数点后三位,四舍五入
	cout.setf(ios_base::showpoint);
	//显示小数点后面的0 
	cout << setprecision(9) << 2.34598 << endl;
	//输出为2.345980000,补零道小数点后九位
	//注意如果没有使用定点计数法,则这个语句输出的应该是补零到有九位有效位
	return 0;
}

(7)再谈setf()

①关于fmtflags

1. 其是bitmask类型的typedef名,储存格式标记

2. 由于其在ios_based中定义,故使用时必须加上ios_based::

3. 一些fmtflags格式常量

ios_base::boolalpha;//输出为bool值
ios_base::showbase;//输出前缀加上进制的前缀(0x,0)
ios_base::showpos;//强行显示正负号

②setf()的两种原型

fmtflags setf(fmtflags);
//指出要设置哪一位,返回指出以前所有标记的设置
fmtflags setf(fmtflags,fmtflags)
//第一个参数一样,第二个参数指出要清除第一个参数中的哪些位,返回也一样

③调用方式

//eg
 cout.setf(ios_base::left, ios_base::adjustfield);
 cout.setf(ios_base::showpos);

④例子

#include <iostream>
#include <cmath>
int main()
{
    using namespace std;
    cout.setf(ios_base::left, ios_base::adjustfield);//使用左对齐
    cout.setf(ios_base::showpos); //显示加号,显示尾随
    cout.setf(ios_base::showpoint);//显示小数点
    cout.precision(3);//精度为3 
    ios_base::fmtflags old = cout.setf(ios_base::scientific,ios_base::floatfield);//使用 e-notation并保存旧格式设置
    cout << "Left Justification:\n";
    long n;
    for (n = 1; n <= 41; n += 10)
    {
        cout.width(4);
        cout << n << "|";
        cout.width(12);
        cout << sqrt(double(n)) << "|\n";
    }   
    return 0;
}
    /*输出
Left Justification:
+1  |+1.000e+00  |
+11 |+3.317e+00  |
+21 |+4.583e+00  |
+31 |+5.568e+00  |
+41 |+6.403e+00  |
*/

⑤unset()

原型为void setf(fmtflags mask)其作用为解除setf()的效果

(8)标准控制符

前面已经给出了很多例子了,其原理在于调用相应的setf()

例如:

//hex调用的就是setf(ios_base::hex, ios_base::basefield);
#include<iostream>
using namespace std;
int main()
{
	cout.setf(ios_base::hex, ios_base::basefield);
	cout << 12;//输出为c
	return 0;
}

再用控制符

#include<iostream>
using namespace std;
int main()
{
	cout <<hex<< 12;//输出也为c
	return 0;
}

(9)头文件iomanip

包含setw(), setprecision(), setfill(), 前面已经讲过了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值