Arithmetic expression calculation Implementation Code _php tutorial implemented in PHP

Source: Internet
Author: User
PHP Implementation:
Copy CodeThe code is as follows:
/**
* Calculate arithmetic expression
*/
Error_reporting (E_all);
$exp = ' (1+2* (3+5)/4) * (5-4) *;
$arr _exp = Array ();
for ($i =0; $i
$arr _exp[] = $exp [$i];
}
$result = Calcexp (Array_reverse ($arr _exp));
Echo $exp. '=' . $result;
function Calcexp ($exp) {
$arr _n = Array ();
$arr _op = Array ();
while ($s = Array_pop ($exp))! = ") {
if ($s = = ' (') {
$temp = Array (); $quote = 1; $endquote = 0;
while ($t = Array_pop ($exp))! = ") {
if ($t = = ' (') {
$quote + +;
}
if ($t = = ') ') {
$endquote + +;
if ($quote = = $endquote) {
Break
}
}
Array_push ($temp, $t);
}
$temp = Array_reverse ($temp);
Array_push ($arr _n, Calcexp ($temp));
}else if ($s = = ' * ' | | $s = = '/') {
$n 2 = Array_pop ($EXP);
if ($n 2 = = ' (') {
$temp = Array (); $quote = 1; $endquote = 0;
while ($t = Array_pop ($exp))! = ") {
if ($t = = ' (') {
$quote + +;
}
if ($t = = ') ') {
$endquote + +;
if ($quote = = $endquote)
Break
}
Array_push ($temp, $t);
}
$temp = Array_reverse ($temp);
$n 2 = calcexp ($temp);
}
$op = $s;
$n 1 = array_pop ($arr _n);
$result = operation ($n 1, $op, $n 2);
Array_push ($arr _n, $result);
}elseif ($s = = ' + ' | | $s = = '-') {
Array_push ($arr _op, $s);
}else{
Array_push ($arr _n, $s);
}
}
$n 2 = Array_pop ($arr _n);
while ($op = Array_pop ($arr _op))! = ") {
$n 1 = array_pop ($arr _n);
$n 2 = operation ($n 1, $op, $n 2);
}
return $n 2;
}
function operation ($n 1, $op, $n 2) {
Switch ($OP) {
Case ' + ':
Return Intval ($n 1) + intval ($n 2);
Break
Case '-':
Return Intval ($n 1)-Intval ($n 2);
Break
Case ' * ':
Return Intval ($n 1) * Intval ($n 2);
Break
Case '/':
Return Intval ($n 1)/Intval ($n 2);
Break
}
}

This implementation uses two stacks, one to store the numbers, one to store the operators, and the parentheses to recursively enter parentheses, the implementation is a bit clumsy, and later add the "Inverse Polish expression" algorithm implementation.

http://www.bkjia.com/PHPjc/324056.html www.bkjia.com true http://www.bkjia.com/PHPjc/324056.html techarticle PHP Implementation: Copy code as follows: PHP/** * Calculates arithmetic expression */error_reporting (e_all); $exp = ' (1+2* (3+5)/4) * (5-4) '; $arr _exp = array (); for ($i =0; $istrlen ($ ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.