好文档 - 专业文书写作范文服务资料分享网站

信息学奥赛培训教程C++版

天下 分享 时间: 加入收藏 我要投稿 点赞

15 cout.unsetf( ios::scientific );

16 cout << \17 << x << ,\\t' << y << ,\\n,;

18 cout.setf( ios::fixed, ios::floatfield ); 19 cout << \20 << x << '\\t' << y << endl; 21 return 0; 22 }

输出结果:

Displayed in default format: 0.00123457 1.946e+009 Displayed in scientific format:

1.234567e-003 1.946000e+009 Displayed in default format after unsetf: 0.00123457 1.946e+009 Displayed in fixed format:

0.001235 1946000000.000000

图 3.26 以系统默认格式、科学计数法和定点格式显示浮点数

3.7.7 大/小写控制(ios::upercase)

设置ios::uppercase标志用来使大写的X和E分别同十六进制整数和以科学记数法表示的浮点数一起输出(见图3.27)一旦设置ios::uppercase标志,十六进制数中的所有字母都转换成大写。 1 // Fig. 3.27: fig11_27.cpp

2 // Using the ios::uppercase flag 3 #include 4 #include 5

6 int main() 7 {

8 cout << setiosflags( ios::uppercase )

9 << \

10 << \11 << 4.345e10 << '\\n' << hex << 123456789 << endl; 12 return O; 13 }

输出结果:

Printing uppercase letters in scientific

notation exponents and hexadecimal values: 4.395E+010

-56-

75BCD16

图 3.27 使用ios::uppercase标志

3.7.8 设置及清除格式标志(flags、setiosflags、resetosflags)

无参数的成员函数flags只返回格式标志的当前设置(long类型的值)。带一个long类型参数的成员函数flags按参数指定的格式设置标志,返回以前的标志设置。flags的参数中未指定的任何格式都会被清除。图3.18的程序用成员函数flags设置新的格式状态和保存以前的格式状态,然后恢复原来的格式设置。 1 // Fig. 3.28: fig11_28.cpp

2 // Demonstrating the flags member function. 3 #include 4

5 int main() 6 {

7 int i = 1000;

8 double d = 0.0947625; 9

10 cout << \11 << cout.flags()

12 << \13 << i << '\\t' << d << \14 long originalFormat =

15 cout.flags( ios::oct | ios::scientific ); 16 cout << \17 << cout.flags()

18 << \19 << \20 << i << '\\t' << d << \21 cout.flags( originalFormat );

22 cout << \23 << cout.flags()

24 << \25 << i << '\\t' << d << endl; 26 return 0; 27 }

输出结果:

The value of the flags variable is: 0 Print int and double in original format: 1000 0.0947628

-57-

The value of the flags variable is: 4040 Print iht and double in a new format

Specified using the flags member function: 1750 9.476280e-002

The value of the flags variable is: 0 Print values in original format again: 1000 0.0947628

图 3.28 演示成员函数flags

成员函数serf设置参数所指定的格式标志,并返回long类型的标志设置值,例如: long previousFlagSettings=

cout.setf(ios::showpoint | ios::showpos);

带两个long型参数的setf成员函数.例如: cout.setf(ios::left,ios::adjustfield); 首先清除ios::adjustfield位,然后设置ios::left标志。该版本的setf用于与ios::basefield(用ios::dec、 ios::oct和ios::hex表示)、ios::floatfield(用ios::scientific和ios::fixed表示)和ios::adjustfiald(用ios:: left、ios::right和ios::internal表示)相关的位段。

成员函数unsetf清除指定的标志并返回清除前的标志值。

3.8 流错误状态

可以用类ios中的位测试流的状态。类ios是输入/输出类istream、ostream和iostream的基类。

当遇到文件结束符时,输人流中自动设置eofbit。可以在程序中使用成员函数eof确定是否已经到达文件尾。如果cin遇到了文件结束符,那么函数调用: cin.eof()

返回true,否则返回false。

当流中发生格式错误时,虽然会设置failbit,但是字符并末丢失。成员函数fail判断流操作是否失败,这种错误通常可修复。

当发生导致数据丢失的错误时,设置badbit。成员函数bad判断流操作是否失败,这种严重错误通常不可修复。

如果eofbit、failbit或badbit都没有设置,则设置goodbit。

如果函数bad、fail和eof全都返回false,则成员函数good返回true。程序中应该只对“好”的流进行I/O操作。

成员函数rdstate返回流的错误状态。例如.函数调用cout.rdstate将返回流的状态,随后可以用一条switch语句测试该状态,测试工作包括检查ios::eofbit、ios::badbit、ios::failbit和ios::goodbit。

测试流状态的较好方法是使用成员函数eof、bad、fail和good,使用这些函数不需要程序员熟知特定的状态位。

成员函数clear通常用于把—个流的状态恢复为“好”,从而可以对该流继续执行I/O操作。由于clear的默认参数为ios::goodbit,所以下列语句:

-58-

cin.clear();

清除cin,并为流设置goodbit。下列语句: cin.clear(ios::failbit)

实际上给流设置了failbit。在用自定义类型对cin执行输入操作或遇到问题时,用户可能需要这么做。clear这个名字用在这里似乎并不合适,但规定就是如此。

图3.29中的程序演示了成员函数rdstate、eof、fail、bad、good和clear的使用。

只要badbit和failbit中有一个被置位,成员函数operator!就返回true。只要badbit和failbit中有一个被置位,成员函数operator void*就返回false。这些函数可用于文件处理过程中测试选择结构或循环结构条件的true/false情况。

1 // Fig. 3.29: fig11_29.cpp 2 // Testing error states. 3 #include 4

5 int main() 6 {

7 int x;

8 cout << \9 << \10 << \ cin.eof(): \11 << \ cin.fail(): \12 << \ cin.bad(): \13 << \ cin.good(): \

14 << \15 cin >> x; 16

17 cout << \18 << \19 << \ cin.eof(): \20 << \ cin.fail(): \21 << \ cin.bad(): \

22 << \ cin.geed() \23

24 cin.clear(); 25

26 cout << \

27 << \

28 << \29 return 0; 30 }

输出结果:

Before a bad input operation: cin.rdstate(): 0

-59-

cin.eof(): 0 cin.fail() 0 tin.bad(): 0 cin.good(): 0

Expects an integer, but enter a character: A

After a bad input operation: cin.eof(): 0 cin.fail(): 2 cin.bad(): 0 cin.good(): 0

After cin.clear() cin. fail():0 cin.good(): 1

3.29 测试错误状态-60-

7147t1zw3j3ef8l93ts6
领取福利

微信扫码领取福利

微信扫码分享