Skip to content

Commit 996974b

Browse files
committed
1.修复括号不匹配。2.除法也要优先于乘法运算
1 parent 80550d6 commit 996974b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

php/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
22
vendor
3+
*my*
4+
Queue

php/Stack/Compute.php renamed to php/08_stack/Compute.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ function expression($str)
2828
array_push($operStack, $arr[$i]);
2929
break;
3030
case '*':
31+
$arrLen = count($operStack);
32+
while ($operStack[$arrLen-1] === '/'){
33+
compute($numStack, $operStack);
34+
$arrLen--;
35+
}
36+
array_push($operStack, $arr[$i]);
37+
break;
3138
case '/':
3239
case '(':
3340
array_push($operStack, $arr[$i]);
@@ -70,6 +77,9 @@ function compute(&$numStack, &$operStack){
7077
case '-':
7178
array_push($numStack, array_pop($numStack) - $num);
7279
break;
80+
case '(':
81+
throw new \Exception("不匹配的(", 2);
82+
break;
7383

7484
}
7585
}

0 commit comments

Comments
 (0)