) Number ) Operator ) Number (7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3) OperatorOperands Stacks...

Preview:

Citation preview

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+ 0

Init: Push + and 0 onto the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

00

Lparen: Push + and 0 onto the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

+

0

0

Number: Pop + and 0 from the stacks,

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

+

0

0

Number: Pop + and 0 from the stacks, add the 7

7

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+70

Number: Pop + and 0 from the stacks, add the 7 and stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

70

Operator: Push the + onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+++

070

LParen: Push a + and 0 onto the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

++

0

70

Number: Pop a + and 0 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

++

0

70

Number: Pop a + and 0 from the stacks add the 3

3

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

370

Number: Pop a + and 0 from the stacks add the 3, stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

370

Operator: Push a * onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+*++

0370

LParen: Push a + and 0 onto the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

0

370

Number: Pop the + and 0 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

0

370

Number: Pop the + and 0 from the stacks add the 4

4

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

4370

Number: Pop the + and 0 from the stacks add the 4, stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+*++

4370

Operator: Push the + onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

4

370

Number: Pop the + and 4 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

4

370

Number: Pop the + and 4 from the stacks, add the read 4

4

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

8370

Number: Pop the + and 4 from the stacks, add the read 4, stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

++

8

70

RParen: Pop the * and 8 and 3 from the stacks,

3

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

70

RParen: Pop the * and 8 and 3 from the stacks, multiply the two

24

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

2470

RParen: Pop the * and 8 and 3 from the stacks, multiply the two, push the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

2470

Operator: Push the * onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

++

24

70

Number: Pop the * and 24 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

++

24

70

Number: Pop the * and 24 from the stacks, multiply by the read 2

2

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

4870

Number: Pop the * and 24 from the stacks, multiply by the read 2, stack the 48

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

4870

Operator: Push the * onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

++

48

70

Number: Pop the * and 48 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

++

48

70

Number: Pop the * and 48 from the stacks, multiply the 2 and 48

2

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

9670

Number: Pop the * and 48 from the stacks, multiply the 2 and 48, stack the 96

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

9670

Operator: Push the * onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+*++

09670

LParen: Push a + and 0 onto the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

0

9670

Number: Pop the + and 0 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

0

9670

Number: Pop the + and 0 from the stacks, add the 1

1

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

19670

Number: Pop the + and 0 from the stacks, add the 1, stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+*++

19670

Operator: Push the + onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

1

9670

Number: Pop the + and 1from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

*++

1

9670

Number: Pop the + and 1from the stacks, add the 2 and 1

2

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*++

39670

Number: Pop the + and 1from the stacks, add the 2 and 1, stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

++

3

70

RParen: Pop the * and 3 and 96 from stacks,

96

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

++

28870

RParen: Pop the * and 3 and 96 from stacks, multiply 96 and 3 and stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+

+

288

0

RParen: Pop the + and 7 and 288 from stacks,

7

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+2950

RParen: Pop the + and 7 and 288 from stacks, add 7 and 288 and stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*+

2950

Operator: Push the * onto the stack

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

+

295

0

Number: Pop the * and 295 from the stacks

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

*

+

295

0

Number: Pop the * and 295 from the stacks, multiply the read 3 and 295

3

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+8850

Number: Pop the * and 295 from the stacks, multiply the read 3 and 295, stack it

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

+ 885

RParen: Pop the + and 0 and 885 from stacks

0

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

885

RParen: Pop the + and 0 and 885 from stacks, add the 0 and 885 and stack the result

)Number

)

OperatorOperator

)

Number

(7 + (3 * (4 + 4) * 2 * 2 * (1 + 2) ) * 3)

Operator Operands

Stacks

Using Two Stacks to Evaluate a Simple Arithmetic Expression

( StartOperator

885

Done: Result is in Operands stack: 885

Recommended