Microsoft/calculator

Support Reverse Polish Notation (RPN)

Open

#128 aperta il 6 mar 2019

Vedi su GitHub
 (29 commenti) (63 reazioni) (0 assegnatari)C++ (2422 fork)batch import
EnhancementPri: 2help wantedneeds spec

Metriche repository

Star
 (16.196 star)
Metriche merge PR
 (Merge medio 15h 43m) (7 PR mergiate in 30 g)

Descrizione

Problem Statement

Reverse Polish Notation / RPN / Postfix mode is an alternate calculator entry mode that is very powerful (leveraging a stack does this!) and should be simple enough to implement.

This behaviour can be seen in physical HP scientific calculators, or the calc mode in Emacs.

image

Evidence or User Insights

It's a simple addition with minimal overhead that would please some potential users that right now need to seek other solutions.

Goals

Users can toggle an RPN mode of entry Low-Fidelity Concept

Simple psuedocode:

#! python3

# Or a function list, or something to check if input is "function" or "input"
functionMap = { 
    "+": lambda x, y: x + y,
    # ....
    "mod": lambda x, y: x % y
}

stack = []
while True:
    userInput = getInput()
    # If it's not a function, add it to the stack
    if userInput not in functionMap.keys():
        stack.append(userInput)
    else:
        # Evaluate the last two items on the stack.
        # In real usage, the last stack item is often implicit, eg, 
        # 27 [enter] 3 / 
        # rather than
        # 27 [enter] 3 [enter] /
        b = stack.pop()
        a = stack.pop()
        stack.append(functionMap[userInput](a, b))

Guida contributor