07slide (Math Functions Characters and Strings)
07slide (Math Functions Characters and Strings)
Mathematical Functions,
Characters, and Strings
Motivations
Suppose you need to estimate the area enclosed by four
cities, given the GPS locations (latitude and longitude) of
these cities, as shown in the following diagram. How
would you write a program to solve this problem? You will
be able to write such a program after completing this
chapter.
Objectives
Mathematical Functions
C++ provides many useful functions in the cmath
header for performing common mathematical
functions.
Trigonometric Functions
Exponent Functions
Rounding Functions
3) returns 3
max(2.5, 3.0) returns 4.0
min(2.5, 4.6) returns 2.5
abs(-2) returns 2
abs(-2.1) returns 2.1
Run
9
10
Read Characters
To read a character from the keyboard, use
cout << "Enter a character: ";
char ch;
cin >> ch;
11
12
13
14
15
16
Note
It is worthwhile to note that the ASCII for
lowercase letters are consecutive integers starting
from the code for 'a', then for 'b', 'c', ..., and 'z'. The
same is true for the uppercase letters. Furthermore,
the ASCII code for 'a' is greater than the code for
'A'. So 'a' - 'A' is the same as 'b' - 'B'. For a
lowercase letter ch, its corresponding uppercase
letter is static_cast<char>('A' + (ch - 'a')).
17
ToUppercase
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
18
19
20
50 + rand() % 50
a + rand() % b
DisplayRandomCharacter
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
21
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Set1
2
10
18
26
3
11
19
27
6
14
22
30
Set2
7
15
23
31
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Set3
Set4
GuessBirthday
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
16
20
24
28
17
21
25
29
18
22
26
30
19
23
27
31
Set5
Run
22
Character Functions
CharacterFunctions
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
23
HexDigit2Dec
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
24
25
26
27
Concatenating Strings
string s3 = s1 + s2;
message += " and programming is fun";
28
Comparing Strings
You can use the relational operators ==, !=, <, <=, >, >= to compare
two strings. This is done by comparing their corresponding
characters on by one from left to right. For example,
string s1 = "ABC";
string s2 = "ABE";
cout << (s1 == s2) << endl; // Displays 0 (means false)
cout << (s1 != s2) << endl; // Displays 1 (means true)
cout << (s1 > s2) << endl; // Displays 0 (means false)
cout << (s1 >= s2) << endl; // Displays 0 (means false)
cout << (s1 < s2) << endl; // Displays 1 (means true)
cout << (s1 <= s2) << endl; // Displays 1 (means true)
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
29
Reading Strings
1
2
3
4
string city;
cout << "Enter a city: ";
cin >> city; // Read to array city
cout << "You entered " << city << endl;
1
2
3
4
string city;
cout << "Enter a city: ";
getline(cin, city, '\n'); // Same as getline(cin, city)
cout << "You entered " << city << endl;
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
30
Example
Write a program that prompts the user to enter two cities and
displays them in alphabetical order.
OrderTwoCities
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
31
LotteryUsingStrings
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Run
32
Description
setprecision(n)
fixed
showpoint
setw(width)
left
right
33
setprecision(n) Manipulator
double number = 12.34567;
cout << setprecision(3) << number << " "
<< setprecision(4) << number << " "
<< setprecision(5) << number << " "
<< setprecision(6) << number << endl;
displays
12.312.3512.34612.3457
34
fixed Manipulator
double monthlyPayment = 345.4567;
double totalPayment = 78676.887234;
cout << fixed << setprecision(2)
<< monthlyPayment << endl
<< totalPayment << endl;
displays
345.46
78676.89
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
35
showpoint Manipulator
cout << setprecision(6);
cout << 1.23 << endl;
cout << showpoint << 1.23 << endl;
cout << showpoint << 123.0 << endl;
displays
1.23
1.23000
123.000
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
36
setw(width) Manipulator
cout << setw(8) << "C++" << setw(6) << 101 << endl;
cout << setw(8) << "Java" << setw(6) << 101 << endl;
cout << setw(8) << "HTML" << setw(6) << 101 << endl;
displays
37
38
39
Radians
0.5236
1.0472
Sine
0.5000
0.8660
Cosine
0.8660
0.5000
FormatDemo
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
Tangent
0.5773
1.7320
Run
40
Run
41
Run
42