calculator7 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/calculator7.rb +19 -5
- 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: 2c3679996d8e9ae4a6d90e3d365f3cc8d134813e
|
4
|
+
data.tar.gz: fedb9322e5b3da48fc084b3c1acbcb71ac250d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 497cb83ae07a9af080536c9ebd594f8d57d5933df71fad0c0964d5e197fd497cc2cf0d90a0df684663f89d53d4798ef6b707bd4d7e0ee6b511f58a436775cb59
|
7
|
+
data.tar.gz: 273fe49badb04fdfa2258ab9e9cae9dc0e1ebb747242a6f17dc8cd5bda999284a2325b6899ef7814dfccc02fd14e6cfc23909e6b3a791a9c62822a4913a7ba15
|
data/lib/calculator7.rb
CHANGED
@@ -3,14 +3,14 @@ class Calculator7
|
|
3
3
|
def self.compute(expression)
|
4
4
|
enable = true
|
5
5
|
expression.chars.each do |c|
|
6
|
-
if !"+-*/0123456789(). ".include?(c)
|
6
|
+
if !"+-*/0123456789(). !".include?(c)
|
7
7
|
enable = false
|
8
8
|
break
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
return nil if !enable
|
13
|
-
expression = expression.chars.
|
13
|
+
expression = expression.chars.reject { |c| c == ' ' }.join('')
|
14
14
|
|
15
15
|
compute_sub(expression)
|
16
16
|
end
|
@@ -21,6 +21,19 @@ class Calculator7
|
|
21
21
|
number = check_number(expression)
|
22
22
|
return number if number != nil
|
23
23
|
|
24
|
+
if expression[-1] == '!'
|
25
|
+
number = check_number expression[0..-2]
|
26
|
+
|
27
|
+
if number != nil
|
28
|
+
ret = 1
|
29
|
+
(1..number).each do |n|
|
30
|
+
ret = ret * n
|
31
|
+
end
|
32
|
+
|
33
|
+
return ret
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
24
37
|
offset = 0
|
25
38
|
value = 0
|
26
39
|
|
@@ -31,7 +44,7 @@ class Calculator7
|
|
31
44
|
|
32
45
|
if offset == 0 and index > 0 and '+-'.include?(c)
|
33
46
|
last_index = index
|
34
|
-
|
47
|
+
end
|
35
48
|
end
|
36
49
|
return nil if offset != 0
|
37
50
|
|
@@ -55,11 +68,12 @@ class Calculator7
|
|
55
68
|
last_index = index
|
56
69
|
end
|
57
70
|
end
|
71
|
+
|
58
72
|
if last_index > 0
|
59
73
|
left = compute_sub(expression[0..last_index-1])
|
60
74
|
right = compute_sub(expression[(last_index+1)..-1])
|
61
75
|
c = expression[last_index]
|
62
|
-
return nil if right.to_f == 0
|
76
|
+
return nil if right.to_f == 0 and c == '/'
|
63
77
|
|
64
78
|
if c == '*'
|
65
79
|
return left * right
|
@@ -80,7 +94,7 @@ class Calculator7
|
|
80
94
|
return nil if count > 0
|
81
95
|
|
82
96
|
count = expression.chars.select { |c| c == '+' or c == '-' }.count
|
83
|
-
return nil if count
|
97
|
+
return nil if count >= 2 or (count == 1 and "0123456789".include?(expression[0]))
|
84
98
|
|
85
99
|
count = expression.chars.select { |c| c == '.' }.count
|
86
100
|
return nil if count > 1
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calculator7
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jianfeng
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple calculator
|
14
14
|
email: tujf.cn@gmail.com
|