From 996974b4d2b53ad715e82174320131df2273c2e3 Mon Sep 17 00:00:00 2001 From: huruidong <1390095912@qq.com> Date: Mon, 15 Oct 2018 09:37:28 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E6=8B=AC=E5=8F=B7=E4=B8=8D?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E3=80=822.=E9=99=A4=E6=B3=95=E4=B9=9F?= =?UTF-8?q?=E8=A6=81=E4=BC=98=E5=85=88=E4=BA=8E=E4=B9=98=E6=B3=95=E8=BF=90?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php/.gitignore | 2 ++ php/{Stack => 08_stack}/Compute.php | 10 ++++++++++ 2 files changed, 12 insertions(+) rename php/{Stack => 08_stack}/Compute.php (85%) diff --git a/php/.gitignore b/php/.gitignore index 3ce5adb..9426df2 100644 --- a/php/.gitignore +++ b/php/.gitignore @@ -1,2 +1,4 @@ .idea vendor +*my* +Queue \ No newline at end of file diff --git a/php/Stack/Compute.php b/php/08_stack/Compute.php similarity index 85% rename from php/Stack/Compute.php rename to php/08_stack/Compute.php index 3e5624b..7b10507 100644 --- a/php/Stack/Compute.php +++ b/php/08_stack/Compute.php @@ -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; } }