当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++格式化输出

C++格式化输出

2019年04月01日  | 移动技术网IT编程  | 我要评论

尖叫现场2,优秀共产党员徐新贤,抗美援朝第五次战役

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

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网