forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompute.php
88 lines (80 loc) · 2.57 KB
/
Compute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
// 四则运算 +-*/()
function expression($str)
{
$str = str_replace(' ','',$str);
$arr = preg_split('/([\+\-\*\/\(\)])/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$numStack = []; // 存放数字
$operStack = []; // 存放运算符
$operStack[] = NULL;
for ($i = 0; $i < count($arr); $i++){
if (ord($arr[$i]) >= 48 && ord($arr[$i] <= 57)){
array_push($numStack, $arr[$i]);
continue;
}
switch ($arr[$i]){
case '+':
case '-':
$arrLen = count($operStack);
while ($operStack[$arrLen-1] === '*' || $operStack[$arrLen-1] === '/' || $operStack[$arrLen-1] === '-'){
compute($numStack, $operStack);
$arrLen--;
}
array_push($operStack, $arr[$i]);
break;
case '*':
$arrLen = count($operStack);
while ($operStack[$arrLen-1] === '/'){
compute($numStack, $operStack);
$arrLen--;
}
array_push($operStack, $arr[$i]);
break;
case '/':
case '(':
array_push($operStack, $arr[$i]);
break;
case ')':
$arrLen = count($operStack);
while ($operStack[$arrLen-1] !== '('){
compute($numStack, $operStack);
$arrLen--;
}
array_pop($operStack);
break;
default:
throw new \Exception("不支持的运算符", 1);
break;
}
}
$arrLen = count($operStack);
while ($operStack[$arrLen-1] !== NULL){
compute($numStack, $operStack);
$arrLen--;
}
echo array_pop($numStack);
}
//数字栈长度减一,运算符栈长度减一
function compute(&$numStack, &$operStack){
$num = array_pop($numStack);
switch (array_pop($operStack)) {
case '*':
array_push($numStack, array_pop($numStack) * $num);
break;
case '/':
array_push($numStack, array_pop($numStack) / $num);
break;
case '+':
array_push($numStack, array_pop($numStack) + $num);
break;
case '-':
array_push($numStack, array_pop($numStack) - $num);
break;
case '(':
throw new \Exception("不匹配的(", 2);
break;
}
}
expression('-1+2-(1+2*3)');
echo PHP_EOL;
eval('echo -1+2-(1+2*3);');