⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 11章 c++输入输出流.txt

📁 C++大学教程txt版中文版 C++大学教程txt版中文版
💻 TXT
📖 第 1 页 / 共 5 页
字号:
11       << "9.9900 prints as: "<< 9.9900
12       << "\n9.9000 prints as: "<< 9.9000
13       << "\n9.0000 prints as: "<< 9.0000
14       << "\n\nAfter setting the ios::showpoint flag\n";
15   cout.setf( ios::showpoint );
16   cout << "9.9900 prints as: "<< 9.9900
17       << "\n9.90O0 prints as: "<< 9.9000
18       << "\n9.0000 prints as: "<< 9.0000 << endl;
19   return 0;
2O }

输出结果:
Before setting the ios::showpolnt flag
9.9900 prints as: 9.99
9.9000 prints as: 9.9
9.9000 prints as: 9

After setting the ios::showpoint flag
9.9900 prints as: 9.99000
9.9000 prints as: 9.90000
9.0000 prints as: 9.00000

               图 11.21 控制浮点数的尾数零和小数点的输出

11.7.3  对齐(ios::left、ios::right、ios::internal)
    left标志可以使输出域左对齐并把填充字符放在输出数据的右边,right标志可以使输出域右对齐并把填充字符放在输出数据的左边。填充的字符由成员函数fill或参数化的流操纵算子setfill指定(见7.4节)。图11.22用流操纵算子setw、setiosflags、reseticsfiags以及成员函数serf和unsetf控制整数值在域宽内左对齐和右对齐。
1 // Fig. 11.22: fig11_22.cpp
2 // Left-justification and right-justification.
3 #include <iostream.h>
4 #include <iomanip.h>
6 int main()
7{
8   int x = 12345;
9
10   cout << "Default is right justified:\n"
11       << setw(10) << x << "\n\nUSING MEMBER FUNCTIONS"
12       << "\nUse serf to set ios::left:\n" << setw(10);
13
14   cout.setf( ios::left, ios::adjustfield );
15   cout << x << "\nUse unsetf to restore default:\n";
16   cout.unsetf( ios::left );
17   cout << setw( 10 ) << x
18       << "\n\nUSING PARAMETERIZED STREAM MANIPUAATORS"
19       << "\nUse setiosflags to set ios::left:\n"
20       << setw( 10 ) << setiosflags( ios::left ) << x
21       << "\nUse resetiosflags to restore default:\n"
22       << setw( 10 ) << resetiosflags( ios::left )
23       << x << endl;
24   return 0;
25 }

输出结果:
Default is right justified:
        12345
USING MEMBER FUNCTIONS
Use setf to set ios::left:
        12345
Use unsetf to restore default:
12345
USING PARAMETERIZED STREAM MANIPULATORS
Use setiosflags to set ios::left:
12345
Use resetiosflags to restore default:
      12345
                                
                                   图 11.22 左对齐和右对齐

    internal标志指示将一个数的符号位(设置了ios::showbase标志时为基数,见11.7.5节)在域宽内左对齐,数值右对齐,中间的空白由填充字符填充。left、right和internal标志包含在静态数据成员ios::adjustfield中。在设置left、right和internal对齐标志时,setf的第二个参数必须是ios::adjustfield,这样才能使serf只设置其中一个标志(这三个标志是互斥的)。图11.23中的程序用流操纵算子setiosflags和setw指定中间的空白。注意,ios::showpos标志强制打印了加号。
1 // Fig. 11.23: figll_23.cpp
2 // Printing an integer with internal spacing and
3 // forcing the plus sign.
4 #include <iostream.h>
5 #include <iomanip.h>
6
7 int main()
8 {
9   cout << setiosflags( ios::internal | ios::showpos )
10      << setw( 10 ) << 123 << endl;
11    return  O;
12  }

输出结果:
  +    123

                      图 11.23  打印中间带空白的整数并强制输出加号

11.7.4  设置填充字符(fill、setfill)
    成员函数fill设置用于使输出域对齐的填充字符,如果不特别指定,空格即为填充字符。fill函数返回以前设置的填充字符。图11.24中的程序演示了使用成员函数fill和流操纵算子,setfill来控制填充字符的设置和清除。
1 // Fig. 11.24: fig11_24.cpp
2 // Using the fill member function and the setfill
3 // manipulator to change the padding character for
4 // fields larger than the values being printed.
5 #include <iostream.h>
6 finclude <iomanip.h>
7
8 int main()
9 {
10   int x = 10000;
11
12   cout << x << "printed as iht right and left justified\n"
13       << "and as hex with internal justification.\n"
14       << "Using the default pad character (space):\n";
15   cout.setf( ios::showbase );
16   cout << setw( 10 ) << x << '\n';
17   cout.setf( ios::left, ios::adjustfield );
18   cout << setw( 10 } << x << '\n';
19   cout.serf( ios::internal, ios::adjustfield );
20   cout << setw( 10 ) << hex << x;
21
22   cout << "\n\nUsing various padding characters:\n";
23   cout.setf( ios::right, ios::adjustfield );
24   cout.fill( '*' );
25   cout << setw( 10 ) << dec << x << '\n';
26   cout.setf( ios::left, ios::adjustfield );
27   cout << setw( 10 ) << setfill( '%' ) << x << '\n';
28   cout.setf( ios::internal, ios::adjustfield );
29   cout << setw( 10 ) << setfill( '^' ) << hex << x << endl;
30   return 0;
31 }

输出结果:
10000 printed as int right and left justified
and as hex with internal justification.
Using the default pad character (space):
         l0000
 10000

0x2710

Using various padding characters:
*****10000
10000%%%%%

                    图 11.24 用fill和setfill为实际宽度小于域宽的数据改变填充字符

11.7.5  整数流的基数:(ios::dec、ios::oct、ios::hex、ios::showbase)
    静态成员ios::basefield(在setf中的用法与ios::adjustfield类似)包括ios::oct、ios::hex和ios::dec标志位,这些标志位分别指定把整数作为八进制、十六进制和十进制值处理。如果没有设置这些位,则流插入运算默认整数为十进制数,流读取运算按整数提供的方式处理数据(即以零打头的整数按八进制数处理,以0x或0X打头的按十六进制数处理,其他所有整数都按十进制数处理)。一旦为流指定了特定的基数,流中的所有整数都按该基数处理,直到指定了新的基数或程序结束为止。
    设置showbase标志可强制输出整数值的基数。十进制数以通常方式输出,输出的八进制数以0打头,输出的十六进制数以0x或OX打头(由uppercase标志决定是0x还是0X,见11.7.7节)。图11.25中的程序用showbase标志强制整数按十进制、八进制和十六进制格式打印。
1 // Fig. 11.25: figll_25.cpp
2 // Using the ios::showbase flag
3 #include <iostream.h>
4 #include <iomanip.h>
5
6 int main()
7 {
8   int x = 100;
9
10   cout << setiosflags( ios::showbase )
11       << "Printing integers preceded by their base:\n"
12       << x << '\n'
13       << oct << x << '\n'
14       << hex << x << endl;
15   return 0;
16 }

输入结果:
Printing integers preceded by their base:
I00
0144
0x64

                                    图 11.25 使用ios::showbase标志

11.7.6  浮点数和科学记数法(ios::scientific、ios::fixed)
    ios::scientific和ios::fixed标志包含在静态数据成员ios::floatfield中(在setf中的用法与ios::  adjustfield和ios::basefield类似)。这些标志用于控制浮点数的输出格式。设置scientific标志使浮点数按科学记数法输出,设置fixed标志使浮点数按照定点格式输出,即显示出小数点,小数点后边
有指定的位数(由成员函数precision指定)。若没有这些设置,则浮点数的值决定输出格式。
    调用cout.setf(O,ios::floatfield)恢复输出浮点数的系统默认格式。图11.26中的程序示范了以定点格式和科学记数法显示的浮点数。
1 // Fig. 11.26: flg11_26.cpp
2 // Displaying floating-point values in system default,
3 // scientific, and fixed formats.
4 #include <iostream.h>
5
6 int main()
7 {
8   double x = .001239567, y = 1.946e9;
9
10   cout << "Displayed in default format:\n"
11       << x << '\t' << y << '\n';
12   cout.setf( ios::scientific, ios::floatfield );
13   cout << "Displayed in scientific format:\n"
14       << x << '\t' << y << '\n';
15   cout.unsetf( ios::scientific );
16   cout << "Displayed in default format after unsetf:\n"
17       << x << ,\t' << y << ,\n,;
18   cout.setf( ios::fixed, ios::floatfield );
19   cout << "Displayed in fixed format:\n"
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

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

11.7.7  大/小写控制(ios::upercase)
    设置ios::uppercase标志用来使大写的X和E分别同十六进制整数和以科学记数法表示的浮点数一起输出(见图11.27)一旦设置ios::uppercase标志,十六进制数中的所有字母都转换成大写。
1 // Fig. 11.27: fig11_27.cpp
2 // Using the ios::uppercase flag
3 #include <iostream.h>
4 #include <iomanip.h>
5
6 int main()
7 {
8     cout << setiosflags( ios::uppercase )
9          << "Printing uppercase letters in scientific\n"
10         << "notation exponents and hexadecimal values:\n"
11         << 4.345e10 << '\n' << hex << 123456789 << endl;
12   return O;
13 }

输出结果:
Printing uppercase letters in scientific
notation exponents and hexadecimal values:
4.395E+010
75BCD16

                               图 11.27 使用ios::uppercase标志

11.7.8  设置及清除格式标志(flags、setiosflags、resetosflags)
    无参数的成员函数flags只返回格式标志的当前设置(long类型的值)。带一个long类型参数的成员函数flags按参数指定的格式设置标志,返回以前的标志设置。flags的参数中未指定的任何格式都会被清除。图11.18的程序用成员函数flags设置新的格式状态和保存以前的格式状态,然后恢复原来的格式设置。
1 // Fig. 11.28: fig11_28.cpp
2 // Demonstrating the flags member function.
3 #include <iostream.h>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -