1recitation

# 1recitation - If Is_Operator(expr(I = true then...

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

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

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

View Full Document
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
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))

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

View Full Document
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) > 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

## This note was uploaded on 01/28/2012 for the course AERO 16.01 taught by Professor Markdrela during the Fall '05 term at MIT.

### 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
Ask a homework question - tutors are online