PHP Inverse Polish expression algorithm-dedicated _php tutorial for payroll calculation

Source: Internet
Author: User
A netizen wrote to me about the calculation of salary in PHP. I've talked about a method of calculating wages in a previous article, but Shang, using the tools of existing expressions, now that someone wants to, I'm going to give you an inverse Polish algorithm.

Our goal is to achieve the following calculation formula:

Suppose you have a formula that reads:

$expression = "(f1*f12+10.34)";

The values of the variables are as follows:

$expression _value = Array (' F1 ' =>10, ' F12 ' =>20);

We want to build a class with PHP to calculate the value of this expression. This application is primarily used in Web payroll management, where users can customize their payroll-phase formulas.

$RPN = new Math_rpn ();
$RPN->setexpressionvalue ($expression _value);
echo $rpn->calculate ($expression, ' deg ', false); That is the corresponding value

The method of parsing the inverse Polish expression, the compiler principle is that the expression is decomposed into a symbol array, then the inverse Polish-style, and finally according to the inverse polish to get its results.

I put three functions in the following, in fact, I am the pear rpn function of the hack.

function _stringtoarray () {
$temp _operator = null;
$temp _value = null;

$this->_input = Str_replace ("", "", $this->_input);

for ($i = 0; $i < strlen ($this->_input); $i + +) {
if ($this->_input[$i] = = ") {
if ($temp _operator! = null) {
Array_push ($this->_input_array, $temp _operator);
$temp _operator = null;
}
if ($temp _value! = null) {
Array_push ($this->_input_array, $temp _value);
$temp _value = null;
}
} elseif (($temp _value = = null) && $temp _operator! = ') ' &&

(!array_key_exists ($temp _operator, $this->_operation) | |

!array_key_exists (2, $this->_operation[$temp _operator]) | |

$this->_operation[$temp _operator][2]>0) && ($this->_input[$i] = = '-')) {
if ($temp _operator! = null) {
Array_push ($this->_input_array, $temp _operator);
$temp _operator = null;
}

Array_push ($this->_input_array, '-1 ');
Array_push ($this->_input_array, ' * ');
} ElseIf ((Is_numeric ($this->_input[$i)) | | ($this->_input[$i] = = '. ')) {
} ElseIf ((Is_numeric ($this->_input[$i)) | | ($this->_input[$i] = = '. ') ||

($this->_input[$i] = = ' F ')) {
if ($temp _operator! = null) {
Array_push ($this->_input_array, $temp _operator);
$temp _operator = null;
}

$temp _value. = $this->_input[$i];
} else {
if ($this->_keyexists ($temp _operator, $this->_operation, 1)) {
Array_push ($this->_input_array, $temp _operator);
$temp _operator = null;
}

if ($temp _value! = null) {
Array_push ($this->_input_array, $temp _value);
$temp _value = null;
}

$temp _operator. = $this->_input[$i];
}
}

if ($temp _operator! = null && $temp _operator! = ") {
Array_push ($this->_input_array, $temp _operator);
} elseif ($temp _value! = null && $temp _value! = ") {
Array_push ($this->_input_array, $temp _value);
}

$this->_testinput ();
Print_r ($this->_expression_value);
Print_r ($this->_input_array);
return $this->_input_array;
}

function _arraytorpn () {

if ($this->_error <> null) {
$this->_output = Array ();
return $this->_output;
}

for ($i = 0; $i < count ($this->_input_array); $i + +) {

$temp = $this->_input_array[$i];

if (Is_numeric ($temp)) {
$this->_outputadd ($temp);
} else if ($this->_keyexists ($temp, $this->_expression_value, 0)) {
$this->_outputadd ($this->_expression_value[$temp]);
} else {
if ($temp = = ') ') {
while (! $this->_stackempty () && ($this->_stackpriority () >= 1)) {
$this->_outputadd ($this->_stackdelete ());
}
if (! $this->_stackempty ()) {
$this->_stackdelete ();
}

} elseif ($temp = = ' (') {
$this->_stackadd ($temp);
} elseif (($this->_stackempty ()) | | (($this->_priority ($temp) >

$this->_stackpriority ()))) {
$this-_stackadd ($temp);
} else {
while (! $this->_stackempty () && ($this->_priority ($temp)

<= $this->_stackpriority ())) {
$this->_outputadd ($this->_stackdelete ());
}
$this->_stackadd ($temp);
}

}

}

while (! $this->_stackempty ()) {
$this->_outputadd ($this->_stackdelete ());
}

return $this->_output;
}

function _rpntovalue () {

$time 1 = $this->_getmicrotime ();

if ($this->_error <> null) {
$this->_value = null;
return $this->_value;
}

$this->_value = 0;
$temp = $this->_output;

do {
$pos = $this->_nextoperator ($temp);

if ($pos = =-1) {
$this->_error = $this->_raiseerror (' Syntax error ');
$this->_value = null;
return $this->_value;
}

$operator = $this->_operation[$temp [$pos]];
$arg = $operator [2];
$function = $operator [3];

if ($arg ==2) && (!isset ($temp [$pos-1]) | |!is_numeric ($temp [$pos-1]) | |

!isset ($temp [$pos-2]) | | !is_numeric ($temp [$pos-2]))) {
$this->_error = $this->_raiseerror (' Syntax error ');
$this->_value = null;
return $this->_value;
} elseif (($arg ==1) && (!isset ($temp [$pos-1]) | |!is_numeric ($temp [$pos-1])) {
$this->_error = $this->_raiseerror (' Syntax error ');
$this->_value = null;
return $this->_value;
}

if (Is_array ($function)) {

if ($arg ==2) $arg _array = Array ($temp [$pos-2], $temp [$pos-1]);
ElseIf ($arg ==1) $arg _array = Array ($temp [$pos-1]);
else $arg _array = Array ();

if ($function [' type '] = = ' Userfunction ') {
$this->_value = Call_user_func_array ($function [' function '], $arg _array);
} else {
$function _array = Array (& $function [' class '], $function [' method ']);
$this->_value = Call_user_func_array ($function _array, $arg _array);
}
} else {
$this->_value = $this $function ($temp, $pos);
}

if ($this->_isnan ($this->_value)) {
$this->_error = $this->_raiseerror (' NAN value ');
$this->_value = null;
return $this->_value;
} elseif ($this->_isinfinite ($this->_value)) {
$this->_error = $this->_raiseerror (' Infinite value ');
$this->_value = null;
return $this->_value;
} elseif (Is_null ($this->_value)) {
return $this->_value;
}

$temp = $this->_refresh ($temp, $pos, $arg, $this->_value);
} while (count ($temp) > 1);

$this->_value = $temp [0];

$time 2 = $this->_getmicrotime ();

$this->_timer = $time 2-$time 1;

return $this->_value;
}


http://www.bkjia.com/PHPjc/446823.html www.bkjia.com true http://www.bkjia.com/PHPjc/446823.html techarticle A Netizen wrote to me about the calculation of salary in PHP. I've talked about a method of calculating wages in a previous article, but Shang, using the tools of existing expressions, now ...

  • 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.