c++输出格式设置
来源:互联网 发布:国内最美的海滩知乎 编辑:程序博客网 时间:2024/06/09 17:51
可以不使用iomanip的,
cout.precision()设置小数点后精确度,
cout.width()设置宽度,
cout.setf()设置显示格式,比如
cout.setf(ios::left)左对齐
cout.setf(ios::showpoint)不管是否有小数位,显示小数点
cout.fill();不足宽度则填充,如cout.fill('0');
如这次周赛1002,如果使用COUT在输出前要这样设置一下。
cout.precision(6);
cout.width(8);
cout.setf(ios::left);
cout.setf(ios::showpoint);
cout.fill('0');
仅仅cout.precision(6)和cout.setf(ios::showpoint)时,不知何原因如果为0只显示到小
数点后5位,所以为了在最后加个0,要加上其它3项
补充(部分是iomanip里的):
long flags( ) const 返回当前的格式标志。
long flays(long newflag) 设置格式标志为newflag,返回旧的格式标志。
long setf(long bits) 设置指定的格式标志位,返回旧的格式标志。
long setf(long bits,long field)将field指定的格式标志位置为bits,返回旧的格式标
志
long unsetf(long bits) 清除bits指定的格式标志位,返回旧的格式标志。
long fill(char c) 设置填充字符,缺省条件下是空格。
char fill( ) 返回当前填充字符。
int precision(int val) 设置精确度为val,控制输出浮点数的有效位,返回旧值。
int precision( ) 返回旧的精确度值。
int width(int val) 设置显示数据的宽度(域宽),返回旧的域宽。
int width( )只返回当前域宽,缺省宽度为0。这时插入操作能按表示数据的最小宽度显示
数据
dec 十进制的输入输出
hex 十六进制的输入输出
oct 八进制的输入输出
ws 提取空白字符
flush 刷新流
resetiosflags(long) 请除特定的格式标志位
setiosflags(long) 设置特定的格式标志位
setfill(char) 设置填充字符
setprecision(int) 设置输出浮点数的精确度
setw(int) 设置域宽格式变量
──── winsweet (Sun Mar 23 11:08:19 2008) ────────────
我喜欢用C的输出函数~~有没有那方面的介绍~~~
【 在 gcc (感冒通) 的大作中提到: 】
: 可以不使用iomanip的,
: cout.precision()设置小数点后精确度,
: cout.width()设置宽度,
: cout.setf()设置显示格式,比如
: cout.setf(ios::left)左对齐
: cout.setf(ios::showpoint)不管是否有小数位,显示小数点
: cout.fill();不足宽度则填充,如cout.fill('0');
:
: 如这次周赛1002,如果使用COUT在输出前要这样设置一下。
:
: .................(以下省略)
──── gcc (Sun Mar 23 11:21:51 2008) ──────────────
include<cstdio>
int main()
{
//for int
int i=30122121;
long i2=309095024l;
short i3=30;
unsigned i4=2123453;
printf("%d,%o,%x,%X,%ld,%hd,%u\n",i,i,i,i,i2,i3,i4);//如果是:%l,%h,则输不
出结果
printf("%d,%ld\n",i,i2);//试验不出%ld和%d之间的差别,因为long是4bytes
printf("%hd,%hd\n\n\n",i,i3);//试验了%hd和%d之间的差别,因为short是2bytes
//for string and char
char ch1='d';
unsigned char ch2=160;
char *str="Hello everyone!";
printf("%c,%u,%s\n\n\n",ch1,ch2,str);//unsigned char超过128的没有字符对应
//for float and double,unsigned and signed can not be used with double and
float
float fl=2.566545445F;//or 2.566545445f
double dl=265.5651445;
long double dl2=2.5654441454;
//%g没有e格式,默认6位包括小数点前面的数,
//%f没有e格式,默认6位仅只小数点后面包含6位
//%e采用e格式,默认6位为转化后的小数点后面的6位
printf("%f,%e,%g,%.7f\n",fl,dl,dl,dl);
printf("%f,%E,%G,%f\n",fl,dl,dl,dl);//%F is wrong
printf("%.8f,%.10e\n",fl,dl);
printf("%.8e,%.10f\n\n\n",fl,dl);
//for point
int *iP=&i;
char *iP1=new char;
void *iP2;//dangerous!
printf("%p,%p,%p\n\n\n",iP,iP1,iP2);
//其他知识:负号,表示左对齐(默认是右对齐);%6.3,6表示宽度,3表示精度
char *s="Hello world!";
printf(":%s: \n:%10s: \n:%.10s: \n:%-10s: \n:%.15s: \n:%-15s: \n:%15.10s:
\n:%-15.10s:\n\n\n",
s,s,s,s,s,s,s,s);
double ddd=563.908556444;
printf(":%g: \n:%10g: \n:%.10g: \n:%-10g: \n:%.15g: \n:%-15g: \n:%15.10g:
\n:%-15.10g:\n\n\n",
ddd,ddd,ddd,ddd,ddd,ddd,ddd,ddd);
//还有一个特殊的格式%*.* ,这两个星号的值分别由第二个和第三个参数的值指定
printf("%.*s \n", 8, "abcdefgggggg");
printf("%*.*f \n", 3,3, 1.25456f);
return 0;
}
还有研究GCC中格式化输出函数的http://bbs.chinaunix.net/archiver/?tid-24825.html
【 在 winsweet (虫不睡着尿尿) 的大作中提到: 】
: 我喜欢用C的输出函数~~有没有那方面的介绍~~~
──── winsweet (Sun Mar 23 13:22:40 2008) ────────────
THX
【 在 gcc (感冒通) 的大作中提到: 】
: include<cstdio>
: int main()
: {
: //for int
: int i=30122121;
: long i2=309095024l;
: short i3=30;
: unsigned i4=2123453;
: printf("%d,%o,%x,%X,%ld,%hd,%u\n",i,i,i,i,i2,i3,i4);//如果是:%l,%h,则输不
: 出结果
: .................(以下省略)
──── RovingCloud (Sun Mar 23 20:32:37 2008) ──────────
赞
【 在 gcc (感冒通) 的大作中提到: 】
: 可以不使用iomanip的,
: cout.precision()设置小数点后精确度,
: cout.width()设置宽度,
: cout.setf()设置显示格式,比如
: cout.setf(ios::left)左对齐
: cout.setf(ios::showpoint)不管是否有小数位,显示小数点
: cout.fill();不足宽度则填充,如cout.fill('0');
:
: 如这次周赛1002,如果使用COUT在输出前要这样设置一下。
:
: .................(以下省略)
──── galaxy (Sun Mar 23 20:43:05 2008) ─────────────
不错~~~
【 在 gcc (感冒通) 的大作中提到: 】
: 可以不使用iomanip的,
: cout.precision()设置小数点后精确度,
: cout.width()设置宽度,
: cout.setf()设置显示格式,比如
: cout.setf(ios::left)左对齐
: cout.setf(ios::showpoint)不管是否有小数位,显示小数点
: cout.fill();不足宽度则填充,如cout.fill('0');
:
: 如这次周赛1002,如果使用COUT在输出前要这样设置一下。
:
: .................(以下省略)
──── bigheadghost (Sun Mar 23 22:47:51 2008) ──────────
赞!
"%*.*"这个好阿
【 在 gcc (感冒通) 的大作中提到: 】
: include<cstdio>
: int main()
: {
: //for int
: int i=30122121;
: long i2=309095024l;
: short i3=30;
: unsigned i4=2123453;
: printf("%d,%o,%x,%X,%ld,%hd,%u\n",i,i,i,i,i2,i3,i4);//如果是:%l,%h,则输不
- Smartform输出格式设置
- Endnote输出格式设置
- smartform输出格式设置
- smartform输出格式设置
- c++输出格式设置
- printf 输出格式设置
- C语言格式输出
- C输出格式总结
- c语言 输出格式
- C格式输出
- C输出格式总结
- C语言输出格式
- C输出格式总结
- C输出格式总结
- C输出格式控制
- C输出格式控制
- c printf 输出格式
- C printf 格式输出
- C#关于多线程原理
- win7 SqlServer2005 Management Studio Express 安装错误提示:29506
- SkinMagic 进行皮肤设置 (SkinMagic使用教程)
- PAT (Basic Level) Practise (中文)—— 1009. 说反话
- 用相对布局实现登陆界面设计
- c++输出格式设置
- Android页面切换方法和区别
- Google发布GAE 1.8.1版
- Mac下删除和安装cocos2d-x
- GZIP压缩
- 【成功解决】ubuntu 12.04 解压 zip 7z rar jar 文件,文件名中文乱码的问题
- SkinMagic使用后按钮加自定义图标或菜单GetMneu返回NULL的解决方法
- 错误笔记!【二】ssh整合问题
- [成功解决]ubuntu12.04 gedit 打开 windows 分区中的 txt 文件乱码,是因为 ubuntu 和 windows 两个系统的编码不同。