calculator7 0.0.2 → 0.0.3
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/calculator7.rb +80 -13
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f59e3a0f339618c747adb971291fc0e1b9b9eda
|
4
|
+
data.tar.gz: 729569bbf4a649108746428a8a218fca14cac235
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8383742eabff7d1d4d74d9cc8281878d92d8a6535e9efe8985e7e15df43728bc5c5780f08581e3695e9acb422c28b94aa5709f8b24b650575521e7ce4e76344b
|
7
|
+
data.tar.gz: 2f074299180a8ff35b81105b6a65afe170dccd78a09c9b0f5a70108d2caaef5f638bbd4a1f3c6772607cff81bff93225640b4ec9bb9ce9ad9cac2fe2e6e828e1
|
data/lib/calculator7.rb
CHANGED
@@ -1,28 +1,95 @@
|
|
1
1
|
class Calculator7
|
2
|
-
def self.hi
|
3
|
-
puts "Hello world!"
|
4
|
-
end
|
5
2
|
|
6
|
-
def self.compute(
|
3
|
+
def self.compute(expression)
|
7
4
|
enable = true
|
8
|
-
|
9
|
-
if !"+-*/0123456789 ".include?(c)
|
5
|
+
expression.chars.each do |c|
|
6
|
+
if !"+-*/0123456789(). ".include?(c)
|
10
7
|
enable = false
|
11
8
|
break
|
12
9
|
end
|
13
10
|
end
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
return nil if !enable
|
13
|
+
expression = expression.chars.select { |c| "+-*/0123456789().".include?(c) }.join('')
|
14
|
+
|
15
|
+
compute_sub(expression)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.compute_sub(expression)
|
19
|
+
return compute_sub(expression[1..-2]) if expression[0] == '(' and expression[-1] == ')'
|
20
|
+
|
21
|
+
number = check_number(expression)
|
22
|
+
return number if number != nil
|
23
|
+
|
24
|
+
offset = 0
|
25
|
+
value = 0
|
26
|
+
|
27
|
+
last_index = -1
|
28
|
+
expression.chars.each_with_index do |c, index|
|
29
|
+
offset = offset + 1 if c == '('
|
30
|
+
offset = offset - 1 if c == ')'
|
31
|
+
|
32
|
+
if offset == 0 and index > 0 and '+-'.include?(c)
|
33
|
+
last_index = index
|
34
|
+
end
|
35
|
+
end
|
36
|
+
return nil if offset != 0
|
37
|
+
|
38
|
+
if last_index > 0
|
39
|
+
left = compute_sub(expression[0..last_index-1])
|
40
|
+
right = compute_sub(expression[(last_index+1)..-1])
|
41
|
+
c = expression[last_index]
|
42
|
+
|
43
|
+
if c == '+'
|
44
|
+
return left + right
|
45
|
+
elsif c == '-'
|
46
|
+
return left - right
|
21
47
|
end
|
22
48
|
end
|
23
49
|
|
24
|
-
|
50
|
+
expression.chars.each_with_index do |c, index|
|
51
|
+
offset = offset + 1 if c == '('
|
52
|
+
offset = offset - 1 if c == ')'
|
53
|
+
|
54
|
+
if offset == 0 and index > 0 and '*/'.include?(c)
|
55
|
+
last_index = index
|
56
|
+
end
|
57
|
+
end
|
58
|
+
if last_index > 0
|
59
|
+
left = compute_sub(expression[0..last_index-1])
|
60
|
+
right = compute_sub(expression[(last_index+1)..-1])
|
61
|
+
c = expression[last_index]
|
62
|
+
return nil if right.to_f == 0
|
63
|
+
|
64
|
+
if c == '*'
|
65
|
+
return left * right
|
66
|
+
elsif c == '/'
|
67
|
+
if (left / right).to_f == (left.to_f / right.to_f)
|
68
|
+
return left / right
|
69
|
+
else
|
70
|
+
return left.to_f / right.to_f
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
nil
|
25
76
|
end
|
26
77
|
|
78
|
+
def self.check_number(expression)
|
79
|
+
count = expression.chars.select { |c| !"0123456789-+.".include?(c) }.count
|
80
|
+
return nil if count > 0
|
81
|
+
|
82
|
+
count = expression.chars.select { |c| c == '+' or c == '-' }.count
|
83
|
+
return nil if count == 2 or (count == 1 and "0123456789".include?(expression[0]))
|
84
|
+
|
85
|
+
count = expression.chars.select { |c| c == '.' }.count
|
86
|
+
return nil if count > 1
|
87
|
+
|
88
|
+
if count == 1
|
89
|
+
expression.to_f
|
90
|
+
else
|
91
|
+
expression.to_i
|
92
|
+
end
|
93
|
+
end
|
27
94
|
|
28
95
|
end
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jianfeng
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple calculator
|
14
14
|
email: tujf.cn@gmail.com
|
@@ -29,7 +29,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
30
30
|
- - '>='
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
32
|
+
version: 1.9.3
|
33
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
35
|
- - '>='
|