php逆波兰表达式,我就给一个PHP逆波兰表达式的算法吧---工资计算专用-程序员宅基地

技术标签: php逆波兰表达式  

有个网友写信给我谈到关于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; }

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39616686/article/details/115128681

智能推荐

微信小程序 swiper轮播图不显示_微信小程序swipper不显示-程序员宅基地

文章浏览阅读5.3k次,点赞6次,收藏6次。设置display flex后,swiper轮播图不显示display 如果在app.wxss中使用也会导致轮播图不显示_微信小程序swipper不显示

[剑指Offer]-序列化二叉树_剑指序列化二叉树-程序员宅基地

文章浏览阅读1.9k次。题目描述请实现两个函数,分别用来序列化和反序列化二叉树解题思路序列化是指通过前序遍历把二叉树变成数组反序列化是指重建二叉树算法图解参考代码:package offer;/** * 请实现两个函数,分别用来序列化和反序列化二叉树 */import javax.swing.tree.TreeNode;public class Offer37 { String ..._剑指序列化二叉树

manjaro 配置zsh以及powerLine主题安装_manjaro 主题安装-程序员宅基地

文章浏览阅读2.2w次,点赞8次,收藏25次。manjaro 配置 vim zsh安装 zsh安装 oh-my-zsh安装 powerline及字体使用zsh替换bash(重新打开终端生效)powerline 配置bashrc 配置(注意python版本)vim .zshrc(注意python版本)vim支持powerline-vim配置配置ZSH更换powerline主题安装 zshsudo pacman -S zsh安装 oh-..._manjaro 主题安装

现代电子计算机英文,现代计算机辅助技术,modern computer aid technology,音标,读音,翻译,英文例句,英语词典...-程序员宅基地

文章浏览阅读123次。补充资料:铸造工艺计算机辅助设计技术2.1 铸造工艺CAD在铸造工艺设计过程中,有许多繁琐的数学计算和大量的查表选择等工作,仅凭工艺设计人员的个人经验和手工操作,不但要花费很多时间,而且设计结果往往因人而异,很难保证铸件质量,60年代以来,特别是进入80年代后,随着电子计算机技术的迅猛发展,计算机辅助设计技术在工业中得到愈来愈广泛的应用,也为铸造工艺设计的科学化、精确化提供了良好的工具,成为铸造技..._现代电子计算机的英文

镜头指标及传感器指标_镜头和传感器-程序员宅基地

文章浏览阅读448次。镜头指标:杂光、解析度、畸变、眩光、漏光1.什么是杂光:杂光是一种非正常传输的光学现象,如果有杂光现象,十字架会被拍成和一个圆圈,这是由于镜头内部及表面缺陷造成的反射。2.什么是解析度:解析度是判断图像细节的多少(测试解析度的工具ISO1223)1.镜头2.模组装配误差3.模组调焦不准4.软件 传感器sharpness gamma的设置3.畸变:枕型畸变和桶型畸变4.眩光:视野中由于不适宜亮度分布,在空间存在极端的亮度对比,各镜片偶次反射,特别是二次反射是造成眩光的主要原因之一。5.漏光传_镜头和传感器

iTOP6818——针对ubuntu系统的双屏异显kernel配置_ubuntu 设置界面显示fb1-程序员宅基地

文章浏览阅读572次。引言iTOP6818是8核嵌入式开发平台,在ubuntu系统下可以设置触摸屏显示或HDMI显示,但官方并没有做对应的双屏异显,即触摸屏和HDMI显示不同的画面。基于个人需求,本人完成了该功能配置,现将配置方法说明如下。资料下载配置思路在内核中重新创建一个fb节点来挂载HDMI设备,主设备触摸屏为fb0,次设备HDMI为fb1,通过修改内核驱动代码可以实现双屏异显。配置方法..._ubuntu 设置界面显示fb1

随便推点

java 将图片转为base64返回给前端_java将图片转为base64返回给前端-程序员宅基地

文章浏览阅读7.3k次,点赞4次,收藏23次。一 controller端代码@RequestMapping(value = "/captcha") public void imagecode(HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject object = new JSONObject(); Captch..._java将图片转为base64返回给前端

关于TCP重传、乱序和重复的问题_tcp通讯数据不按顺序输出-程序员宅基地

文章浏览阅读1.1w次,点赞5次,收藏23次。数据重传TCP提供两种重传的机制,一种是基于时间的超时重传,一种是基于接收端反馈消息的快速重传。相比之下前者占用更少的网络带宽,但是效率很低。而后者则相反。下面我们来具体看一下这两种机制的实现方式。超时重传顾名思义,如果发送端等待接收端发送的ACK超过了TCP所设置的RTO,那么此时发送端便会重传刚发的数据包。一般而言,TCP会对数据包的超时重传非常重视,当发生这种情况时,TCP会降低当..._tcp通讯数据不按顺序输出

点击收藏代码 php,php 实现收藏功能的示例代码-程序员宅基地

文章浏览阅读436次。整理文档,搜刮出一个php 实现收藏功能的示例代码,稍微整理精简一下做下分享。HTML:取消收藏收藏Js://点击收藏,实现已收藏$('.x').on('click',function(){var Oa=$(this);var id=Oa.attr('id');//获取图片id属性var uid = Oa.attr('uid');//获取用户idvar status = Oa.attr('stat..._收藏按钮代码

java docker java api_Java API 操作Docker示例-程序员宅基地

文章浏览阅读661次。大家好,我是邵奈一,一个不务正业的程序猿、正儿八经的斜杠青年。1、世人称我为:被代码耽误的诗人、没天赋的书法家、五音不全的歌手、专业跑龙套演员、不合格的运动员…2、这几年,我整理了很多IT技术相关的教程给大家,爱生活、爱分享。3、如果您觉得文章有用,请收藏,转发,评论,并关注我,谢谢!博客导航跳转(请收藏):邵奈一的技术博客导航| 公众号 | 微信 | 微博 | CSDN | 简书 |0x00 教..._java docker api demo

K线入门之初识K线-程序员宅基地

文章浏览阅读2w次。K线图

¥3EG踩坑记录¥Vitis HLS xfopencv库的安装解决minGW32-make 编译opencv时 error_vitishls导入xfopencv库导入不进去怎么办-程序员宅基地

文章浏览阅读4.3k次。解决minGW32-make 编译opencv时 error1、'mutex' in namespace 'std' does not name a type。minGW64下载解决办法:下载如图所示版本2、gcc: error: long: No such file or directory解决办法:在cmake里面找到OPENCV_ENABLE_ALLOCATOR_STATS,然后把后面的勾选去掉,重新生成后继续编译。3、error: 'std::_hypot' has no_vitishls导入xfopencv库导入不进去怎么办

推荐文章

热门文章

相关标签