PHP uses inverse Polish method of calculating wages, PHP Poland calculates payroll _php tutorial

Source: Internet
Author: User

PHP uses inverse Polish method of calculating wages, PHP Poland calculates wages


This example describes how PHP uses inverse polish to calculate wages. Share to everyone for your reference. Specific as follows:

The general algorithm for converting a normal middle-order expression to an inverse Polish expression is:

First you need to allocate 2 stacks, a stack S1 as a temporary storage operator (with a closing symbol), a stack S2 (empty stack) as an input inverse polish, the S1 stack can first be placed in the lowest priority operator #, note that infix should end with this operator of the lowest priority. You can specify other characters, not necessarily #. Start with the left end of the drop-down and sequence the following steps:

(1) If the character being taken is an operand, the complete operand is parsed and the operand is fed directly into the S2 stack, and if the operator is removed and the current S1 stack is at the top of (, the current operator is directly into the S1 stack.

(2) If the removed character is an operator, the operator is compared to the top element of the S1 stack, if the operator precedence is greater than the S1 stack top operator priority, then the operator into the S1 stack, no, the S1 stack top operator pops up, into the S2 stack, The operator is fed into the S1 stack until the S1 stack top operator is below (not equal to) the operator priority.

(3) If the character being removed is "(", it is fed directly to the top of the S1 stack.)

(4) If the character that is removed is ")", then the "(" operator from the top of the S1 stack will be sent to the S2 stack, which is then discarded.

(5) Repeat the 1~4 step above until all the input characters are processed

(6) If the character is "#", then all the operators (not including "#") in the S1 stack are pulled out of the stack, and then fed into the S2 stack.

Complete the above steps, the S2 stack will be reversed Polish output results. But S2 should do reverse processing. can be calculated according to the inverse Polish-style calculation!

The math_rpn.php file is as follows:

<?php/** * MATH_RPN * * Implement inverse Polish algorithm * */class MATH_RPN {//initial calculation expression private $_expression = ';  Post-processing inverse polish expression private $_rpnexp = Array ();  An array of simulated stack structures private $_stack = Array (' # ');  Regular judgment//private $_reg = '/^ ([a-za-z0-9\ (\) \+\-\*\/]) *$/';  Priority Private $_priority = Array (' # ' = 0, ' (' = = ', ' + ' = ' + '-'-' + ' = ' * ' = ' + ', ' * ' = ', '/' = 30);  Arithmetic Private $_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[] = arr        Ay_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 Instance $expression = "(A * (b+c)-e+f) *g"; Var_dump ($expression); $mathrpn = new Math_rpn ($expression); Var_dump ($ MATHRPN-&GT;EXP2RPN ());/*end of php*/

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1039186.html www.bkjia.com true http://www.bkjia.com/PHPjc/1039186.html techarticle PHP uses inverse Polish method of calculating wages, PHP Poland Calculation of Wages This example describes how PHP uses inverse polish to calculate wages. Share to everyone for your reference. Specific as follows: ...

  • Related Article

    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.