PHP calculates the post-order expression (inverse Polish)

Source: Internet
Author: User
Baidu's Google search was fruitless and had to build a wheel on its own. PHP *** rpn2value * calculates the inverse Polish formula * @ authorleo108root@leo108.com * functionrpn2value ($ str) {$ arrexplode (, $ str); $ stackarray (); $ lencount ($ arr); for ($ i0; $ I $ len; $ I ++) {if (is_numeric ($ ar

Baidu's Google search was fruitless and had to build a wheel on its own. PHP/*** rpn2value * calculate the inverse Polish formula * @ author leo108 root@leo108.com */function rpn2value ($ str) {$ arr = explode (',', $ str ); $ stack = array (); $ len = count ($ arr); for ($ I = 0; $ I $ len; $ I ++) {if (is_numeric ($ ar

Baidu's Google search was fruitless and had to build a wheel on its own. PHP

/*** Rpn2value * calculate the inverse Polish formula * @ author leo108 root@leo108.com */function rpn2value ($ str) {$ arr = explode (',', $ str ); $ stack = array (); $ len = count ($ arr); for ($ I = 0; $ I <$ len; $ I ++) {if (is_numeric ($ arr [$ I]) {array_push ($ stack, $ arr [$ I]);} else {$ op = $ arr [$ I]; $ right = array_pop ($ stack); $ left = array_pop ($ stack); eval ("\ $ re = $ left $ op $ right;"); array_push ($ stack, $ re) ;}} return $ stack [0];}

Usage:

Reverse Polish Style

$str = "1,2,3,+,*,4,-,5,+,7,*";echo rpn2value($str);

Attached to the forward and backward code, copyright to the original author all http://leo108.com/pid-1901.asp

/* Math_rpn ** implement inverse Polish algorithm ** @ author sparkHuang 260558820@qq.com * @ version RPN 1.0.0 **/class math_rpn {// initial calculation expression private $ _ expression = ''; // The Reverse polish expression after processing is private $ _ rpnexp = array (); // The array that simulates the stack structure is private $ _ stack = array ('#'); // regular identification // private $ _ reg = '/^ ([A-Za-z0-9 \ (\) \ + \-\ * \/]) * $ /'; // priority private $ _ priority = array ('#' => 0, '(' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30); // returns the value of privat. E $ _ operator = array (',' + ','-','*','/',')'); public function _ construct ($ expression) {$ this-> _ init ($ expression);} private function _ init ($ expression) {$ this-> _ expression = $ expression;} public function exp2rpn () {$ len = strlen ($ this-> _ expression); for ($ I = 0; $ I <$ len; $ I ++) {$ char = substr ($ this-> _ expression, $ I, 1); if ($ char = '(') {$ this-> _ stack [] = $ char; continue;} else if (! In_array ($ char, $ this-> _ operator) {$ this-> _ rpnexp [] = $ char; continue;} else if ($ char = ')') {for ($ j = count ($ this-> _ stack); $ j> = 0; $ j --) {$ tmp = array_pop ($ this-> _ stack ); if ($ tmp = "(") {break;} else {$ this-> _ rpnexp [] = $ tmp ;}} continue ;} else if ($ this-> _ priority [$ char] <= $ this-> _ priority [end ($ this-> _ stack)]) {$ this-> _ rpnexp [] = array_pop ($ this-> _ stack); $ this-> _ stack [] = $ char; continue ;} else {$ this-> _ stack [] = $ char; continue ;}for ($ I = count ($ this-> _ stack); $ I >= 0; $ I --) {if (end ($ this-> _ stack) = '#') break; $ this-> _ rpnexp [] = array_pop ($ this-> _ stack);} return $ this-> _ rpnexp ;}} // test the instance $ expression = "(A * (B + C)-E + F) * G"; var_dump ($ expression ); $ mathrpn = new math_rpn ($ expression); var_dump ($ mathrpn-> exp2rpn ());

Ordinal expression

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.