Microsoft/calculator

Support Reverse Polish Notation (RPN)

Open

#128 opened on 2019年3月6日

GitHub で見る
 (29 comments) (63 reactions) (0 assignees)C++ (2,422 forks)batch import
EnhancementPri: 2help wantedneeds spec

Repository metrics

Stars
 (16,196 stars)
PR merge metrics
 (平均マージ 15h 43m) (30d で 7 merged PRs)

説明

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))

コントリビューターガイド