forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1244bb
commit 7d7933f
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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 '*': | ||
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; | ||
|
||
} | ||
} | ||
expression('-1+2-(1+2*3)'); | ||
echo PHP_EOL; | ||
eval('echo -1+2-(1+2*3);'); |