22
計計計計計計計 計計計計計計計 2007-03-16 2007-03-16

計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

  • View
    217

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

計算機概論實習計算機概論實習2007-03-162007-03-16

Page 2: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

2

Integral Stream Base expression: dec, oct, hIntegral Stream Base expression: dec, oct, hex, setbase, and showbaseex, setbase, and showbase

Use header <iomanip> 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

Page 3: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

3

Integral Stream Base expression: dec, oct, hIntegral Stream Base expression: dec, oct, hex, setbase, and showbaseex, 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 01750

Remove with noshowbase

Page 4: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

4

Floating-Point Precision (precision, setpreciFloating-Point Precision (precision, setprecision)sion)

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

Page 5: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

5

precisionprecision example example

int main(){

double root2 = sqrt( 2.0 ); //sqrt(2.0) = 1.414213562…..

for (int i = 0; i <= 11 ; i++){

cout.precision( i );

cout << root2 << endl;

}

return 0;

}

1.4142111.41.411.4141.41421.414211.4142141.41421361.414213561.4142135621.4142135624

Page 6: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

6

Field Width (width, setw)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

Page 7: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

7

width, setwwidth, setw example 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;

}

Page 8: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

8

Trailing Zeros and Decimal Points (shoTrailing Zeros and Decimal Points (showpoint)wpoint)

showpoint Forces decimal number to print with trailing zeros For decimal number 79.0

79 without showpoint 79.000000 with showpoint (up to level of precision)

Reset with noshowpoint

Page 9: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

9

showpointshowpoint example example

int main(){

cout << showpoint

<< "After using showpoint" << endl

<< "9.9900 prints as: " << 9.9900 << endl

<< "9.9000 prints as: " << 9.9000 << endl

<< "9.0000 prints as: " << 9.0000 << endl

<< "9 prints as: " << 9 << endl;

return 0;

} After using showpoint9.9900 prints as: 9.990009.9000 prints as: 9.900009.0000 prints as: 9.000009 prints as: 9

Page 10: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

10

Justification (left, right and internal)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

+ 123 showpos forces sign to print

Remove with noshowpos

Page 11: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

11

left, right and internalleft, right and internal example 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;

Page 12: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

12

left, right and internalleft, right and internal example example

Default is right justified: +12345

Use std::left to left justify x:+12345

Use std::right to right justify x: +12345

Use std::internal to internal justify x:+ 12345

Page 13: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

13

Padding (fill, setfill)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;

Page 14: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

14

Floating-Point Numbers; scientific and Floating-Point Numbers; scientific and fixed)fixed)

Stream manipulator scientific Forces scientific notation

1.946000e+009

Stream manipulator fixed Forces fixed point format Prints number of decimals specified by precision

1946000000.000000

Page 15: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

15

Scientific and fixedScientific and fixed example example

double x = 0.001234567;

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;

Page 16: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

16

Uppercase/Lowercase Control Uppercase/Lowercase Control (uppercase)(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

Page 17: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

17

Specifying Boolean Format (boolalpha)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 noboolalphabool boolvalue = true;

cout << “The result is " << boolalpha << boolvalue; The result is true

Page 18: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

18

Setting and Resetting the Format State Setting and Resetting the Format State via Member-Function flagsvia 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

Page 19: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

19

ExampleExample

int main() {

int integerValue = 1000;

double doubleValue = 0.0947628;

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

Page 20: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

20

ExampleExample

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;

}

Page 21: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

21

The value of the flags variable is: 513Print int and double in original format:1000 0.0947628  

The value of the flags variable is: 012011Print int and double in a new format:01750 9.476280e-002 The restored value of the flags variable is: 513Print values in original format again:1000 0.0947628

Page 22: 計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators

22

Practice 2 (P2)Practice 2 (P2)

Please write a program which input interface is as follows:

and the output interface is like below:

請輸入學生的三個成績>: 80,90,90

國文 英文 數學 平均 80 90 90 86.67

10 字元寛 10 字元寛 10 字元寛