ITEEDU

11.6.2 设置浮点数精度(precision、setprecision)

在C++中可以人为控制浮点数的精度,也就是说可以用流操纵算子setprecision或成员函数percision控制小数点后面的位数。设置了精度以后,该精度对之后所有的输出操作都有效,直到下一次设置精度为止。无参数的成员函数percision返回当前设置的精度。图11.17中的程序用成员函数precision和流操纵算子setprecision打印出了2的平方根表,输出结果的精度从0连续变化到9。

 // Fig. 11.17: fig11_17.cpp
 // Controlling precision of floating-point values
 #include < iostream.h>
 #include < iomanip.h>
 #include < math.h>
 int main()
   double root2 = sqrt( 2.0 );
   int places;
   cout << setiosflags(ios::fixed)
       << "Square root of 2 with precisions 0-9.\n"
       << "Precision set by the"
       << "precision member function:" << endl;
   for ( places = 0; places <= 9; places++ ) {
     cout.precision( places );
     cout << root2 << '\n';
     }
   cout << "\nPrecision set by the"
       << "setprecision manipulator:\n";
   for ( places = 0; places <= 9; places++ )
     cout << setprecision( places ) << root2 << '\n';
   return 0;
 }

输出结果:

Square root of 2 with pzecisions 0-9.

Precision set by the precision member function:

1

1.4

1.41

1.414

1.4142

1.41421

1.414214

1.4142136

1.41421356

1.414213562

Precision set by the setprecision manipulator:

1

1.4

1.4l

1.414

1.4142

1.41421 ·

1.414214

1.4142136

1.41421356

1.414213562

图11.17 控制浮点数的精度

11.6.3 设置域宽(setw、width)

成员函数ios.width设置当前的域宽(即输入输出的字符数)并返回以前设置的域宽。如果显示数据所需的宽度比设置的域宽小,空位用填充字符填充。如果显示数据所需的宽度比设置的域宽大,显示数据并不会被截断,系统会输出所有位。

常见编程错误11.4

域宽设置仅对下一行流读取或流插入操作有效,在一次操作完成之后,城宽又被置回0,也就是输出值按照所需要的宽度来输出。不带参数的width函数返回当前域宽。认为域宽设置适用于所有输出走十逻辑错误。

常见编程错误11.5

未对所处理的输出数据提供足够的域宽时,输出数据将按需要的域宽进行输出,有可能产生难以阅读的输出结果。

图1l.18中的程序示范了成员函数width的输入和输出操作。注意,输入操作提取字符串的最大宽度比定义的域宽小1,这是因为在输入的字符串后面必须加上一个空字符。当遇到非前导空白字符时,流读取操作就会终止。流操纵算子setw也可以用来设置域宽。注意:提示用户输入时,用户应输入一行文本并按Enter键加上文件结束符( <ctrl>-z对于IDMPC兼容系统;<ctrl>-d对于UNIX与Macintosh系统)。

 // fig11_18.cpp
 // Demonstrating the width member function
 #include < iostream.h>
 int main()
{
   int w = 4;
   char string[ 10 ];
  cout << "Enter a sentence:\n";
  cin.width( 5 );
  while (cin >> string ) {
     cout.width( w++ );
     cout << string << endl;
     cin.width( 5 );
   }
   return 0;
 }

输出结果:

Enter a sentence:
This is a test of the width member function
This
       is
         a
       test
          of
          the
          widt
              h
            memb
               er
              func
               tion

图 11.18 演示成员函数width

11.6.4 用户自定义的流操纵算子

用户可以建立自己的流操纵算子。图11.19中的程序说明了如何建立和使用新的流操纵算子bell、ret、tab和endline。用户还可以建立自己的带参数的流操纵算子,这方面内容可参看系统的手册。

 /! Fig. 11.19: fig11_19.cpp
 // Creating and testing user-defined, nonparameterized
 // stream manipulators.
 #include < iostream.h>
 // bell manipulator (using escape sequence \a)
 ostream& bell( ostream& output ) { return output << '\a'; }
 // ret manipulator (using escape sequence \r)
 ostream& ret( ostream& output ) ( return output << '\r'; }
 // tab manipulator (using escape sequence \t)
 ostream& tab( ostream& output ) { return output << '\t'; ]
 // endLine manipulator (using escape sequence \n
 // and the flush member function)
 ostream& endLine( ostream& output )
 {
   return output << '\n' << flush;
 }
 int main()
 {
   cout << "Testing the tab manipulator:" << endLine
       << 'a' << tab << 'b' << tab << 'c' << endLine
       << "Testing the ret and bell manipulators:"
       << endLine << "..........  ";
   cout << bell;
   cout << ret <<  .........  << endLine;
   return 0;
 }

输出结果:

Testing the tab manipulator:
a b c
Testing the ret and bell manipulators:
--------........

图 11.19 建立并测试用户自定义的、无参数的流操作算子