informatik i...reverse polish notation (rpn) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8...

18
Informatik I Exercise session 11 Autumn 2019

Upload: others

Post on 30-Oct-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Informatik IExercise session 11Autumn 2019

Page 2: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Use case

Reverse polish notation calculator.

A calculator with a stack.

1

Page 3: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

Supported operationspush(value) push a value on top of the stackpop() pop and display value on top of stackadd(), subtract(), multiply(), divide() basic arithmeticbinary operations: pop values v2 and v1 from stack andpush v2 op v1

2

Page 4: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 5: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24

8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 6: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 7: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 8: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 9: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 10: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 11: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 12: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 13: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 14: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38

5

pop()

3

Page 15: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 16: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

24

multiply()24 8

push(5)58

push(1) 158

add() 158

68

push(2) 268

divide() 268

38

substract()38 5

pop()

3

Page 17: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

(4 · 2)− (5 + 1)/2 = 5

4 2 * 5 1 + 2 / - =Commands:

value→ push(value)=→ pop()+, -, *, /→ add(), subtract(), multiply(), divide()

4

Page 18: Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8 push(5) 5 8 push(1) 1 5 8 add() 1 5 8 6 8 push(2) 2 6 8 divide() 2 6 8 3 8 substract()

Reverse polish notation (RPN) calculator

Implement the stack datastructure for the calculator.

5