Microsoft/calculator

Support Reverse Polish Notation (RPN)

Open

#128 创建于 2019年3月6日

在 GitHub 查看
 (29 评论) (63 反应) (0 负责人)C++ (2,422 fork)batch import
EnhancementPri: 2help wantedneeds spec

仓库指标

Star
 (16,196 star)
PR 合并指标
 (平均合并 15小时 43分钟) (30 天内合并 7 个 PR)

描述

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

贡献者指南