C++格式化输出

#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; }

猜你喜欢

转载自www.cnblogs.com/lemonyam/p/10631494.html