1 / 20

COMPUTER 2430 Object Oriented Programming and Data Structures I

COMPUTER 2430 Object Oriented Programming and Data Structures I. Binary Arithmetic Operations. Addition (plus): + Subtraction (minus): - Multiplication (times): * Division: / Remainder: % operand1 operator operand2 12 + 5 3 * (7 – 4 * 5). Binary Arithmetic Operations. Infix A + B

willer
Download Presentation

COMPUTER 2430 Object Oriented Programming and Data Structures I

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. COMPUTER 2430Object Oriented Programming andData Structures I

  2. Binary Arithmetic Operations • Addition (plus): + • Subtraction (minus): - • Multiplication (times): * • Division: / • Remainder: % operand1 operator operand2 12 + 5 3 * (7 – 4 * 5)

  3. Binary Arithmetic Operations • Infix A + B • Prefix + A B • Postfix (RPN) A B +

  4. Binary Arithmetic Operations • Infix • Prefix and Postfix (RPN) Don’t need parentheses (No commutative law) (No associative law) (No distributed law)

  5. Examples Infix (A + B) * C A + (B * C) Prefix * + A B C + A * B C Postfix (RPN) A B + C * A B C * + What are the operands (A, B and C)? Integers Double Complex Numbers FixedPoint Numbers (Prog3) TVectors (Prog4)

  6. Evaluating Prefix Expression * + 5 3 2 ------------ * 8 2 16 • / 12 4 * 5 + 1 3 ------------ ------------- - 3 * 5 4 ---------------- - 3 20 -17 From left to right Perform the operation when an operator is followed by two operands

  7. Evaluating Postfix Expression 5 3 + 2 * ------------ 8 2 * 16 • 4 / 5 1 3 + * - ------------ ------------ 3 5 4 * - ------------------------ 3 20 - -17 From left to right Perform the operation when an operator follows two operands

  8. Converting Prefix Expressions to Infix * + 5 3 2 ------------ * (5 + 3) 2 (5 + 3) * 2 • / 12 4 * 5 + 1 3 ------------ ------------- - (12 / 4) * 5 (1 + 3) -------------------- - (12 / 4) (5 * (1 + 3) ) (12 / 4) – (5 * (1 + 3) ) From left to right, add parentheses when an operator is followed by two operands and move the operator (a pair of parentheses is an operand) Original operands remain in the same order!

  9. Converting Prefix Expressions to Infix * A + B C ------------ * A (B + C) A * (B + C) • / A B * + C D E ------------ ------------- - (A / B) * (C + D) E ----------------------- - (A / B) ( (C + D) * E) (A / B) – ( (C + D) * E) From left to right, add parentheses when an operator is followed by two operands and move the operator (a pair of parentheses is an operand) Original operands remain in the same order!

  10. Converting Infix Expressions to Prefix A * (B + C) * A (B + C) * A + B C A * (B + C) A * (+ B C) * A (+ B C) * A + B C Move operator to the front and remove parentheses when it’s clear Original operands remain in the same order!

  11. Converting Infix Expressions to Prefix (A / B) - ( (C + D) * E) - (A / B) ( (C + D) * E) - (/ A B) ( * (C + D) E) - (/ A B) ( * (+ C D) E) - (/ A B) ( * + C D E) - / A B * + C D E (A / B) - ( (C + D) * E) (/ A B) - ( (+ C D) * E) (/ A B) - ( * (+ C D) E) (/ A B) - ( * + C D E) - (/ A B) ( * + C D E) - / A B * + C D E Move operator to the front and remove parentheses when it’s clear Original operands remain in the same order!

  12. Converting Postfix Expressions to Infix A B C + * ------------ A (B + C) * A * (B + C) A B / C D + E * - ----------- ------------- (A / B) (C + D) E * - --------------------- (A / B) ( (C + D) * E) - (A / B) - ( (C + D) * E) Add parentheses when two operands followed by an operator and move the operator Original operands remain in the same order!

  13. Converting Infix Expressions to Postfix A * (B + C) A (B + C) * A (B C +) * A B C + * A * (B + C) A * (B C +) A (B C +) * A B C + * Move operator to the end and remove parentheses when it’s clear Original operands remain in the same order!

  14. Converting Infix Expressions to Postfix (A / B) - ( (C + D) * E) (A / B) ( (C + D) * E) - (A B /) ( (C + D) E *) - (A B /) ( (C D +) E *) - (A B /) ( C D + E *) - A B / C D + E * - (A / B) - ( (C + D) * E) (A B /) - ( (C D +) * E) (A B /) - ( (C D +) E *) (A B /) - ( C D + E *) (A B /) ( C D + E *) - A B / C D + E * - Move operator to the front and remove parentheses when it’s clear Original operands remain in the same order!

  15. Prog 2 and Lab 4

  16. Lab 5 and Lab 6

  17. Quiz 3

  18. Exercise

More Related