計算機概論實習
2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators to change base dec (resets to base 10) cout << dec << 1000 1000 hex (base 16) cout << hex << 1000; 3e8 oct (base 8) cout << oct << 1000; 1750
3 Integral Stream Base expression: dec, oct, hex, setbase, and showbase setbase(newBase) One of 8, 10, or 16 cout << setbase(dec) << 1000; Base remains same until explicitly changed showbase Forces base of number to be shown Decimal numbers default 1000 Preceding 0x or 0X for hex 0x3e8 Preceding 0 for octal Remove with noshowbase
4 Floating-Point Precision (precision, setprecision) Set precision of floating point numbers Number of digits to right of decimal setprecision stream manipulator Pass number of decimal points cout << setprecision(5) precision member function cout.precision(newPrecision) With no arguments, returns current precision Settings remain until changed explicitly
5 precision example int main(){ double root2 = sqrt( 2.0 ); //sqrt(2.0) = ….. for (int i = 0; i <= 11 ; i++){ cout.precision( i ); cout << root2 << endl; } return 0; }
6 Field Width (width, setw) width member function (base class ios_base) Sets field width Number of character positions for output Can also use setw stream manipulator
7 width, setw example int main(){ const int SIZE= 80; char buffer[SIZE]; cout << "Enter a sentence:" << endl; cin.get(buffer, SIZE); cout << "set width 5" << endl; cout.width(5); cout << buffer << endl; cout << "set width 6\n" << setw(6) << buffer << endl; return 0; }
8 Trailing Zeros and Decimal Points (showpoint) showpoint Forces decimal number to print with trailing zeros For decimal number without showpoint with showpoint (up to level of precision) Reset with noshowpoint
9 showpoint example int main(){ cout << showpoint << "After using showpoint" << endl << " prints as: " << << endl << " prints as: " << << endl << " prints as: " << << endl << "9 prints as: " << 9 << endl; return 0; } After using showpoint prints as: prints as: prints as: prints as: 9
10 Justification (left, right and internal) left stream manipulator Left-justified, padding to right right stream manipulator Right-justified, padding to left internal Number's sign left-justified Number's value right-justified showpos forces sign to print Remove with noshowpos
11 left, right and internal example int x = 12345; cout << showpos; cout << "Default is right justified:" << endl << setw( 10 ) << x; cout << "\n\nUse std::left to left justify x:\n" << left << setw( 10 ) << x; cout << "\n\nUse std::right to right justify x:\n" << right << setw( 10 ) << x << endl; cout << "\n\nUse std::internal to internal justify x:\n" << internal << setw(10) << x << endl;
12 left, right and internal example Default is right justified: Use std::left to left justify x: Use std::right to right justify x: Use std::internal to internal justify x:
13 Padding (fill, setfill) Set fill character used in padding fill member function cout.fill('*') cout.fill( '*' ); cout << setw( 10 ) << dec << x << endl; *****10000 setfill stream manipulator setfill( '*' ) cout << setfill( '*' ) << setw( 10 ) << dec << x << endl;
14 Floating-Point Numbers; scientific and fixed) Stream manipulator scientific Forces scientific notation e+009 Stream manipulator fixed Forces fixed point format Prints number of decimals specified by precision
15 Scientific and fixed example double x = ; double y = 1.946e9; cout << "Displayed in default format:" << endl << x << '\t' << y << endl; cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl; cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl;
16 Uppercase/Lowercase Control (uppercase) Stream manipulator uppercase Uppercase E in scientific notation 1E10 Uppercase X in hex notation and uppercase hex letters 0XABCD By default, lowercase Reset with nouppercase
17 Specifying Boolean Format (boolalpha) Data type bool Values true or false Outputs 0 (false) or 1 (true) when used with << Displayed as integers Stream manipulator boolalpha Display strings "true" and "false" Reset with noboolalpha bool boolvalue = true; cout << “The result is " << boolalpha << boolvalue; The result is true
18 Setting and Resetting the Format State via Member-Function flags Can save/restore format states After apply many changes, may want to restore original Member function flags cout.flags() With no argument Returns current state as fmtflags object Namespace ios_base Represents format state cout.flags(0) : reset format With fmtflags argument Sets state Returns previous state
19 Example int main() { int integerValue = 1000; double doubleValue = ; cout << "The value of the flags variable is: " << cout.flags() << "\nPrint int and double in original format:\n" << integerValue << '\t' << doubleValue << endl << endl; ios_base::fmtflags originalFormat = cout.flags(); cout << showbase << oct << scientific; // change format
20 Example cout << "The value of the flags variable is: " << cout.flags() << "\nPrint int and double in a new format:\n" << integerValue << '\t' << doubleValue << endl << endl; cout.flags( originalFormat ); // restore format cout << "The restored value of the flags variable is: " << cout.flags() << "\nPrint values in original format again:\n" << integerValue << '\t' << doubleValue << endl; return 0; }
21 The value of the flags variable is: 513 Print int and double in original format: The value of the flags variable is: Print int and double in a new format: e-002 The restored value of the flags variable is: 513 Print values in original format again:
22 Practice 2 (P2) Please write a program which input interface is as follows: and the output interface is like below: 請輸入學生的三個成績 >: 80,90,90 國文 英文 數學 平均 字元寛