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.
- checksums.yaml +4 -4
- data/lib/calculate-bot.rb +22 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a90de87ac5f71eee2614dae47a1c38651e827a83
|
4
|
+
data.tar.gz: 9ea98ef1e965f46a8e8711ce0e5d3f6ac0d588dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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().
|
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.
|
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-
|
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
|