Principle and usage of reverse Polish algorithm in PHP

墨辰丷
Release: 2023-03-30 16:46:01
Original
2495 people have browsed it

This article mainly introduces the principle and usage of PHP reverse Polish algorithm. Interested friends can refer to it. I hope it will be helpful to everyone.

The general algorithm for converting an ordinary inorder expression into a reverse Polish expression is:

First, you need to allocate 2 stacks, one as a temporary storage operator stack S1 (including a End symbol), a reverse Polish stack S2 (empty stack) is used as input. The S1 stack can be put into the operator # with the lowest priority first. Note that the infix expression should end with the operator with the lowest priority. Other characters can be specified, not necessarily #. Take characters from the left end of the infix expression and proceed as follows in sequence:

(1) If the character taken out is an operand, the complete operand will be analyzed and the operand will be sent directly to the S2 stack; if What is taken out is an operator, and the current top of the S1 stack is (, then the current operator is directly put into the S1 stack.

(2) If the character taken out is an operator, then the operator is combined with the top of the S1 stack Element comparison, if the priority of the operator is greater than the priority of the operator on the top of the S1 stack, then the operator will be pushed into the S1 stack. Otherwise, the top operator on the S1 stack will be popped out and sent to the S2 stack until the S1 stack If the operator on the top of the stack has a priority lower than (excluding equal to) the operator's priority, the operator will be sent to the S1 stack.

(3) If the character taken out is "(", it will be sent directly to S1 The top of the stack.

(4) If the character taken out is ")", the operators between the "(" closest to the top of the S1 stack will be popped off the stack one by one and sent to the S2 stack in turn. At this time, discard "(".

(5) Repeat the above steps 1~4 until all input characters

(6) If the character taken out is "#", then Pop all the operators (excluding "#") in the S1 stack one by one and send them to the S2 stack in turn.

After completing the above steps, the S2 stack will output the result in reverse Polish style. However, S2 should do something Process in reverse order. Then you can calculate it according to the reverse Polish calculation method!

math_rpn.php file is as follows:

<?php
/**
 * math_rpn 
 *
 * 实现逆波兰式算法
 *  
 */
class math_rpn {
  //初始的计算表达式
  private $_expression = &#39;&#39;;
  //处理后的逆波兰表达式
  private $_rpnexp = array();
  //模拟栈结构的数组
  private $_stack = array(&#39;#&#39;);
  //正则判断
  //private $_reg  = &#39;/^([A-Za-z0-9\(\)\+\-\*\/])*$/&#39;;
  //优先级
  private $_priority = array(&#39;#&#39; => 0, &#39;(&#39; => 10, &#39;+&#39; => 20, &#39;-&#39; => 20, &#39;*&#39; => 30, &#39;/&#39; => 30);
  //四则运算
  private $_operator = array(&#39;(&#39;, &#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;, &#39;)&#39;);
  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 == &#39;(&#39;) {
        $this->_stack[] = $char;
        continue;
      } else if ( ! in_array($char, $this->_operator)) {
        $this->_rpnexp[] = $char;
        continue;
      } else if ($char == &#39;)&#39;) {
        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) == &#39;#&#39;) break;
      $this->_rpnexp[] = array_pop($this->_stack); 
    }
    return $this->_rpnexp;
  }
}
//测试实例
$expression = "(A*(B+C)-E+F)*G";
var_dump($expression);
$mathrpn = new math_rpn($expression);
var_dump($mathrpn->exp2rpn());
/*End of php*/
Copy after login

Summary: The above is The entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to localize remote images in php

Testing methods and examples of PHP scripts

Summary sharing of PHP processing session functions

The above is the detailed content of Principle and usage of reverse Polish algorithm in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template