calculate-bot 0.0.1
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 +7 -0
- data/lib/calculate-bot.rb +112 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b500b6e351ad7e7ed1594f09173a3e31a5cb8747
|
4
|
+
data.tar.gz: b7b857382749ab5b3516f2242f02740aa65fa86b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 15abd6bca97b51ec0d82b590327984687eb18755e40311ee7648ce798d0b739b889288a875fbde3e96ce64d8c9b9974d91e32b2fc17be5068be54f018a27acf9
|
7
|
+
data.tar.gz: e35b06b16aa9af409927864ad15464541ffcae1b42f176b4dda1288b3217590bf670fbbd2c0953802b4fbbf1837d213741a854d117c745d7f05c0c8023ebe3af
|
@@ -0,0 +1,112 @@
|
|
1
|
+
class CalculateBot
|
2
|
+
|
3
|
+
def self.compute(expression)
|
4
|
+
enable = true
|
5
|
+
expression.chars.each do |c|
|
6
|
+
if !"+-*/0123456789(). !".include?(c)
|
7
|
+
enable = false
|
8
|
+
break
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
return {:status => :no_result} if !enable
|
13
|
+
expression = expression.chars.reject { |c| c == ' ' }.join('')
|
14
|
+
|
15
|
+
result = compute_sub(expression)
|
16
|
+
return {:status => :no_result} if result == nil
|
17
|
+
|
18
|
+
{:status => :normal, :result => result.to_s }
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.compute_sub(expression)
|
22
|
+
return compute_sub(expression[1..-2]) if expression[0] == '(' and expression[-1] == ')'
|
23
|
+
|
24
|
+
number = check_number(expression)
|
25
|
+
return number if number != nil
|
26
|
+
|
27
|
+
if expression[-1] == '!'
|
28
|
+
number = check_number expression[0..-2]
|
29
|
+
|
30
|
+
if number != nil
|
31
|
+
ret = 1
|
32
|
+
(1..number).each do |n|
|
33
|
+
ret = ret * n
|
34
|
+
end
|
35
|
+
|
36
|
+
return ret
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
offset = 0
|
41
|
+
value = 0
|
42
|
+
|
43
|
+
last_index = -1
|
44
|
+
expression.chars.each_with_index do |c, index|
|
45
|
+
offset = offset + 1 if c == '('
|
46
|
+
offset = offset - 1 if c == ')'
|
47
|
+
|
48
|
+
if offset == 0 and index > 0 and '+-'.include?(c)
|
49
|
+
last_index = index
|
50
|
+
end
|
51
|
+
end
|
52
|
+
return nil if offset != 0
|
53
|
+
|
54
|
+
if last_index > 0
|
55
|
+
left = compute_sub(expression[0..last_index-1])
|
56
|
+
right = compute_sub(expression[(last_index+1)..-1])
|
57
|
+
c = expression[last_index]
|
58
|
+
|
59
|
+
if c == '+'
|
60
|
+
return left + right
|
61
|
+
elsif c == '-'
|
62
|
+
return left - right
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
expression.chars.each_with_index do |c, index|
|
67
|
+
offset = offset + 1 if c == '('
|
68
|
+
offset = offset - 1 if c == ')'
|
69
|
+
|
70
|
+
if offset == 0 and index > 0 and '*/'.include?(c)
|
71
|
+
last_index = index
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
if last_index > 0
|
76
|
+
left = compute_sub(expression[0..last_index-1])
|
77
|
+
right = compute_sub(expression[(last_index+1)..-1])
|
78
|
+
c = expression[last_index]
|
79
|
+
return nil if right.to_f == 0 and c == '/'
|
80
|
+
|
81
|
+
if c == '*'
|
82
|
+
return left * right
|
83
|
+
elsif c == '/'
|
84
|
+
if (left / right).to_f == (left.to_f / right.to_f)
|
85
|
+
return left / right
|
86
|
+
else
|
87
|
+
return left.to_f / right.to_f
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
nil
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.check_number(expression)
|
96
|
+
count = expression.chars.select { |c| !"0123456789-+.".include?(c) }.count
|
97
|
+
return nil if count > 0
|
98
|
+
|
99
|
+
count = expression.chars.select { |c| c == '+' or c == '-' }.count
|
100
|
+
return nil if count >= 2 or (count == 1 and "0123456789".include?(expression[0]))
|
101
|
+
|
102
|
+
count = expression.chars.select { |c| c == '.' }.count
|
103
|
+
return nil if count > 1
|
104
|
+
|
105
|
+
if count == 1
|
106
|
+
expression.to_f
|
107
|
+
else
|
108
|
+
expression.to_i
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: calculate-bot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jianfeng Tu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A calculator lib for computing result from string
|
14
|
+
email: tujf.cn@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/calculate-bot.rb
|
20
|
+
homepage: http://rubygems.org/gems/calculate-bot
|
21
|
+
licenses:
|
22
|
+
- MIT
|
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: 1.9.3
|
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.1.11
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: calculator-bot
|
44
|
+
test_files: []
|