PHP uses inverse polish to calculate wages _php Tips

Source: Internet
Author: User
Tags php programming

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

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

First you need to allocate 2 stacks, a stack S1 (with a closing symbol) as a temporary storage operator, a stack S2 (empty stack) as an input inverse polish, and the S1 stack can first be placed at the lowest precedence operator #, noting that infix should end with this lowest precedence operator. You can specify a different character, not necessarily a #. Starts the character from the left end of the prefix, sequentially following the steps:

(1) If the character is the operand, then the complete operation number is analyzed, the operand is directly fed into the S2 stack; If the operator is removed and the current S1 stack is (, the current operator is directly into the S1 stack.)

(2) If the character being removed is an operator, the operator is compared to the top element of the S1 stack, and if the operator precedence is greater than the S1 stack top operator precedence, the operator is fed into the S1 stack, or the top operator of the S1 stack is ejected into the S2 stack, The operator is fed to the S1 stack until the S1 stack top operator is below (excluding equals) of the operator precedence.

(3) If the character is taken out is "(", then directly into the S1 stack stack top.

(4) If the character is ""), will be away from the top of the S1 stack of the nearest "(" operator, one by one out of the stack, in turn into the S2 stack, at this time abandoned "(".

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

(6) If the character is "#", then the S1 stack all operators (excluding "#"), one by one stack, in turn, into the S2 stack.

Complete the above steps, the S2 stack will be reverse Polish output results. But S2 should do reverse processing. can be calculated in accordance with the reverse Polish calculation Method!

math_rpn.php files are as follows:

<?php/** * MATH_RPN * * * To implement the reverse Polish algorithm * */class MATH_RPN {//initial computational expression private $_expression = ';
  Handle the inverse Polish expression private $_rpnexp = Array ();
  Array of analog stack structures private $_stack = Array (' # ');
  Regular judgement//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;
    The 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[] = AR
        Ray_pop ($this->_stack);
        $this->_stack[] = $char;
      Continue
        else {$this->_stack[] = $char;
      Continue
      for ($i = count ($this->_stack), $i >= 0; $i-) {if ($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 will help you with your PHP programming.

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.