首页 > 编程语言 > 详细

C++格式化输出

时间:2019-03-31 16:11:37      阅读:140      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <iomanip>
#include <cmath> 

using namespace std;

int main()
{
    
//    boolalpha 可以让 bool 值按字符串输出 
    cout<<"Before operating: "<<true<<boolalpha<<endl;
cout<<"After operating: "<<true<<noboolalpha<<endl; // showbase 显示进制,uppercase 在十六进制打印 0X、在科学记数法打印 E // oct 打印八进制,hex 打印十六进制,dec 打印十进制 ,也可以用 setbase(base) 来设置进制 int num = 0; cout<<"Enter the num: "; cin>>num; cout<<showbase<<uppercase<<"Default: "<<num<<endl; cout<<"Octal: "<<oct<<num<<endl; cout<<"Hex: "<<hex<<num<<endl; cout<<"Decimal: "<<dec<<num<<noshowbase<<nouppercase<<endl; // 我们可以使用 cout.precision(size) 或 setprecision(size) 来设置精度
// 即几位数字,通过 cout.precision() 返回当前精度值
cout<<"Precision: "<<cout.precision()<<", Value: "<<sqrt(2.0)<<endl; cout.precision(12); cout<<"Precision: "<<cout.precision()<<", Value: "<<sqrt(2.0)<<endl; cout<<setprecision(3); cout<<"Precision: "<<cout.precision()<<", Value: "<<sqrt(2.0)<<endl; // showpoint 对浮点值总是显示小数点,showpos 对非负数显示 + cout<<"Before operating the num is: "<<2.0<<endl;
cout<<showpoint<<showpos<<"After operating: "<<2.0<<noshowpoint<<noshowpos<<endl; // sciencetific 用科学计数法显示浮点数,acos 为反三角函数,在头文件 cmath 里 cout<<scientific<<"Scientific: "<<acos(-1)<<endl; // setw(width) 设置宽度为 width 个字符,right 为右对齐,left 为左对齐,setfill(ch) 用 ch 来填充 cout<<setw(20)<<setfill(*)<<left<<"I LOVE U"<<endl<<setw(20)<<right<<"I LOVE U TOO";
cout<<endl; // unitbuf 每次输出操作都刷新缓冲区,对应nounitbuf;skipws 输入运算符跳过空白符,对应 noskip cout<<unitbuf; cin>>noskipws; cout<<"Enter the chars or enter the ‘!‘ to end it: "<<endl; char ch; while (cin>>ch && ch != !) cout<<ch; cout<<nounitbuf; cin>>skipws; system("pause"); return 0; }

 

C++格式化输出

原文:https://www.cnblogs.com/lemonyam/p/10631494.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!