calculate-bot 0.0.1 → 0.0.2

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 +4 -4
  2. data/lib/calculate-bot.rb +22 -2
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b500b6e351ad7e7ed1594f09173a3e31a5cb8747
4
- data.tar.gz: b7b857382749ab5b3516f2242f02740aa65fa86b
3
+ metadata.gz: a90de87ac5f71eee2614dae47a1c38651e827a83
4
+ data.tar.gz: 9ea98ef1e965f46a8e8711ce0e5d3f6ac0d588dc
5
5
  SHA512:
6
- metadata.gz: 15abd6bca97b51ec0d82b590327984687eb18755e40311ee7648ce798d0b739b889288a875fbde3e96ce64d8c9b9974d91e32b2fc17be5068be54f018a27acf9
7
- data.tar.gz: e35b06b16aa9af409927864ad15464541ffcae1b42f176b4dda1288b3217590bf670fbbd2c0953802b4fbbf1837d213741a854d117c745d7f05c0c8023ebe3af
6
+ metadata.gz: 554298d0928d101330e1c41c798b1dab7b5f23005725c4dbd742bdb86b2ad80fcfc067c2f90cdead14646fc0fd489a0bb33f826ee8156729a9b6b6e0097130a4
7
+ data.tar.gz: d9a12f15c99fd5f9be3caa2d683ccb2031df7bb91a11e6197d683444ce3f9cf103cbea6c510e5af273eb231cab0e6e4d9c38dec0a5685f6a812b047082b5da47
data/lib/calculate-bot.rb CHANGED
@@ -2,15 +2,15 @@ class CalculateBot
2
2
 
3
3
  def self.compute(expression)
4
4
  enable = true
5
+ expression = expression.chars.reject { |c| c == ' ' }.join('')
5
6
  expression.chars.each do |c|
6
- if !"+-*/0123456789(). !".include?(c)
7
+ if !"+-*/0123456789(). !%^cgilnops".include?(c)
7
8
  enable = false
8
9
  break
9
10
  end
10
11
  end
11
12
 
12
13
  return {:status => :no_result} if !enable
13
- expression = expression.chars.reject { |c| c == ' ' }.join('')
14
14
 
15
15
  result = compute_sub(expression)
16
16
  return {:status => :no_result} if result == nil
@@ -93,6 +93,26 @@ class CalculateBot
93
93
  end
94
94
 
95
95
  def self.check_number(expression)
96
+ if expression == 'pi'
97
+ return 3.14159265359
98
+ end
99
+
100
+ index = expression.rindex('^')
101
+ if index != nil
102
+ leftValue = check_number expression[0..(index-1)]
103
+ rightValue = check_number expression[(index+1)..-1]
104
+ if leftValue != nil and rightValue != nil
105
+ ret = 1
106
+ (1..rightValue).each { |c| ret = ret * leftValue }
107
+ return ret
108
+ end
109
+ end
110
+
111
+ if expression.rindex('%') == expression.length - 1
112
+ subValue = check_number(expression[0..-2])
113
+ return subValue * 1.0 / 100
114
+ end
115
+
96
116
  count = expression.chars.select { |c| !"0123456789-+.".include?(c) }.count
97
117
  return nil if count > 0
98
118
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calculate-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jianfeng Tu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-05 00:00:00.000000000 Z
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A calculator lib for computing result from string
14
14
  email: tujf.cn@gmail.com