1 / 129

Chapter 3. Expressions and Interactivity

Chapter 3. Expressions and Interactivity. 3.1 The cin Object. The cin object reads information types at the keyboard. cin is the standard input object Notice the >> and << operators appear to point in the direction information is flowing. Program 3-1. #include <iostream.h>

gomer
Download Presentation

Chapter 3. Expressions and Interactivity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3. Expressions and Interactivity

  2. 3.1 The cin Object • The cin object reads information types at the keyboard. • cin is the standard input object • Notice the >> and << operators appear to point in the direction information is flowing.

  3. Program 3-1 #include <iostream.h> void main(void) { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"What is the length of the rectangle? "; cin>>length; cout <<"What is the width of the rectangle? "; cin>>width; area = length * width; cout <<"The area of the rectangle is " << area << ".\n"; }

  4. Program Output This program calculates the area of a rectangle. What is the length of the rectangle? 10 [Enter] What is the width of the rectangle? 20 [Enter] The area of the rectangle is 200.

  5. Program 3-2 // This program reads the length and width of a rectangle. // It calculates the rectangle's area and displays // the value on the screen. #include <iostream.h> void main(void) { int length, width, area; cin >> length; cin >> width; area = length * width; cout << "The area of the rectangle is " << area << endl; }

  6. Entering Multiple Values • The cin object may be used to gather multiple values at once.

  7. Program 3-3 #include <iostream.h> void main(void) { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"Enter the length and width of the rectangle separated by a space. \n"; cin >> length>> width; area = length * width; cout <<"The area of the rectangle is " << area << endl; }

  8. Program Output This program calculates the area of a rectangle. Enter the length and width of the rectangle separated by a space. 10 20 [Enter] The area of the rectangle is 200

  9. Program 3-4 // This program demonstrates how cin can read multiple values // of different data types. #include <iostream.h> void main(void) { int whole; float fractional; char letter; cout << "Enter an integer, a float, and a character: "; cin >> whole >> fractional >> letter; cout << "whole: " << whole << endl; cout << "fractional: " << fractional << endl; cout << "letter: " << letter << endl; }

  10. Program Output Enter an integer, a float, and a character: 4 5.7 b [Enter] whole: 4 fractional: 5.7 letter: b

  11. Reading Strings • cin can read strings as well as numbers. • Strings are stored in character arrays. char Company[12];

  12. Program 3-5 #include <iostream.h> #include<string> void main(void) { string name; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; }

  13. Program Output What is your name? Charlie [Enter] Good morning Charlie

  14. Program 3-6 // This program demonstrates how cin can read a // string into a character array #include <iostream.h> void main(void) { char name[21] ; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; }

  15. Program Output What is your name? Charlie [Enter] Good morning Charlie

  16. Program 3-7 // This program reads two strings into two character arrays. #include <iostream.h> void main(void) { char first[16], last[16]; cout << "Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; }

  17. Program Output Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny

  18. Notes on strings: • If a character array is intended to hold strings, it must be at least one character larger than the largest string that will be stored in it. • The cin object will let the user enter a string larger than the array can hold. If this happens, the string will overflow the array’s boundaries and destroy other information in memory. • If you wish the user to enter a string that has spaces in it, you cannot use this input method.

  19. 3.2 Focus on Software Engineering: Mathematical Expressions • C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.

  20. Program 3-7 // This program reads two strings into two character arrays #include <iostream.h> void main(void) { char first[16], last[16]; cout << Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; }

  21. Program 3-7 Output with Example Input Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny

  22. Program 3-8 // This program asks the user to enter the numerator // and denominator of a fraction and it displays the // decimal value #include <iostream.h> void main(void) { float numerator, denominator; cout << "This program shows the decimal value of "; cout << "a fraction.\n"; cout << “Enter the numerator: “; cin >> numerator; cout << “Enter the denominator: “; cin >> denominator; cout << “The decimal value is “; cout << (numerator / denominator); }

  23. Program Output for Program 3-8 with Example Input This program shows the decimal value of a fraction. Enter the numerator: 3 [Enter] Enter the denominator: 6 [Enter] The decimal value is 0.1875

  24. Table 3-1 Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) -* / %+ -

  25. Table 3-2 Some Expressions

  26. Associativity • If two operators sharing an operand have the same precedence, they work according to their associativity, either right to left or left to right

  27. Converting Algebraic Expressions to Programming Statements

  28. No Exponents Please! • C++ does not have an exponent operator. • Use the pow() library function to raise a number to a power. • Will need #include <math.h> for pow() function. area = pow(4,2) // will store 42 in area

  29. Program 3-9 // This program calculates the area of a circle. // The formula for the area of a circle is Pi times // the radius squared. Pi is 3.14159. #include <iostream.h> #include <math.h> // needed for the pow function void main(void) { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = 3.14159 * pow(radius,2); cout << "The area is " << area; }

  30. Program 3-9 Output With Example Input This program calculates the area of a circle. What is the radius of the circle? 10[Enter] The area is 314.159

  31. 3.3 When you Mix Apples and Oranges: Type Coercion • When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.

  32. Type Coercion Rules: • Rule 1: Chars, shorts, and unsigned shorts are automatically promoted to int. • Rule 2: When an operator works with two values of different data types, the lower-ranking value is promoted to the type of the higher-ranking value. • Rule 3: When the final value of an expression is assigned to a variable, it will be converted to the data type of the variable.

  33. 3.4 Overflow and Underflow • When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows. • Overflow - when a variable is assigned a number that is too large for its data type • Underflow - when a variable is assigned a number that is too small for its data type

  34. Program 3-10 // This program demonstrates integer underflow and // overflow #include <iostream.h> void main(void) { short testVar = 32767; cout << testVar << endl; testVar = testVar + 1; cout << testVar << endl; testVar = testVar - 1; cout << testVar << endl; }

  35. Program Output 32767 -32768 32767

  36. Program 3-11 // This program can be used to see how your system // handles floating point overflow and underflow. #include <iostream.h> void main(void) { float test; test = 2.0e38 * 1000; // Should overflow test cout << test << endl; test = 2.0e-38 / 2.0e38; cout << test << endl; }

  37. 3.5 The Typecast Operator • The typecast operator allows you to perform manual data type conversion. Val = int(number); //If number is a floating //point variable, it will be //truncated to an integer and //stored in the variable Val

  38. Program 3-12 #include <iostream.h> void main(void) { int months, books; float perMonth; cout << "How many books do you plan to read? "; cin >> books; cout << "How many months will it take you to read them? "; cin >> months; perMonth = float(books) / months; cout << "That is " << perMonth << " books per month.\n"; }

  39. Program Output How many books do you plan to read? 30 [Enter] How many months will it take you to read them? 7 [Enter] That is 4.285714 books per month.

  40. Typecast Warnings • In Program 3-11, the following statement would still have resulted in integer division: perMonth = float(books / months); • Because the division is performed first and the result is cast to a float.

  41. Program 3-13 // This program uses a typecast operator to print // a character from a number. #include <iostream.h> void main(void) { int number = 65; cout << number << endl; cout << char(number) << endl; }

  42. Program Output 65 A

  43. 3.6 The Power of Constants • Constants may be given names that symbolically represent them in a program.

  44. Program 3-14 // This program calculates the area of a circle. #include <iostream.h> #include <math.h> void main(void) { const float pi = 3.14159; double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = pi * pow(radius,2); cout << "The area is " << area; }

  45. The #define Directive • The older C-style method of creating named constants is with the #define directive, although it is preferable to use the const modifier. #define PI 3.14159 • is roughly the same as const float PI=3.14159;

  46. Program 3-15 #include <iostream.h> #include <math.h> // needed for pow function #define PI 3.14159 void main(void) { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * pow(radius, 2); cout << "The area is " << area; }

  47. 3.7 Multiple Assignment and Combined Assignment • Multiple assignment means to assign the same value to several variables with one statement. A = B = C = D = 12; Store1 = Store2 = Store3 = BegInv;

  48. Program 3-16 // The program tracks the inventory of three widget stores // that opened at the same time. Each store started with the // same number of widgets in inventory. By subtracting the // number of widgets each store has sold from its inventory, // the current inventory can be calculated. #include <iostream.h> void main(void) { int begInv, sold, store1, store2, store3; cout << “One week ago, 3 new widget stores opened\n"; cout << “at the same time with the same beginning\n"; cout << “inventory. What was the beginning inventory? "; cin >> begInv; store1 = store2 = store3 = begInv;

  49. Program 3-16 Continued cout << "How many widgets has store 1 sold? "; cin >> sold; store1 = store1 – sold; //Subtract sold from store1 cout << "How many widgets has store 2 sold? "; cin >> sold; store2 = store2 – sold; //Subtract sold from store2 cout << "How many widgets has store 3 sold? "; cin >> sold; store3 = store3 – sold; //Subtract sold from store 3 cout << "Store1: " << store1 << endl; cout << "Store2: " << store2 << endl; cout << "Store3: " << store3 << endl; }

More Related