首页 > php教程 > php手册 > 正文

PHP计算后序表达式(逆波兰式)

php中文网
发布: 2016-06-06 20:08:27
原创
1187人浏览过

百度谷歌搜索无果,只好自己造一次轮子。 PHP /** * rpn2value * 计算逆波兰式 * @author leo108 root@leo108.com */function rpn2value($str){ $arr = explode(',',$str); $stack = array(); $len = count($arr); for($i=0;$i$len;$i++){ if(is_numeric($ar

百度谷歌搜索无果,只好自己造一次轮子。 php

/**
 * rpn2value
 * 计算逆波兰式
 * @author   leo108 root@leo108.com
 */
function rpn2value($str){
    $arr = explode(',',$str);
    $stack = array();
    $len = count($arr);
    for($i=0;$i<$len;$i++){
        if(is_numeric($arr[$i])){
            array_push($stack,$arr[$i]);
        }else{
            $op = $arr[$i];
            $right = array_pop($stack);
            $left = array_pop($stack);
            eval("\$re = $left $op $right;");
            array_push($stack,$re);
        }   
    }
    return $stack[0];
}
登录后复制

使用方法:

逆波兰式

$str = "1,2,3,+,*,4,-,5,+,7,*";
echo rpn2value($str);
登录后复制

另附中序转后序代码,版权归原作者所有 http://leo108.com/pid-1901.asp

/**
 * math_rpn
 *
 * 实现逆波兰式算法
 *
 * @author   sparkHuang 260558820@qq.com
 * @version  RPN 1.0.0
 *
 */
class math_rpn {
    //初始的计算表达式
    private $_expression = '';
    //处理后的逆波兰表达式
    private $_rpnexp = array();
    //模拟栈结构的数组
    private $_stack  = array('#');
    //正则判断
    //private $_reg    = '/^([A-Za-z0-9\(\)\+\-\*\/])*$/';
    //优先级
    private $_priority = array('#' => 0, '(' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30);
    //四则运算
    private $_operator = array('(', '+', '-', '*', '/', ')');
    public function __construct($expression) {
        $this->_init($expression);
    }
    private function _init($expression) {
        $this->_expression = $expression;
    }
    public function exp2rpn() {
        $len = strlen($this->_expression);
        for($i = 0; $i < $len; $i++) {
            $char = substr($this->_expression, $i, 1);
            if ($char == '(') {
                $this->_stack[] = $char;
                continue;
            } else if ( ! in_array($char, $this->_operator)) {
                $this->_rpnexp[] = $char;
                continue;
            } else if ($char == ')') {
                for($j = count($this->_stack); $j >= 0; $j--) {
                    $tmp = array_pop($this->_stack);
                    if ($tmp == "(") {
                        break;
                    } else {
                        $this->_rpnexp[] = $tmp;
                    }
                }
                continue;
            } else if ($this->_priority[$char] <= $this->_priority[end($this->_stack)]) {
                $this->_rpnexp[] = array_pop($this->_stack);
                $this->_stack[]  = $char;
                continue;
            } else {
                $this->_stack[] = $char;
                continue;
            }
        }
        for($i = count($this->_stack); $i >= 0; $i--) {
            if (end($this->_stack) == '#') break;
            $this->_rpnexp[] = array_pop($this->_stack);
        }
        return $this->_rpnexp;
    }
}
//测试实例
$expression = "(A*(B+C)-E+F)*G";
var_dump($expression);
$mathrpn = new math_rpn($expression);
var_dump($mathrpn->exp2rpn());
登录后复制

 

中序表达式

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号