Skip to content
Simple Reverse Polish Notation calculator and converter
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
examples
lib
spec
.gitignore
.rspec
.travis.yml
CODE_OF_CONDUCT.md
Gemfile
LICENSE.txt
README.md
Rakefile
rrpn.gemspec

README.md

RRPN

Build Status

RRPN is Reverse Polish Notation calculator and converter, written in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'rrpn'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rrpn

Usage

Integer

rpn = '1 + 2 + 3'.to_rpn
p rpn.queue #=> [1, 2, :+, 3, :+]
p rpn.calc  #=> 6
puts rpn    #=> 1 2 + 3 +

Float

rpn = '(1.0 + 2.0) * 3'.to_rpn
p rpn.queue #=> [1.0, 2.0, :+, 3, :*]
p rpn.calc  #=> 9.0
puts rpn    #=> 1.0 2.0 + 3 *

Rational

rpn = '1/2r + 1/3r'.to_rpn
p rpn.queue #=> [1, "2r", :/, 1, "3r", :/, :+]
p rpn.calc  #=> (5/6)
puts rpn    #=> 1 2r / 1 3r / +

Complex

rpn = '(1+2i) + (3+3i)'.to_rpn
p rpn.queue #=> [1, "2i", :+, 3, "3i", :+, :+]
p rpn.calc  #=> (4+5i)
puts rpn    #=> 1 2i + 3 3i + +

Other

rpn = '(1 << 3) + (16 >> 2)'.to_rpn
p rpn.queue #=> [1, 3, :<<, 16, 2, :>>, :+]
p rpn.calc  #=> 12
puts rpn    #=> 1 3 << 16 2 >> +

Calculation

p RRPN.calc([3, 4, :*]) #=> 12

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/siman-man/rrpn. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.