?? 11章 c++輸入輸出流.txt
字號:
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 + -