rpn-calculator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67b5ea07408d666e0b0b1f0e564b989773f7e095
|
4
|
+
data.tar.gz: 37623c7573e54025ff442b97c7c024b6ea09a9e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e84986323f02c63a8dfa0cac0a144ff0ddb4916a83489a8f91088d2e0dbf24bd3d857749f1d8338bb9e971526f6c8b351b4f36851b855cee9c02a08b6ec43547
|
7
|
+
data.tar.gz: eacb1e523c7b9d4d98c4468ae8982ba251f19409c0a9256859be731ede0a108fba549c3b22e44cfea3cf36eb2edb0643993827ab6032e9c293b1099220721d08
|
@@ -9,6 +9,7 @@ module RPNCalculator
|
|
9
9
|
def start
|
10
10
|
while (input = io_interface.read_input)
|
11
11
|
processor_result = operation_processor.process(input_stack, input)
|
12
|
+
|
12
13
|
if processor_result.valid?
|
13
14
|
print_result_array(processor_result.result)
|
14
15
|
@input_stack = processor_result.result
|
@@ -1,8 +1,12 @@
|
|
1
1
|
module RPNCalculator
|
2
2
|
module Operation
|
3
3
|
class Base
|
4
|
+
def self.operate(operands)
|
5
|
+
new(operands).result
|
6
|
+
end
|
7
|
+
|
4
8
|
def initialize(operands)
|
5
|
-
@operands = operands
|
9
|
+
@operands = operands.compact
|
6
10
|
end
|
7
11
|
|
8
12
|
def result
|
@@ -24,6 +28,10 @@ module RPNCalculator
|
|
24
28
|
def invalid_operation_result(operation)
|
25
29
|
Result::Operation.new(operation, [], operands)
|
26
30
|
end
|
31
|
+
|
32
|
+
def operation_string
|
33
|
+
raise 'must implement in subclass'
|
34
|
+
end
|
27
35
|
end
|
28
36
|
end
|
29
37
|
end
|
@@ -7,12 +7,14 @@ module RPNCalculator
|
|
7
7
|
@operation_symbols = @operation_classes.keys
|
8
8
|
end
|
9
9
|
|
10
|
+
# Refator needed, process should receive parsed input
|
10
11
|
def process(previous_operations, input)
|
11
12
|
validator_result = input_validator.validate(input)
|
12
13
|
return Result::Processor.new([], validator_result.error) unless validator_result.valid?
|
13
14
|
|
14
15
|
parser_result = input_parser.parse(input)
|
15
16
|
return Result::Processor.new([], parser_result.error) unless parser_result.valid?
|
17
|
+
|
16
18
|
process_operations(previous_operations + parser_result.parsed_elements)
|
17
19
|
end
|
18
20
|
|
@@ -21,19 +23,18 @@ module RPNCalculator
|
|
21
23
|
attr_reader :input_validator, :input_parser, :operation_classes, :operation_symbols
|
22
24
|
|
23
25
|
def process_operations(operations)
|
24
|
-
|
25
|
-
while operations.size > 0
|
26
|
-
element = operations.shift
|
26
|
+
result_stack = operations.inject([]) do |stack, element|
|
27
27
|
if operation_symbols.include?(element)
|
28
|
-
result = operation_classes.fetch(element)
|
28
|
+
result = operation_classes.fetch(element)
|
29
|
+
.operate([stack.pop, stack.pop].reverse)
|
29
30
|
return invalid_processor_result(result) unless result.valid?
|
30
|
-
|
31
|
-
operation_stack = [result.result]
|
31
|
+
stack.push(result.result)
|
32
32
|
else
|
33
|
-
|
33
|
+
stack.push(element)
|
34
34
|
end
|
35
|
+
stack
|
35
36
|
end
|
36
|
-
Result::Processor.new(
|
37
|
+
Result::Processor.new(result_stack)
|
37
38
|
end
|
38
39
|
|
39
40
|
def invalid_processor_result(result)
|