打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C 流格式控制的输入/输出
 

 

1.用ios类成员函数进行格式化

/*状态标志    ios类中定义的枚举:*/
enum {
skipws    =0x0001,    //跳过输入中的空白,可用于input
left    =0x0002,      //左对齐输出,可用于output
right    =0x0004,     //右对齐输出,可用于output
internal =0x0008,   //在符号位和基指示符后填入字符,可用于output
dec    =0x0010,       //转换基制为十进制数,可用于output/input
oct    =0x0020,       //转换基制为八进制数,可用于output/input
hex    =0x0040,       //转换基制为十六进制,可用于output/input
showbase =0x0080,   //在输出时显示基指示符,可用于output/input
showpoint =0x0100,  //在输出是显示小数点,可用于output
uppercase =0x0200,  //十六进制输出时,表示制式的和表示数值的字符一律为大写,可用于output
showpos    =0x0400,   //对正整数显示符号,可用于output
scientific =0x0800, //用科学表示法显示浮点数,可用于output
fixed    =0x1000,     //用定点形式显示浮点数,可用于output
unitbuf    =0x2000,   //在输出操作后立即刷新所有流,可用于output
stdio    =0x4000,     //在输出操作后刷新stdout和stderr,可用于output
};

/*状态标志是一个long类型的长整数,这些枚举值可以作为x——flags上的每一位的标志,
若定义了某一项,则x——flags中的某一位为1,否则为0。
*/


/*用成员函数对状态标志进行操作*/
#include<iostream.h>
void showflags(long f);
main()
{
    long f;
    f=cout.flags();       //去当前状态标志
    showflags(f);         //显示状态字
    cout.setf(ios::showpos |ios::scientific |ios::fixed);  //追加状态字
    f=cout.flags();       //取当前状态标志
    showflags(f);         //显示状态字
    cout.unsetf(ios::scientific);  //从状态标志中去掉scientific
    f=cout.flags();
    showflags(f);
    f=cout.flags(ios::hex);  //重新设置状态字
    showflags(f);
    f=cout.flags(f);     //取当前状态
    showflags(f);
    return 1;
}
void showflags(long f)
{
    long i;
    for(i=0x8000;i;i=i>>1)     //用右移方式使i中的1不断右移
       if(i&f) cout<<"1";      //判断f中的某一位是否为1
       else cout<<"0";
    cout<<"\n";
}


/*通过设置域宽,填充字符,设置精度来控制输入/输出格式*/
#include<iostream.h>
main()
{
cout<<"x_width="<<cout.width()<<"\n";
cout<<"x_fill="<<cout.fill()<<"\n";
cout<<"x_precision="<<cout.precision()<<"\n";
cout<<123<<" "<<123.78985<<"\n";
cout.precision(4);       //设置精度
      cout.width(10);       //设置域宽
cout<<"x_width="<<cout.width()<<'\n';
cout<<"x_precision="<<cout.precision()<<'n';
cout<<123<<" " <<123.78985<<" "<<342.787<<'\n';
cout<<"x_width="<<cout.width()<<'\n';
cout.fill('#');      //设置填充字符(注意,这里的#只可以用单引号且必须用)
cout.width(10);
cout<<123<<" "<<123.78985<<'\n';
cout<<"x_width="<<cout.width()<<'\n';
cout<<"x_fill="<<cout.fill()<<'\n';
cout<<"x_precision="<<cout.precision()<<'\n';
return 1;
}

2.使用操纵函数进行格式化控制

C++提供了一种被称为操纵符的控制方式,操纵符是以一个流引用作为其参数,并返回
同一流的引用,因此它可以嵌入到输入/输出操作的链中。

C++预定义的标准操纵符:
dec 设置十进制转换基格式标志,可用于输入/输出
hex 设置十六制的转换基格式标志,可用于输入/输出
oct 设置八进制的转换基格式标志,可用于输入/输出
ws 提取空白字符,仅用于输入
endl 插入换行符,并刷新流,仅用于输出
ends 在串后插入终止空字符,仅用于输出
flush 刷新一个输出流,仅用于输出操作
setbase(int n)     置转换基格式为n(n范围为0,8,10,或16),缺省为0,表示采用十进制.仅输出
resetiosflags(long f)     清除由参数f指定的格式,用于输入输出
setiosflags(long f)     用参数f指定格式位,可用于输入输出.
setfill(int c)     用c置填充字符,可用于输入输出
setprecision(int n)      设置浮点数精度为n,可用于输入输出
setw(int n)     置域宽为n,可用于输入输出操作

其中操作符是在头文件isotream.h中定义的,而操作函数是在iomanip.h中定义的

/*操纵符和操纵函数的使用
#include<iostream.h>
#include<iomanip.h>
main()
{
cout<<123<<setiosflags(ios::scientific)<<setw(20)
     <<3456.45<<endl;
cout<<setw(10)<<123<<678<<endl;
cout<<123<<setw(10)<<hex<<123<<setw(10)<<setbase(8)       //setbase()无法识别~~
     <<34894<<endl;
cout<<resetiosflags(ios::scientific)<<setprecision(4)
     <<123.893242<<endl;
return 1;
}

/*用户自定义操纵符
若为输出流定义操纵符函数,则定义下面的形式:
     ostream&manip_name(ostream&stream)
     {
        //your code here;
        return stream;
     }
若为输入流:
     istream&manip_name(istream&stream)
     {
        //your code here;
        return stream;
     }
*/

//example:
#include<iostream.h>
#include<iomanip.h>

ostream & out1(ostream & output)
{
output<<setw(16)<<hex<<setiosflags(ios::scientific)<<setfill('*');
//设定操纵符为域宽16,整型用十六进制,浮点型用科学表示法,当域不满时用*填充
return output;
}
istream & in1(istream & input)
{
input>>hex;     //设定操纵符为以十六进制输入
cout<<"Enter number using hex format:\n";
return input;
}
main()
{
int i;
float x=3457.734;
cin>>in1>>i;
cout<<out1<<i<<out1<<x<<endl;
return 1;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
cout 格式化输出
C++的cin/cout高阶格式化操作
C++之预定义类型IO格式控制
iostream_setiosflags.cpp
结构体和链表的简单应用
C++输入cout与输出cin
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服