1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ChumakovAnton\Calculator; |
6
|
|
|
|
7
|
|
|
use ChumakovAnton\Calculator\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ExpressionCalculator |
11
|
|
|
* @package ChumakovAnton\Calculator |
12
|
|
|
*/ |
13
|
|
|
class ExpressionCalculator implements Calculator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param string $input |
17
|
|
|
* @return float |
18
|
|
|
* @throws InvalidArgumentException |
19
|
|
|
* @throws Exception\DivisionByZeroException |
20
|
|
|
*/ |
21
|
|
|
public function process(string $input = ''): float |
22
|
|
|
{ |
23
|
|
|
self::validateInput($input); |
24
|
|
|
$matches = []; |
25
|
|
|
preg_match_all('/(\-?\d+)([\+\-\*\/])?/', $input, $matches); |
26
|
|
|
$rootExpression = null; |
27
|
|
|
$expressionPoint = null; |
28
|
|
|
foreach ($matches[0] as $key => $value) { |
29
|
|
|
$expression = new ExpressionStackItem(new DefaultOperation($matches[2][$key]), (float)$matches[1][$key]); |
30
|
|
|
|
31
|
|
|
if (null === $rootExpression) { |
32
|
|
|
$rootExpression = $expression; |
33
|
|
|
$expressionPoint = $rootExpression; |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$isHigherPriority = $expression->getOperationPriority() > $expressionPoint->getOperationPriority(); |
38
|
|
|
$isLowerPriority = $expression->getOperationPriority() < $expressionPoint->getOperationPriority(); |
39
|
|
|
|
40
|
|
|
if ($isHigherPriority) { |
41
|
|
|
$expressionPoint = $expressionPoint->appendSubExpression($expression); |
42
|
|
|
} else { |
43
|
|
|
$expressionPoint->appendNextExpression($expression); |
44
|
|
|
|
45
|
|
|
if ($isLowerPriority) { |
46
|
|
|
$expressionPoint = $expressionPoint->getParentExpression(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$expressionPoint->calculate(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
if (null === $rootExpression) { |
53
|
|
|
throw new InvalidArgumentException('Input string has error'); |
54
|
|
|
} |
55
|
|
|
return $rootExpression->calculate(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $input |
60
|
|
|
* @throws InvalidArgumentException |
61
|
|
|
*/ |
62
|
|
|
protected static function validateInput(string $input): void |
63
|
|
|
{ |
64
|
|
|
if (empty($input)) { |
65
|
|
|
throw new InvalidArgumentException('Empty input string'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (preg_match('/([\+\-\*\/]){2,}/', $input) === 1) { |
69
|
|
|
throw new InvalidArgumentException('Double arithmetic operation'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!is_numeric($input[0]) && $input[0] !== '-') { |
73
|
|
|
throw new InvalidArgumentException('First symbol is incorrect'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!is_numeric($input[strlen($input) - 1])) { |
77
|
|
|
throw new InvalidArgumentException('Last symbol is incorrect. Empty right operand'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (preg_match('/([^\d\+\-\*\/])+/', $input) === 1) { |
81
|
|
|
throw new InvalidArgumentException('Input string has incorrect characters'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|