Php-PHP source code

Source: Internet
Author: User
Php implements simple expression algorithm Php implements simple expression algorithm

Returns an expression.

$ Char = array (); // symbol stack $ number = array (); // number stack $ charnum = array ('+' => 1, '-' => 1, '*' => 2, '/' => 2 ); // specify the priority of the symbol stack $ arr = '4 + 2*5-7/11 '; // 4 2 5 * + 7 11/-inverse Polish expression $ arr = preg_replace ('/(\ s +)/', ', $ arr ); $ len = strlen ($ arr); $ flag = 0; $ j = 0; for ($ I = 0; $ I <$ len; $ I ++) {if (is_numeric ($ arr [$ I]) {if (! Isset ($ number [$ j]) $ number [$ j] = 0; if ($ flag = 1) $ number [$ j] = $ number [$ j] * 10 + $ arr [$ I]; if ($ flag! = 1) {$ number [$ j] = (int) $ arr [$ I] ;}$ flag = 1 ;}if (! Is_numeric ($ arr [$ I]) & is_string ($ arr [$ I]) {$ char [] = $ arr [$ I]; $ j ++; $ flag = 2 ;}} function do_operation ($ data1, $ data2, $ symbol) {switch ($ symbol) {case '+': return (double) ($ data1 + $ data2); case '-': return (double) ($ data1-$ data2); case '*': return (double) ($ data1 * $ data2); case '/': return (double) ($ data1/$ data2) ;}} function change_number (& $ arr, $ pos, $ temp) {$ I = $ pos; $ arr [$ I] = $ temp; $ I ++; $ len = count ($ arr)-1; while ($ I <$ len) {$ arr [$ I] = $ arr [$ I + 1]; $ I ++ ;} unset ($ arr [$ I]);} function change_char (& $ char, $ pos) {$ I = $ pos; $ len = count ($ char)-1; while ($ I <$ len) {$ char [$ I] = $ char [$ I + 1]; $ I ++ ;} unset ($ char [$ I]);} function char_value ($ char, $ charnum) {if (array_key_exists ($ char, $ charnum )) return $ charnum [$ char] ;}$ x1 =x2 x2 = 0; // Two pointers $ result = 0; $ lennum = count ($ number ); $ lenchar = count ($ char); while ($ x1 <$ lennum & $ x2 <$ lenchar) {// It is mainly calculated by symbols. Therefore, the priority of a symbol is very important. if (isset ($ char [$ x2]) & isset ($ char [$ x2 + 1]) & char_value ($ char [$ x2], $ charnum)> = char_value ($ char [$ x2 + 1], $ charnum) {if ($ result = 0) {$ result = do_operation ($ number [$ x1], $ number [$ x1 + 1], $ char [$ x2]); $ x1 ++; $ x2 ++;} else {$ result = do_operation ($ result, $ number [$ x1], $ char [$ x2]); $ x1 ++; $ x2 ++ }}// when only one symbol is left, else if ($ x2 = ($ lenchar-1) {$ result = do_operation ($ result, $ number [$ x1 + 1], $ char [$ x2]); break ;} else {// The symbol stack needs to remove a symbol and then move one digit to the other to change one digit to the other. $ temp = do_operation ($ number [$ x1 + 1], $ number [$ x1 + 2], $ char [$ x2 + 1]); change_number ($ number, $ x1 + 1, $ temp); change_char ($ char, $ x2 + 1); $ lennum = count ($ number); $ lenchar = count ($ char); var_dump ($ number); var_dump ($ char ); // if ($ x2 = 1) {echo $ lenchar; echo $ x1; exit ;}} echo $ result;


The above is the content for php to implement simple expression calculation algorithms. For more information, see PHP Chinese website (www.php1.cn )!

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.