https://github.com/siman-man/rrpn
Simple Reverse Polish Notation calculator and converter
https://github.com/siman-man/rrpn
ruby
Last synced: about 1 year ago
JSON representation
Simple Reverse Polish Notation calculator and converter
- Host: GitHub
- URL: https://github.com/siman-man/rrpn
- Owner: siman-man
- License: mit
- Created: 2017-02-20T11:56:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T20:14:43.000Z (over 9 years ago)
- Last Synced: 2025-03-14T04:33:35.954Z (over 1 year ago)
- Topics: ruby
- Language: Ruby
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# RRPN
[](https://travis-ci.org/siman-man/rrpn)
RRPN is Reverse Polish Notation calculator and converter, written in Ruby.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'rrpn'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install rrpn
## Usage
Integer
```ruby
rpn = '1 + 2 + 3'.to_rpn
p rpn.queue #=> [1, 2, :+, 3, :+]
p rpn.calc #=> 6
puts rpn #=> 1 2 + 3 +
```
Float
```ruby
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
```ruby
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
```ruby
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
```ruby
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
```ruby
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](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).