Tải bản đầy đủ (.ppt) (13 trang)

C++ lecture 8

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (186.43 KB, 13 trang )

C++ Programming
Lecture 8

Input/Output
Manipulations
The Hashemite University
Computer Engineering
Department
(Adapted from the textbook slides)


Outline








Introduction.
Floating-Point Numbers; Scientific
Notation.
Floating-Point Precision.
Field Width.
Trailing Zeros and Decimal Points.
Padding.
The Hashemite University

2



Input/Output
Manipulations I






C++ provides various stream manipulators that
perform formatting tasks.
Till now we have dealt with screen output using
cout. So, all these manipulators will format the
text that appears on the screen.
Examples:







Setting the precision of the displayed floating point
numbers.
Setting the field width.
Showing trailing zeros and decimal point in floating
point numbers.
Filling field with specific characters.
The Hashemite University


3


Input/Output
Manipulations II




All the previous tasks or functions
are provided via the iomanip library
(short for input/output manipulation).
So, when you want to use any of
these functions you must include this
library in your code.
#include<iomanip>
using namespace std;

The Hashemite University

4


Input/Output
Manipulations III


These functions or operators are
classified into:



Sticky operators:




Applied to all subsequent cout statements
that come after calling the specified
function.

Non-sticky operators:


Applied only to the first cout statement that
comes after calling the specified function.
The Hashemite University

5


Floating-Point Numbers; Scientific
Notation (scientific, fixed)


scientific
 forces output of a floating point number in scientific notation:


1.946000e+009


Only one digit in the integer part of the floating point number
and n digits after the floating point (depends on the precision
value and by default its 6) and put the suitable power after the
e exponent.

E.g.: cout << scientific << 234.5678; // 2.345678e+002
fixed
 forces floating point numbers to display a specific number of
digits to the right of the decimal point (specified with
precision) and the whole integral part.

E.g.: cout << fixed << 234.5e-1;// 23.450000






Both of then are applied for all subsequent cout
statements, i.e. sticky.



The default setting in C++ is neither the fixed nor the scientific
formatting. It depends on the original value of the floating point
number.
The Hashemite University

6



Floating-Point Precision
(setprecision)


setprecision







sets number of digits to the right of decimal point (with
rounding)
parameterized stream manipulator
Like all parameterized stream manipulators, <iomanip>
required
specify precision:
cout << fixed << setprecision(2) << x;






Changes last until a different value is set, i.e. sticky.
The default precision for the compiler is 6.
Applied to floating point numbers only.
Must use the fixed command to force the compiler

to use the specified precision.
The Hashemite University

7


Field Width(setw)


setw stream manipulator








sets field width (number of character positions a value should be
output or number of characters that should be input).
Associated with the decimal part of the number if it is floating
point
returns to previous width after the implementation (so it is
applied to the next insertion or extraction only), i.e. non-sticky.

When applied to cout:
If values processed are smaller than width, fill characters
inserted as padding.

Large values are not truncated - full number printed.


Displayed output is right justified by default.

Examples:
int number=123456.99887;
cout << setw(5) << number; // will display 123456
Cout << setw(8) << number; //will display
123456


The Hashemite University

2

8


Trailing Zeros and Decimal
Points (showpoint)


ios::showpoint


forces a float with an integer value to be printed with its decimal
point and trailing zeros

cout << showpoint << 79.0;
will print 79.00000 (this is not the default case,
the default case with the precision=6 therefore

79.0 will be displayed as 79.0000)
 number of zeros determined by precision settings
 Applied for all subsequent output operations, i.e. sticky.
 To reset this option apply the following command:
cout << noshowpoint;
 Remember trailing zeros are applied to floating point
numbers only and not for integers.
 It is not identical to fixed command, pay
attention!!!!!!
The Hashemite University

9


Padding (setfill)









If you specify the field width to 10 and you print
56 then this number will be preceded by white
spaces.
White space here is the fill character.
You can change this fill character to any other
character you want.

Applied for all subsequent cout statements. To
remove it set the fill character to white space to
return to the default settings.
setfill manipulator


sets fill character
cout << setfill ('*');
cout << setw(5) << 3; //output is ****3
The Hashemite University

10


Examples
cout< cout< cout<92017.24
 double x=920.09346;
cout< double x=786.8357017;
cout<(remember by default the precision is 6)
 double x=786.835701;
cout<(remember by default the precision is 6)
4
 double x=4274.12;
spaces

cout<4274.12
 double x=123.589;
cout<

The Hashemite University

11


Showpoint vs. fixed
double x=91.4792;
cout<cout<cout<cout<91.479200
cout<statement restore the default precision to 6

double x=91.4792;
cout<cout<cout<cout<
double x=91.4792;
cout<cout<

fixed is sticky

double x=91.4792;
The Hashemite University// will print 91.5
12
cout<


Additional Notes


This lecture covers the following
material from the textbook:


Fourth edition:


Chapter 12: Sub-Sections 12.6.2, 12.6.3,
12.7.1, 12.7.3, 12.7.5

The Hashemite University

13



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×