© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 3: Input/Output
Basic Elements of C++ Chapter 2.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
1 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter Topics The Basics of a C++ Program Data Types
Chapter 4 Mathematical Functions, Characters, and Strings
Basic Elements of C++.
Chapter 3: Expressions and Interactivity.
Basic Elements of C++ Chapter 2.
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 3: Input/Output
Chapter 3 Input output.
Chapter 3 Mathematical Functions, Strings, and Objects
Chapter 2 Primitive Data Types and Operations
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 2 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.

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.3 Objectives F To solve mathematics problems by using the C++ mathematical functions (§4.2). F To represent characters using the char type (§4.3). F To encode characters using ASCII code (§4.3.1). F To read a character from the keyboard (§4.3.2). F To represent special characters using the escape sequences (§4.3.3). F To cast a numeric value to a character and cast a character to an integer (§4.3.4). F To compare and test characters (§4.3.5). F To program using characters (DisplayRandomCharacter, GuessBirthday) (§§ ). F To test and convert characters using the C++ character functions (§4.6). F To convert a hexadecimal character to a decimal value (HexDigit2Dec) (§4.7). F To represent strings using the string type and introduce objects and instance functions (§4.8). F To use the subscript operator for accessing and modifying characters in a string (§4.8.1). F To use the + operator to concatenate strings (§4.8.2). F To compare strings using the relational operators (§4.8.3). F To read strings from the keyboard (§4.8.4). F To revise the lottery program using strings (LotteryUsingStrings) (§4.9). F To format output using stream manipulators (§4.10). F To read and write data from/to a file (§4.11).

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 4 Mathematical Functions C++ provides many useful functions in the cmath header for performing common mathematical functions.

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 5 Trigonometric Functions

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 6 Exponent Functions

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 7 Rounding Functions

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 8 The min, max, and abs Functions F max(2, 3) returns 3 F max(2.5, 3.0) returns 4.0 F min(2.5, 4.6) returns 2.5 F abs(-2) returns 2 F abs(-2.1) returns 2.1

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 9 Case Study: Computing Angles of a Triangle Write a program that prompts the user to enter the x- and y-coordinates of the three corner points in a triangle and then displays the triangle’s angles. ComputeAnglesRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 10 Character Data Type char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding character. For example, the following statements display character b. char ch = 'a'; cout << ++ch;

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 11 Read Characters To read a character from the keyboard, use cout << "Enter a character: "; char ch; cin >> ch;

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 12 Escape Sequences for Special Characters

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 13 Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 14 ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 15 Casting between char and Numeric Types int i = ' a ' ; // Same as int i = static_cast ( ' a ') ; char c = 97; // Same as char c = static_cast (97);

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 16 Numeric Operators on Characters The char type is treated as if it is an integer of the byte size. All numeric operators can be applied to char operands. A char operand is automatically cast into a number if the other operand is a number or a character. For example, the following statements int i = '2' + '3'; // (int)'2' is 50 and (int)'3' is 51 cout << "i is " << i << endl; // i is decimal 101 int j = 2 + 'a'; // (int)'a' is 97 cout << "j is " << j << endl; cout << j << " is the ASCII code for character " << static_cast (j) << endl; Display i is 101 j is is the ASCII code for character c

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 17 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 ('A' + (ch - 'a')).

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 18 Problem: Converting a Lowercase to Uppercase ToUppercaseRun Write a program that prompts the user to enter a lowercase letter and finds its corresponding uppercase letter.

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 19 Comparing and Testing Characters Two characters can be compared using the comparison operators just like comparing two numbers. This is done by comparing the ASCII codes of the two characters. 'a' < 'b' is true because the ASCII code for 'a' (97) is less than the ASCII code for 'b' (98). 'a' < 'A' is true because the ASCII code for 'a' (97) is less than the ASCII code for 'A' (65). '1' < '8' is true because the ASCII code for '1' (49) is less than the ASCII code for '8' (56).

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 20 Case Study: Generating Random Characters Computer programs process numerical data and characters. You have seen many examples that involve numerical data. It is also important to understand characters and how to process them. Every character has a unique ASCII code between 0 and 127. To generate a random character is to generate a random integer between 0 and 127. You learned how to generate a random number in §3.8. Recall that you can use the srand(seed) function to set a seed and use rand() to return a random integer. You can use it to write a simple expression to generate random numbers in any range. For example,

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 21 Case Study: Generating Random Characters, cont. DisplayRandomCharacter

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 22 Case Study: Guessing Birthdays This section uses the if statements to write an interesting game program. The program can find your birth date. The program prompts you to answer whether your birth date is in the following five sets of numbers: GuessBirthdayRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 23 Character Functions Run

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 24 Case Study: Converting a Hexadecimal Digit to a Decimal Value Write a program that converts a hexadecimal digit into a decimal value. HexDigit2DecRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 25 The string Type string s; string message = "Programming is fun";

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 26 String Subscript Operator For convenience, C++ provides the subscript operator for accessing the character at a specified index in a string using the syntax stringName[index]. You can use this syntax to retrieve and modify the character in a string.

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 27 String Subscript Operator string s = "ABCD"; s[0] = 'P'; cout << s[0] << endl;

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 28 Concatenating Strings string s3 = s1 + s2; message += " and programming is fun";

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 29 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 s2) << endl; // Displays 0 (means false) cout = 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. 30 Reading Strings 1 string city; 2 cout << "Enter a city: "; 3 cin >> city; // Read to array city 4 cout << "You entered " << city << endl; 1 string city; 2 cout << "Enter a city: "; 3 getline(cin, city, '\n'); // Same as getline(cin, city) 4 cout << "You entered " << city << endl;

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 31 Example Write a program that prompts the user to enter two cities and displays them in alphabetical order. OrderTwoCitiesRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 32 Case Study: Revising the Lottery Program Using Strings A problem can be solved using many different approaches. This section rewrites the lottery program in Listing 3.7 using strings. Using strings simplifies this program. LotteryUsingStringsRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 33 Formatting Console Output

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 34 setprecision(n) Manipulator double number = ; cout << setprecision(3) << number << " " << setprecision(4) << number << " " << setprecision(5) << number << " " << setprecision(6) << number << endl; displays 12.3 □ □ □

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 35 fixed Manipulator double monthlyPayment = ; double totalPayment = ; cout << fixed << setprecision(2) << monthlyPayment << endl << totalPayment << endl; displays

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 36 showpoint Manipulator cout << setprecision(6); cout << 1.23 << endl; cout << showpoint << 1.23 << endl; cout << showpoint << << endl; displays

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 37 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

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 38 left and right Manipulators cout << right; cout << setw(8) << 1.23 << endl; cout << setw(8) << << endl; displays □□□□1.23 □□351.34

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 39 left and right Manipulators cout << left; cout << setw(8) << 1.23; cout << setw(8) << << endl; displays 1.23□□□□351.34□□

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 40 Example: Display a Table Degrees Radians Sine Cosine Tangent FormatDemoRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 41 Simple File Output To write data to a file, first declare a variable of the ofstream type: ofstream output; To specify a file, invoke the open function from output object as follows: output.open("numbers.txt"); Optionally, you can create a file output object and open the file in one statement like this: ofstream output("numbers.txt"); To write data, use the stream insertion operator (<<) in the same way that you send data to the cout object. For example, output << 95 << " " << 56 << " " << 34 << endl; SimpleFileOutputRun

© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 42 Simple File Input To read data from a file, first declare a variable of the ifstream type: ifstream input; To specify a file, invoke the open function from input as follows: input.open("numbers.txt"); Optionally, you can create a file output object and open the file in one statement like this: ifstream input("numbers.txt"); To read data, use the stream extraction operator (>>) in the same way that you read data from the cin object. For example, input << score1 << score2 << score3; SimpleFileInputRun