1 / 19

Integer Arithmetic

Integer Arithmetic. Operator Priority. Real Number Arithmetic. Mixed-Mode Arithmetic. When the operands are of the same type, the result is also of that type When the operands are of different types, the result is of the more inclusive type. double int char. More inclusive.

Télécharger la présentation

Integer Arithmetic

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. Integer Arithmetic

  2. Operator Priority

  3. Real Number Arithmetic

  4. Mixed-Mode Arithmetic • When the operands are of the same type, the result is also of that type • When the operands are of different types, the result is of the more inclusive type double int char More inclusive

  5. Assignment Statement • Form: <variable> = <expression>; • Example: x = y * z; 3 y Variables are names for memory locations 4 z 7 x

  6. Mixed-Mode Assignment • When the variable is of a more inclusive type, the value copied to it is promoted to that type • When the variable is of a less inclusive type, the value copied to it is truncated and information may be lost

  7. Mixed-Mode Assignment int i; double d; i = 2; d = i; // d contains 2.0

  8. Mixed-Mode Assignment int i; double d; d = 3.56; i = d; // i contains 3

  9. Type Cast Operations int i; double d; d = 3.56; i = d; // i contains 3 i = int(d); // same effect as above Form: <type name>(<expression>)

  10. Compound Assignment Statement

  11. Interactive Input double payRate, hoursWorked, weeklyPay; cout << "Enter the pay rate: "; cin >> payRate; cout << "Enter the hours worked: "; cin >> hoursWorked; weeklyPay = hoursWorked * payRate; cout << "Weekly pay = " << weeklyPay << endl;

  12. The ASCII Character Set 41 = ')'

  13. Using apstring: Input and Output • #include "apstring.h" • apstring name; • cout << "Enter your last name: "; • cin >> name; • cout << "Your last name is " << name << endl; >> reads a word up to a space or newline character

  14. Using apstring: getline • #include "apstring.h" • apstring name; • cout << "Enter your full name: "; • getline(cin, name); • cout << "Your full name is " << name << endl; getline reads an entire line, including blanks, up to a newline character

  15. Using >> Before getline • #include "apstring.h" • apstring name; • int age; • cout << "Enter your age: "; • cin >> age; • cout << "Enter your full name: "; • getline(cin, name); • cout << "Your age is " << age << endl; • cout << "Your full name is " << name << endl; getline encounters the trailing newline after the age, stores an empty string in name, and does not wait for user input

  16. Put >> After getline, or Do This • #include "apstring.h" • apstring name; • int age; • cout << "Enter your age: "; • cin >> age; • cin.ignore(); // consume \n • cout << "Enter your full name: "; • getline(cin, name); • cout << "Your age is " << age << endl; • cout << "Your full name is " << name << endl;

  17. Library Constants

  18. Math Library Functions

  19. apstring Member Functions

More Related