1recitation

1recitation - If Is_Operator(expr(I)) = true then

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Introduction to Computers and Programming Prof. I. K. Lundqvist Recitation 1 Mar 18 2004 2 constraint error Arrays 1 2 3 4 5 6 7 8 9 10 lb ub type My_Array is array ( 1 .. 10 ) of Integer; lb ub • Static structures of fixed size – lb: lower bound – ub : upper bound – index<lb | index>ub
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 Palindrome same both forward and backward x y z a z y x lb ub 4 Evaluating Postfix Read from left to right: the stack (the first number popped is the second binary operand) push the result back onto the stack on top of stack is the answer 5 3 + 10 * 5 3 + 4 + 1 + • A palindrome is a string that reads the • Two questions: – How do you check equality? – When do you stop? 1. if a number is read, push it on the stack 2. if an operator is read, pop two numbers off 3. apply the operation to the numbers, and 4. when the expression is complete, the number
Background image of page 2
5 Infix to Postfix post_fix := “” Create(Op_Stack) for I in 1 . . Length loop If Is_Operand(expr(I)) = true then Append(post_fix, expr(I))
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: If Is_Operator(expr(I)) = true then Process_Next_Operator(expr(I)) end loop 6 Process_Next_Operator Done := False loop If Is_Empty(Op_Stack) or next_op is (, set Done to True Elsif precedence(next_op) &gt; precedence(top_operator) Push next_op onto Op_stack --ensures higher precedence operators evaluated first Set Done to True Else Pop the operator_stack If operator popped is ( set Done to True Else Done = True -- string post_fix has the result push next_op onto Op_Stack append operator popped to post_fix string exit when end loop 7 Infix to Postfix: Example 3 5 6 * + 7 8 5 + * 8 Unary Operators binary and unary operations and unary operators? Infix Expression 3 + 5 * 6 7 * (8 + 5) Postfix Expression + and - are symbols used for both How do you distinguish between binary 9 Infix to Postfix: Example 3 5 6 Infix Expression 3 + 5 * 6 7 * (8 + 5) Postfix Expression * + 7 8 5 + *...
View Full Document

Page1 / 5

1recitation - If Is_Operator(expr(I)) = true then

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online