Postfix ExpressionsFunction CallsThe Triangle PuzzleAssignmentStackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentStack ApplicationsLecture 25Section 7.4 - 7.5Robb T. KoetherHampden-Sydney CollegeMon, Mar 23, 2009StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentOutline1Postfix Expressions2Function Calls3The Triangle Puzzle4AssignmentStackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentStack ApplicationsAn infix expression is an arithmetic expression in whichthe binary operators are written in between theoperands.For example, to add 3 and 4, we write3 + 4.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix ExpressionsA postfix expression with one (binary) operator iswritten in the order:left-operand, right-operand, operator.For example, to add 3 and 4, we write3 4 + .StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPrefix ExpressionsA prefix expression with one binary operator is writtenin the order:operator, left-operand, right-operand.For example, to add 3 and 4, we write + 3 4.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Expression: 3 4 + 5 6 + ∗.Left operand of ∗ is 3 4 +.Right operand of ∗ is 5 6 +.In postfix expressions, parentheses are never needed!StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 +StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4 5 6 +StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4 5 6 + 7 8 ∗StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4 5 6 + 7 8 ∗ −StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4 5 6 + 7 8 ∗ − /StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4 5 6 + 7 8 ∗ − / −StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Expressions)Rewrite the infix expression((1 + 2) ∗ 3 − 4/(5 + 6 − 7 ∗ 8)) ∗ 9as a postfix expression.1 2 + 3 ∗ 4 5 6 + 7 8 ∗ − / − 9 ∗StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationTo evaluate a postfix expression:Begin with an empty stack.Each number or operator is a token.Process the tokens in the postfix expression from left toright.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationFor each token,If the token is a number,Push it onto the stack.If the token is a binary operator,Pop two numbers off the stack.Combine them under the operator.Push the result onto the stack.The single remaining value on the stack is the value ofthe expression.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Evaluation)Evaluate 1 2 + 3 ∗ 4 5 6 + 7 8 ∗ − / − 9 ∗.Read 1: Push 1.Read 2: Push 2.Read +: Pop 2; Pop 1; Compute 1 + 2 = 3; Push 3.Read 3: Push 3.Read ∗: Pop 3; Pop 3; Compute 3 ∗ 3 = 9; Push 9.Read 4: Push 4.Read 5: Push 5.Read 6: Push 6.Read +: Pop 6; Pop 5; Compute 5 + 6 = 11; Push 11.Read 7: Push 7.Read 8: Push 8.Read ∗: Pop 8; Pop 7; Compute 7 ∗ 8 = 56; Push 56.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Evaluation)Evaluate 1 2 + 3 ∗ 4 5 6 + 7 8 ∗ − / − 9 ∗.Read 1: Push 1.Read 2: Push 2.Read +: Pop 2; Pop 1; Compute 1 + 2 = 3; Push 3.Read 3: Push 3.Read ∗: Pop 3; Pop 3; Compute 3 ∗ 3 = 9; Push 9.Read 4: Push 4.Read 5: Push 5.Read 6: Push 6.Read +: Pop 6; Pop 5; Compute 5 + 6 = 11; Push 11.Read 7: Push 7.Read 8: Push 8.Read ∗: Pop 8; Pop 7; Compute 7 ∗ 8 = 56; Push 56.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe TrianglePuzzleAssignmentPostfix Expression EvaluationExample (Postfix Evaluation)Evaluate 1 2 + 3 ∗ 4 5 6 + 7 8 ∗ − / − 9 ∗.Read 1: Push 1.Read 2: Push 2.Read +: Pop 2; Pop 1; Compute 1 + 2 = 3; Push 3.Read 3: Push 3.Read ∗: Pop 3; Pop 3; Compute 3 ∗ 3 = 9; Push 9.Read 4: Push 4.Read 5: Push 5.Read 6: Push 6.Read +: Pop 6; Pop 5; Compute 5 + 6 = 11; Push 11.Read 7: Push 7.Read 8: Push 8.Read ∗: Pop 8; Pop 7; Compute 7 ∗ 8 = 56; Push 56.StackApplicationsRobb T.KoetherPostfixExpressionsFunction CallsThe
View Full Document