Skip to content

Instantly share code, notes, and snippets.

@Arnauld
Last active July 6, 2018 13:04
Show Gist options
  • Save Arnauld/b558d08fe0eb1a61e47465f673410254 to your computer and use it in GitHub Desktop.
Save Arnauld/b558d08fe0eb1a61e47465f673410254 to your computer and use it in GitHub Desktop.

Context

An RPN calculator computes expressions written in Reverse Polish Notation.

An RPN expression or postfix expression is one of the following :

  • a number X, in wich case the value of the expression is that of X,
  • a sequence of form E1 E2 OP where E1 and E2 are RPN expressions and OP is an arithmetic operation.

Samples :

20 5 /        => 20/5 = 4
4 2 + 3 -     => (4+2)-3 = 3
3 5 8 * 7 + * => 3*((5*8)+7) = 141
7 2 - 3 4     => 5 3 4

Objective

Write a program that take an expression (e.g. "20 5 /") and prints the result (e.g. "4")

The following operators should be supported: +, -, /, *

  1. program can optionally support floating point number
  2. program can optionally support negative number

Options

  • Add the SQRT operator:
    9 SQRT      => √9 = 3
  • Add the SWAP operator:
    1 2 3 SWAP  => 1 3 2
  • Add the DUP operator:
    2 4 DUP     => 2 4 4
  • Add the MAX operator:
    5 3 4 2 9 1 MAX   => MAX(5, 3, 4, 2, 9, 1) = 9
    4 5 MAX 1 2 MAX * => MAX(4,5) * MAX(1,2) = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment