C++中控制符方式控制输出

注意点及解释在程序中都有体现,主函数中/**/注释掉的的代码模块都分别对应一部分,每个小模块都可以实现。

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值