科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道应用软件PHP逆波兰表达式的算法-工资计算专用

PHP逆波兰表达式的算法-工资计算专用

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文讲解了PHP的逆波兰表达式算法主要用于web工资管理中,用户可以自定义其工资相公式的情况。

作者:LUPA  来源:LUPA  2007年9月14日

关键字: PHP 逆波兰表达式 工资计算

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共19页)

有个网友写信给我谈到关于PHP计算工资问题。我以前一篇文章中谈到过一种计算工资的方法,不过是偷巧,利用现有的表达式的工具,现在既然有人想要,我就给出一个逆波兰的算法。

我们的目标是实现如下的计算公式:

假设有一个计算公式如下:

$expression = "(F1*F12+10.34)";

其中的变量值如下:

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

我们希望用PHP构建一个类来计算出这个表达式的值。这种应用主要用于web工资管理中,用户可以自定义其工资相公式的情况。

$rpn = new Math_Rpn();

$rpn->setExpressionValue($expression_value); 

echo $rpn->calculate($expression,'deg',false); // 即为相应的值

解析逆波兰表达式的方法,编译原理中有,就是先把表达式分解成符号数组,然后求逆波兰式,最后根据逆波兰式得到其结果。

我分别把三个函数贴在下面,其实本质我就是对Pear的RPN函数进行了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() {

$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;

  }

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    闂傚倸鍊搁崐椋庢閿熺姴鐭楅幖娣妼缁愭鏌¢崶鈺佷汗闁哄閰i弻鏇$疀鐎n亞浠炬繝娈垮灠閵堟悂寮婚弴锛勭杸閻庯綆浜栭崑鎾诲冀椤撱劎绋忛梺璺ㄥ櫐閹凤拷

    濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ巻鍋撶紓宥咃躬楠炲啫螣鐠囪尙绐為梺褰掑亰閸撴盯鎮惧ú顏呪拺闂傚牊鍗曢崼銉ョ柧婵犲﹤瀚崣蹇旂節婵犲倻澧涢柛瀣ㄥ妽閵囧嫰寮介妸褋鈧帡鏌熼挊澶婃殻闁哄瞼鍠栭幃婊堝煛閸屾稓褰嬮柣搴ゎ潐濞叉ê鐣濈粙璺ㄦ殾闁割偅娲栭悡娑㈡煕鐏炲墽鐭嬫繛鍫熸倐濮婄粯鎷呯粵瀣異闂佹悶鍔嬮崡鍐茬暦閵忋倕鍐€妞ゆ劑鍎卞皬闂備焦瀵х粙鎴犫偓姘煎弮瀹曚即宕卞Ο闀愮盎闂侀潧鐗嗛幊搴㈡叏椤掆偓閳规垿鍩ラ崱妞剧凹濠电姰鍨洪敋閾荤偞淇婇妶鍛櫤闁稿鍊圭换娑㈠幢濡纰嶉柣搴㈣壘椤︾敻寮诲鍫闂佸憡鎸鹃崰搴敋閿濆鏁嗗〒姘功閻绻涢幘鏉戠劰闁稿鎹囬弻锝呪槈濞嗘劕纾抽梺鍝勬湰缁嬫垿鍩為幋锕€宸濇い鏇炴噺閳诲﹦绱撻崒娆戝妽妞ゃ劌鎳橀幆宀勫磼閻愰潧绁﹂柟鍏肩暘閸斿矂鎮為崹顐犱簻闁圭儤鍨甸鈺呮倵濮橆剦妲归柕鍥у瀵粙濡歌閸c儳绱撴担绛嬪殭婵☆偅绻堝濠氭偄绾拌鲸鏅i悷婊冪Ч閹﹢鎳犻鍌滐紲闁哄鐗勯崝搴g不閻愮儤鐓涢悘鐐跺Г閸犳﹢鏌℃担鐟板鐎规洜鍠栭、姗€鎮╅搹顐ら拻闂傚倷娴囧畷鍨叏閹惰姤鈷旂€广儱顦崹鍌炴煢濡尨绱氶柨婵嗩槸缁€瀣亜閺嶃劎鈽夋繛鍫熺矒濮婅櫣娑甸崨顔俱€愬銈庡亝濞茬喖宕洪埀顒併亜閹哄棗浜鹃梺鎸庢穿婵″洤危閹版澘绫嶉柛顐g箘椤撴椽姊虹紒妯哄鐎殿噮鍓欒灃闁告侗鍠氶崢鎼佹⒑閸撴彃浜介柛瀣閹﹢鏁冮崒娑氬幈闁诲函缍嗛崑鍡樻櫠椤掑倻纾奸柛灞剧☉缁椦囨煙閻熸澘顏柟鐓庢贡閹叉挳宕熼棃娑欐珡闂傚倸鍊风粈渚€骞栭銈傚亾濮樺崬鍘寸€规洖缍婇弻鍡楊吋閸涱垽绱遍柣搴$畭閸庨亶藝娴兼潙纾跨€广儱顦伴悡鏇㈡煛閸ャ儱濡煎褜鍨伴湁闁绘ǹ绉鍫熺畳闂備焦瀵х换鍌毼涘Δ鍛厺闁哄洢鍨洪悡鍐喐濠婂牆绀堟慨妯挎硾閽冪喖鏌曟繛褍瀚烽崑銊╂⒑缂佹ê濮囨い鏇ㄥ弮閸┿垽寮撮姀鈥斥偓鐢告煥濠靛棗鈧懓鈻嶉崶銊d簻闊洦绋愰幉楣冩煛鐏炵偓绀嬬€规洟浜堕、姗€鎮㈡總澶夌处

    重磅专题
    往期文章
    最新文章