RpnCalculator 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/RpnCalculator.rb +29 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 51071e26e805a18a91fcfaeb11cb7e06e2ea17d7
4
+ data.tar.gz: b90c5850d48ac6148e4141d67d233c557535d1ea
5
+ SHA512:
6
+ metadata.gz: 2fd9a78310036b622472ac2d18e513f9d5451ce2f5dfa0bec063c20e1f320ba8d6c7e863cb36812544be67424e3d7ca4f26f55c990fb37fa0c66a2ec532f5d95
7
+ data.tar.gz: f55f0fdb714f20ef7183da2bbb2441e5f5f71dc62ce7dd9524509dc46f5c91dc141775e6de886c7bfd8a5ed3e725c2780ba231998fea161506aa6241bfeb7828
@@ -0,0 +1,29 @@
1
+ class RpnCalculator
2
+
3
+ #evaluate gets User Inputted Reverse Ploish Notation (Post Fix) Expression - and gets split
4
+
5
+ def self.evaluate(expression)
6
+ expression = expression.split
7
+ operands = []
8
+ evaluation = []
9
+
10
+ # Each Split item from PostFix Expression then gets evaluated in two cases
11
+ #(1) When its a Digit (Regex /\d/) gets pushed onto evaluation array
12
+ #(2) When it encounters any of the listed operands it pops two form the stack and acts with that operand and pushes result back to stack
13
+
14
+ expression.each do |x|
15
+ case x
16
+ when /\d/
17
+ evaluation.push(x.to_f)
18
+ when "-", "/", "*", "+", "**"
19
+ operands = evaluation.pop(2)
20
+ evaluation.push(operands[0].send(x, operands[1]))
21
+ end
22
+ end
23
+ return evaluation
24
+
25
+ end
26
+
27
+ end
28
+
29
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RpnCalculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - grahambarry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Will Evaluate an Expression in Reverse Polish Notation and Return the
14
+ answer (Addition, Subtraction, Division, Multiplication
15
+ email: grahambarry@live.ie
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/RpnCalculator.rb
21
+ homepage: ''
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Reverse Polish Notation (Postfix) Calculator
44
+ test_files: []