Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.

50
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    1

Transcript of Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.

Stacks & QueuesInfix Calculator

CSC 172

SPRING 2002

LECTURE 5

Workshop sign-up

Still time :Dave Feil-Seifer [email protected]

Ross Carmara [email protected]

Infix to postfix

1 + 2 * 3

== 7 (because multiplication has higher precidence)

10 – 4 – 3

== 3 (because subtraction proceeds left to right)

Infix to postfix

4 ^ 3 ^ 2== 262144!= 4096

234

234

Generally,

Rather than:

Precidence

A few simple rules:

() > ^ > * / > + -

Subtraction associates left-to-right

Exponentiation associates right to left

Infix Evaluation

1 – 2 – 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2

== -8

(1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) )

Could you write a program to evaluate stuff like this?

Postfix

If we expressed

(1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) )

As

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

Then, we could use the postfix stack evaluator

Postfix evaluation using a stack

1. Make an empty stack

2. Read tokens until EOFa. If operand push onto stack

b. If operator i. Pop two stack values

ii. Perform binary operation

iii. Push result

3. At EOF, pop final result

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

21

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

4-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

5 4-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

1024-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

31024

-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

3072-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

63072

-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

18432-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

718432

-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

27

18432-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

227

18432-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

47

18432-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

204118432

-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

7-1

1 2 – 4 5 ^ 3 * 6 * 7 2 2 ^ ^ / -

-8

But how to go from infix to postfix? Could you write a program to do it? What data structures would you use

Stack Queue

How about a simple case just using “+” 1+ 2 + 7 + 4 1 2 7 4 + + +

Operands send on to output? Operator push on stack? Pop ‘em all at the end?

More complex

2 ^ 5 – 1 == 2 5 ^ 1 –

Modify the simple rule?

If you are an operator, pop first, then push yourself?

1 + 2 + 7 + 4

1 2 + 7 + 4 + ok

Even more complex

3 * 2 ^ 5 - 1

3 2 5 ^ * 1 –

If you are an operator:

Pop if the top of the stack is higher precedence than

Infix to postfix Stack AlgorithmOperands : Immediately output

Close parenthesis: Pop stack until open parenthesis

Operators: 1. Pop all stack symbols until a symbol of lower

precedence (or a right-associative symbol of equal precedence) appears.

2. Push operator

EOF: pop all remaining stack symbols

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7

1

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7

-

1

1 – 2 ^ 3 ^ 3 – ( 4 + 5 * 6) * 7

-

1 2

1 – 2 ^ 3 ^ 3 – ( 4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