?? 11章 c++輸入輸出流.txt
字號:
11.6.1 整數流的基數:流操縱算子dec、oct、hex和setbase
整數通常被解釋為十進制(基數為10)整數。如下方法可改變流中整數的基數:插人流操縱算子hex可設置十六進制基數(基數為16)、插人流操縱算子oct可設置八進制基數(基數為8)、插人流操縱算子dec可恢復十進制基數。
也可以用流操縱算子setbace來改變基數,流操縱算于setbase帶有一個整數參數10、8或16。因為流操縱算子setbase是帶有參數的,所以也稱之為參數化的流操縱算子。使用setbose或其他任何參數化的操縱算子都必須在程序中包含頭文件iomanip.h。如果不明確地改變流的基數,流的基數是不變的。圖11.16中的程序示范了流操縱算子hex、oct、dec和setbase的用法。
1 // Fig. 11.16: fig11_16.cpp
2 // Using hex, oct, dec and setbase stream manipulators.
3 #include <iostream.h>
4 #include <iomanip.h>
5
6 int main()
7{
8 int n;
9
10 cout << "Enter a decimal number: ";
11 cin >> n;
12
13 cout << n << "in hexadecimal is:"
14 << hex << n << '\n'
15 << dec << n << "in octal is:"
16 << oct << n << '\n'
17 << setbase( 10 ) << n <<" in decimal is:"
18 << n << endl;
19
20 return 0;
21 }
輸出結果:
Enter a decimal number: 20
20 in hexadecimal is: 14
20 in octal is: 24
20 in decimal is: 20
圖 11.16 使用流操縱算子hex、oct、dec和setbase
11.6.2 設置浮點數精度(precision、setprecision)
在C++中可以人為控制浮點數的精度,也就是說可以用流操縱算子setprecision或成員函數percision控制小數點后面的位數。設置了精度以后,該精度對之后所有的輸出操作都有效,直到下一次設置精度為止。無參數的成員函數percision返回當前設置的精度。圖11.17中的程序用成員函數precision和流操縱算子setprecision打印出了2的平方根表,輸出結果的精度從0連續變化到9。
1 // Fig. 11.17: fig11_17.cpp
2 // Controlling precision of floating-point values
3 #include <iostream.h>
4 #include <iomanip.h>
5 #include <math.h>
6
7 int main()
8
9 double root2 = sqrt( 2.0 );
10 int places;
11
12 cout << setiosflags(ios::fixed)
13 << "Square root of 2 with precisions 0-9.\n"
14 << "Precision set by the"
15 << "precision member function:" << endl;
16
17 for ( places = 0; places <= 9; places++ ) {
18 cout.precision( places );
19 cout << root2 << '\n';
20 }
21
22 cout << "\nPrecision set by the"
23 << "setprecision manipulator:\n";
24
25 for ( places = 0; places <= 9; places++ )
26 cout << setprecision( places ) << root2 << '\n';
27
28 return 0;
29 }
輸出結果:
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系統)。
1 // fig11_18.cpp
2 // Demonstrating the width member function
3 #include <iostream.h>
4
5 int main()
6{
7 int w = 4;
8 char string[ 10 ];
9
10 cout << "Enter a sentence:\n";
11 cin.width( 5 );
12
13 while (cin >> string ) {
14 cout.width( w++ );
15 cout << string << endl;
16 cin.width( 5 );
17 }
18
19 return 0;
20 }
輸出結果:
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。用戶還可以建立自己的帶參數的流操縱算子,這方面內容可參看系統的手冊。
1 /! Fig. 11.19: fig11_19.cpp
2 // Creating and testing user-defined, nonparameterized
3 // stream manipulators.
4 #include <iostream.h>
5
6 // bell manipulator (using escape sequence \a)
7 ostream& bell( ostream& output ) { return output << '\a'; }
8
9 // ret manipulator (using escape sequence \r)
10 ostream& ret( ostream& output ) ( return output << '\r'; }
11
12 // tab manipulator (using escape sequence \t)
13 ostream& tab( ostream& output ) { return output << '\t'; ]
14
15 // endLine manipulator (using escape sequence \n
16 // and the flush member function)
17 ostream& endLine( ostream& output )
18 {
19 return output << '\n' << flush;
20 }
21
22 int main()
23 {
24 cout << "Testing the tab manipulator:" << endLine
25 << 'a' << tab << 'b' << tab << 'c' << endLine
26 << "Testing the ret and bell manipulators:"
27 << endLine << ".......... ";
28 cout << bell;
29 cout << ret << ......... << endLine;
30 return 0;
31 }
輸出結果:
Testing the tab manipulator:
a b c
Testing the ret and bell manipulators:
--------........
圖 11.19 建立并測試用戶自定義的、無參數的流操作算子
C++大學教程(第11章 C++輸入/輸出流-02)
11.7 流格式狀態
各種格式標志指定了在I/O流中執行的格式類型。成員函數serf、unserf和flag控制標志的設置。
11.7.1 格式狀態標志
圖11.20中的格式狀態標志在類ios中被定義為枚舉值,留到下幾節解釋。
雖然成員函數flags、serf和unseff可以控制這些標志,但是許多C++程序員更喜歡使用流操縱算子(見11.7.8節)。程序員可以用按位或操作(|)把各種標志選項結合在一個long類型的值中(見圖11.23)。調用成員函數flags并指定要進行或操作的標志選項就可以在流中設置這些標志,并返回一個包含以前標志選項的long類型的值。返回值通常要保存起來,以便用保存值調用flags來恢復以前的流選項。
flags函數必須指定一個代表所有標志設置的值。而帶有一個參數的函數setf可指定一個或多個要進行或操作的標志,并把它們與現存的標志設置相“或”,形成新的格式狀態。
參數化的流操縱算子setiosflags與成員函數serf執行同樣的功能。流操縱算子resetiosflags與成員函數unsetf執行同樣的功能。不論使用哪一種流操縱算子,在程序中都必須包含頭文件iomanip.h。
skipws標志指示>>跳過輸人流中的空白字符。>>的默認行為是跳過空白字符,調用unsetf(ios::skipws)可改變默認行為。流操縱算子ws也可用于指定跳過流中的空白字符。
----------------------------------------------------------------------------------------------
格式狀態 說明
----------------------------------------------------------------------------------------------
ios::skipws 跳過輸入流中的空白字符
ios::left 在域中左對齊輸出,必要時在右邊顯示填充字符
ios:right 在域中右對齊輸出,必要時在左邊顯示填充字符
ios::internal 表示數字的符號應在域中左對齊,而數字值應在域中右對齊(即在符號和數字之間填充字符
ios::dec 指定整數應作為十進制(基數10)值
ios::oct 指定整數應作為八進制(基數8)值
ios::hex 指定整數應作為十六進制(基數16)值
ios::showbase 指定在數字疥面輸出進制(0表示八進制,0x或0X表示十六進制)
ios::showpoint 指定浮點數輸出時應帶小數點。這通常和ios::fixed一起使用保證小數點后面有一定位數
ios::uppercase 指定表示十六進制Rx應為大寫,表示浮點科學計數法的e應為大寫
ios::showpos 指定正數和負數前面分別加上正號或-號
ios::scientific 指事實上浮點數輸出采用科學計數法
ios::fixed 指事實上浮點數輸出采用定點符號,保證小數點后面有一定位數
-----------------------------------------------------------------------------------------------
圖 11.20 格式狀態標志
11.7.2 尾數零和十進制小數點(ios::showpoint)
設置showpoint標志是為了強制輸出浮點數的小數點和尾數零。若沒有設置showpoint,浮點數79.0將被打印為79,否則打印為79.000000(或由當前精度指定的尾數零的個數)。圖11.21中的程序用成員函數setf設置showpoint標志,從而控制了浮點數的尾數零和小數點的輸出。
i // Fig. 11.21: fig11_21.cpp
2 // Controlling the printing of trailing zeros and decimal
3 // points for floating-point values.
4 #include <iostream.h>
5 #include <iomanip.h>
6 #include <math.h>
7
8 int main()
9 {
10 cout << "Before setting the ios::showpoint flag\n"
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -