rpn.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,003 行 · 第 1/2 页
PHP
1,003 行
* @access private
*/
function _outputAdd($value) {
if ($value<>'(') {
array_push($this->_output, $value);
}
}
/**
* Change input array into RPN array
*
* @return array Array with RPN expression
* @access private
*/
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 ($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;
}
/**
* Return position of the first operator in array
*
* @param array $array Temporary array
* @return integer Position of the first operator
* @access private
*/
function _nextOperator($array) {
$pos = 0;
while(is_numeric($array[$pos])) {
$pos++;
if ($pos >= count($array)) {
return -1;
}
}
return $pos;
}
/**
* Delete from array operator [posision $pos] and its argument and insert new value
*
* @param array $temp Temporary array
* @param integer $pos Position of the last operator
* @param integer $arg Number of last operator's arguments
* @param float $result Last operation result
* @return array New temporary array
* @access private
*/
function _refresh($temp, $pos, $arg, $result) {
$temp1 = array_slice($temp, 0, $pos-$arg);
$temp1[] = $result;
$temp2 = array_slice($temp, $pos+1);
return array_merge($temp1, $temp2);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _sum($temp, $pos) {
return $temp[$pos-2]+$temp[$pos-1];
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _difference($temp, $pos) {
return $temp[$pos-2]-$temp[$pos-1];
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _multiplication($temp, $pos) {
return $temp[$pos-2]*$temp[$pos-1];
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _division($temp, $pos) {
if ($temp[$pos-1]==0) {
$this->_error = $this->_raiseError('Division by 0');
$this->_value = null;
return $this->_value;
}
return $temp[$pos-2]/$temp[$pos-1];
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _root($temp, $pos) {
return pow($temp[$pos-1], (1/$temp[$pos-2]));
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _power($temp, $pos) {
return pow($temp[$pos-2], $temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _sin($temp, $pos) {
if ($this->_angle) {
$angle = $temp[$pos-1];
} else {
$angle = deg2rad($temp[$pos-1]);
}
return sin($angle);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _cos($temp, $pos) {
if ($this->_angle) {
$angle = $temp[$pos-1];
} else {
$angle = deg2rad($temp[$pos-1]);
}
return cos($angle);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _tan($temp, $pos) {
if ($this->_angle) {
$angle = $temp[$pos-1];
} else {
$angle = deg2rad($temp[$pos-1]);
}
return tan($angle);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _asin($temp, $pos) {
$angle = asin($temp[$pos-1]);
if (!$this->_angle) {
$angle = rad2deg($angle);
}
return $angle;
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _acos($temp, $pos) {
$angle = acos($temp[$pos-1]);
if (!$this->_angle) {
$angle = rad2deg($angle);
}
return $angle;
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _atan($temp, $pos) {
$angle = atan($temp[$pos-1]);
if (!$this->_angle) {
$angle = rad2deg($angle);
}
return $angle;
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _sqrt($temp, $pos) {
return sqrt($temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _exp($temp, $pos) {
return exp($temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _log($temp, $pos) {
return log10($temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _ln($temp, $pos) {
return log($temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _const_pi($temp, $pos) {
return M_PI;
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _const_e($temp, $pos) {
return M_E;
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _E($temp, $pos) {
return pow(10, $temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _factorial($temp, $pos) {
$factorial = 1;
for($i=1;$i<=$temp[$pos-1];$i++) {
$factorial *= $i;
}
return $factorial;
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _abs($temp, $pos) {
return abs($temp[$pos-1]);
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _mod($temp, $pos) {
return $temp[$pos-2]%$temp[$pos-1];
}
/**
* Math function
*
* @param array $temp Temporary array
* @param integer $pos Position of operator
* @return float Function's relult
* @access private
*/
function _div($temp, $pos) {
return floor($temp[$pos-2]/$temp[$pos-1]);
}
/**
* Calculate RPN Expression and return value
*
* @return float Result of input expression
* @access private
*/
function _rpnToValue() {
$time1 = $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];
$time2 = $this->_getMicroTime();
$this->_timer = $time2 - $time1;
return $this->_value;
}
/**
* Return a time in second
*
* @return float Current time in seconds
* @access private
*/
function _getMicroTime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?