rpn-calculator

command module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

README

RPN Calculator

made-with-Go GitHub go.mod Go version GitHub tag (latest by date) Github Actions GoDoc Languages Top language

Introduction

In reverse Polish notation, the operators follow their operands; for instance, to add 3 and 4 together, one would write 3 4 + rather than 3 + 4. If there are multiple operations, operators are given immediately after their second operands; so the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in reverse Polish notation: 4 is first subtracted from 3, then 5 is added to it. An advantage of reverse Polish notation is that it removes the need for parentheses that are required by infix notation. While 3 − 4 × 5 can also be written 3 − (4 × 5), that means something quite different from (3 − 4) × 5. In reverse Polish notation, the former could be written 3 4 5 × −, which unambiguously means 3 (4 5 ×) − which reduces to 3 20 − (which can further be reduced to -17); the latter could be written 3 4 − 5 × (or 5 3 4 − ×, if keeping similar formatting), which unambiguously means (3 4 −) 5 ×.

Table of contents

Prerequisites

Running, building and testing

This package contains a Makefile which has three commands build, clean and test.

Running the calculator

The calculator can be run in multiple ways, when running it from a produced binary use the provided rpn-calculator binary. For development you can run go run main.go with additional arguments.

Interactive

Run rpn-calculator -i or go run main.go -i to run in interactive shell mode

Command line input

Run rpn-calculator 1 2 "*" 4 \ or go run main.go 1 2 "*" 4 \ to calculate the given input, beware to quote shell arguments like *

Releasing

To create a new release just create a new tag and push this to Github. The Github actions will prepare a new release.

Requirements

  • The calculator has a stack that can contain real numbers.
  • The calculator waits for user input and expects to receive strings containing whitespace separated lists of numbers and operators.
  • Numbers are pushed on to the stack. Operators operate on numbers that are on the stack. • Available operators are +, -, *, /, sqrt, undo, clear.
  • Operators pop their parameters off the stack, and push their results back onto the stack.
  • The clear operator removes all items from the stack.
  • The undo operator undoes the previous operation. undo undo will undo the previo us two operations.
  • sqrt performs a square root on the top item from the stack.
  • The +, -, *, / operators perform addition, subtraction, multiplication and division respectively on the top two items from the stack.
  • After processing an input string, the calculator displays the current contents of the stack as a space-separated list.
  • Numbers should be stored on the stack to at least 15 decimal places of precision, but displayed to 10 decimal places (or less if it causes no loss of precision).
  • All numbers should be formatted as plain decimal strings (ie. no engineering formatting).
  • If an operator cannot find a sufficient number of parameters on the stack, a warning is displayed: operator (position: ): insufficient parameters
  • After displaying the warning, all further processing of the string terminates and the current state of the stack is displayed.

Addendum

  • The interactive console has been adapted with an improved interface allowing to easily calculate with speed.

Documentation

Overview

Copyright © 2021 Unknown <applicant@airwallex.com>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL