Informatik I...Reverse polish notation (RPN) calculator (4·2)−(5+1)/2 = 5 2 4 multiply() 2 4 8...

Preview:

Citation preview

Informatik IExercise session 11Autumn 2019

Use case

Reverse polish notation calculator.

A calculator with a stack.

1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Reverse polish notation (RPN) calculator

Implement the stack datastructure for the calculator.

5

Recommended