200 likes | 218 Views
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
E N D
COMPUTER 2430Object Oriented Programming andData 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 • Prefix + A B • Postfix (RPN) A B +
Binary Arithmetic Operations • Infix • Prefix and Postfix (RPN) Don’t need parentheses (No commutative law) (No associative law) (No distributed law)
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)
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
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
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!
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!
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!
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!
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!
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!
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!