2016.4.15上机内容摘要.ppt

2016.4.15 2.2.3 简单的i/o格式控制 C++I/o流提供了一些操纵符,可以直接嵌入到输入输出语句中来实现输入输出格式控制。 manip是manipulator(操纵器)的缩写 manipulate 操作 2.2.3 简单的i/o格式控制 使用操纵算子,则可以在插入和提取运算符的表达式中控制格式化输入和输出。 在程序中使用操纵算字必须嵌入头文件iomanip (书上写错) #includeiomanip.h Withdraw space 一般,预定义的控制符有以下几种: dec 十进制的输入输出? hex 十六进制的输入输出? oct 八进制的输入输出? ws 提取空白字符? ends 输出一个空字符? endl 输出一个换行字符,同时刷新流? setprecision(int) 设置输出浮点数的精度数 setw(int) 设置域宽? binary 二进制的 octal 八进制的 hexadecimal 十六进制的 decimal 十进制的 插入运算符与预先定义的操纵符一起工作,用来控制输出格式。 dec、oct和hex操纵符设置输入和输出的缺省进制,例如,若将hex操纵符插入到输出流中,则以十六进制格式输出。 #include iostream using namespace std; void main() { couthex128endl; coutoct128endl; coutdec128endl; } #include iostream using namespace std; //#includeiomanip.h void main() { couthex128endl; coutoct128endl; coutdec128endl; } #includeiostream using namespace std; void main() { int x,y,z; cinhexxoctydecz; coutx=xendl; couty=yendl; coutz=zendl; } /* #include iostream using namespace std; #include bitset void main( ) { int a=128 ; cout aendl; cout bitset8(a) endl; } #include iostream using namespace std; #include bitset void main( ) { int a=128 ; cout aendl; cout bitset16(a) endl; } #include iostream using namespace std; #include bitset void main( ) { short int a=32769 ; cout aendl; cout bitset16(a) endl; } */ #include iostream #include iomanip using namespace std; void main() { int i; for( i=1;i=9;i++) coutsetw(3)i; coutendl; } 例如:要输出浮点数3.1415并换行,设置域宽为5个字符,小数点后保留两位有效数字,输出语句如下: #includeiostream using namespace std; #includeiomanip void main() { coutsetw(5)setprecision(3) 3.1415endl; coutsetw(10)setprecision(5) 3.1415926endl; coutsetw(10)setprecision(5) 314.15926endl; } 实际上指的是几位有效数字 #includeiostream using namespace std; #includeiomanip void main() { coutsetiosflags(ios_base::fixed); coutsetw(10)setprecision(3) 3.1415endl; coutsetw(10)setprecision(5) 3.1

文档评论(0)

1亿VIP精品文档

相关文档