Analytic algorithm of calculation

Source: Internet
Author: User
These days nothing is written in PHP with a parser program.

For example: (21*12-23-(21+14-(21-31/3+ (14-21*12-14)) +54)-21) +21*25-22*26

Then the result of the analysis is:-169.33.

The code is now posted, if there is a bad place also ask the programmer friends to point out.

if (Isset ($_get[' Sizhi ')) {$sizhi =$_get[' Sizhi '];    if (!checksizhi ($sizhi)) {echo (' The formula is not valid, check if the parentheses are paired ');     }else{$sizhi =priority ($sizhi);     Echo (Calculate ($sizhi));    }}//Check whether the calculation is legal function Checksizhi ($sizhi) {$Amatch =array (); $pattern = '/((\d|!) \()| ([-\+\*\/]\))|    [^-\+\*\/!\d\ (\)]/';      if (Preg_match ($pattern, $sizhi, $Amatch) >0) {echo (' illegal element: ');      Print_r ($Amatch);    return false;      }else{$kuohao = 0;         For ($i =0, $k =strlen ($sizhi), $i < $k, $i + +) {if ($sizhi [$i]== "(") {$kuohao + +);               } if ($sizhi [$i]== ")") {$kuohao-;         if ($kuohao <0) return false;      }} if ($kuohao!=0) return false;    return true; }}//gets the priority to handle the contents of the parentheses in the function priorities ($sizhi, $start =0) {for ($i = $start; $i
 
  0) {echo ("factorial phase problem, please change the formula");  Exit ();    }else{//Replaces part of all factorial $sizhi =preg_replace_callback ($par, function ($siz) {return calculatefactorial ($siz [0]);  }, $sizhi); //Exchange symbols and numbers and calculate return change ($sizhi);} Calculates the factorial function calculatefactorial ($sizhi) {for ($i =0; $i
  
   0) {$str = $array [0];    } return $STR; }}
  
 

The above describes the calculation of the analytic algorithm, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.