1.修复括号不匹配。2.除法也要优先于乘法运算

This commit is contained in:
huruidong 2018-10-15 09:37:28 +08:00
parent 80550d66de
commit 996974b4d2
2 changed files with 12 additions and 0 deletions

2
php/.gitignore vendored
View File

@ -1,2 +1,4 @@
.idea
vendor
*my*
Queue

View File

@ -28,6 +28,13 @@ function expression($str)
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]);
@ -70,6 +77,9 @@ function compute(&$numStack, &$operStack){
case '-':
array_push($numStack, array_pop($numStack) - $num);
break;
case '(':
throw new \Exception("不匹配的(", 2);
break;
}
}