Arithmetic expression, we are called infix expression in writing, but the calculator, but more like the suffix expression, bracket priority, subtraction priority, etc. so that the operation of infix four expression becomes difficult. This time the introduction of a computer-like format, called the suffix expression. In this paper, we implement the logic of infix expression suffix expression with PHP code.
This article takes PHP as the code environment, some people can say that the high-level language writes the expression to be good, they can calculate, but why they will calculate, how calculates, or needs to convert infix expression to the suffix expression. So this article code just simulates a logic.
For example: the traditional arithmetic expression (infix expression) is 9 + (3-1) * 3 + 10/2, the corresponding suffix expression is 9 3 1-3 * + 10 2/+.
Conversion logic: The input of one character a character, if the number is directly output; If the opening parenthesis is directly into the stack, if the closing parenthesis starts the stack until the first opening parenthesis is encountered, and if it is subtraction, it is judged that if the top of the stack is also a symbol and the input symbol has a priority that is not higher than the top of the stack, All out stack, otherwise the input of the symbol into the stack.
<?php
/**
* Treat the characters entered according to the rules of infix expression suffix expression
* @param $str the characters entered
* @param $stack Stack
* @param $newStrList a new expression
*/
function suffix ($str, & $stack, & $newStrList) {
Output if it is a number
if (Is_numeric ($STR)) {
$newStrList. = $str. ' ';
}
If the opening parenthesis is the stack
else if ($str = = ' (') {
$stack [] = $STR;
}
If it's a closing parenthesis, then all the data in front of the nearest left parenthesis is out of the stack
else if ($str = =) ') {
while ($arrPop = Array_pop ($stack)) {
if ($arrPop = = ' (') {
Break
}
$newStrList. = $arrPop. ' ';
}
}
If it is subtraction, then judge the top of the stack with the symbol priority
else if (In_array ($str, Array (' + ', '-', ' * ', '/') && count ($stack) > 0) {
$key = (count ($stack)-1);
if (In_array ($stack [$key], Array (' + ', '-', ' * ', '/')) {
The symbol priority is not higher than the top of the stack.
if (Checkpriority ($str, $stack [$key])!= 1) {
for ($i = $key; $i >=0; $i-) {
if ($stack [$i] = = ' (') {
Break
}
$newStrList. = $stack [$i]. ' ';
Unset ($stack [$i]);
$stack = Array_values ($stack);
}
}
}
This time the symbol into the stack
$stack [] = $STR;
}else{
$stack [] = $STR;
}
}
/**
* Determine the precedence of operators
* @param $operatorA
* @param $operatorB
* @return A greater than B returns 1,a equals B returns 0,a less than B 1
*/
function checkpriority ($operatorA, $operatorB) {
Switch ($operatorA) {
Case ' + ':
Case '-':
if ($operatorB = = ' + ' | | | $operatorB = = ') {
return 0;
}else if ($operatorB = = ' * ' | | | $operatorB = = '/') {
return-1;
}
Break
Case ' * ':
Case '/':
if ($operatorB = = ' + ' | | | $operatorB = = ') {
return 1;
}else if ($operatorB = = ' * ' | | | $operatorB = = '/') {
return 0;
}
Break
Default
Exit (' Error ');
}
}
Stack
$stack = Array ();
expressions to be converted
$strList = ' 9 + (3-1) * 3 + 10/2 ";
The new expression
$newStrList = ';
$strList = Explode (", $strList);
foreach ($strList as $str) {
if ($str!= ') {
Suffix ($str, $stack, $newStrList);
}
}
Array inversion
while ($s = Array_pop ($stack)) {
$newStrList. = $s. ' ';
}
Echo $newStrList;