中缀转后缀计算

<?php

class Calcu
{
    const EXP= ['+','-','*','/','(',')'];

    public static function calculate($arr){
        $outInput = [];
        foreach ($arr as $v) {
            if (!in_array($v, self::EXP)) {
                array_push($outInput, $v);
                continue;
            }
            $a = array_pop($outInput);
            $b = array_pop($outInput);
            switch ($v) {
                case '+':
                    array_push($outInput, bcadd($a, $b, 2));
                    break;
                case '-':
                    array_push($outInput, bcsub($b, $a, 2));
                    break;
                case '*':
                    array_push($outInput, bcmul($b, $a, 2));
                    break;
                case '/':
                    array_push($outInput, bcdiv($b, $a, 2));
                    break;
                default:
                    break;
            }
        }
        return count($outInput) == 1 ? $outInput[0] : 0;
    }

    public static function infixToSuffix($arr)
    {
        $exps        = ['+' => 1, '-' => 1, '*' => 2, '/' => 2, "(" => 0, ")" => 0];
        $expStack    = [];
        $outputQueue = [];
        foreach ($arr as $input) {
            if (!in_array($input, array_keys($exps))) {
                array_push($outputQueue, $input);
                continue;
            }
            if ($input == "(") {
                array_push($expStack, $input);
                continue;
            }
            if ($input == ")") {
                $tmpExp = array_pop($expStack);
                while ($tmpExp && $tmpExp != "(") {
                    array_push($outputQueue, $tmpExp);
                    $tmpExp = array_pop($expStack);
                }
                continue;
            }
            foreach (array_reverse($expStack) as $exp) {
                if ($exps[$input] <= $exps[$exp]) {
                    array_pop($expStack);
                    array_push($outputQueue, $exp);
                } else {
                    break;
                }
            }
            array_push($expStack, $input);
        }
        foreach (array_reverse($expStack) as $exp) {
            array_push($outputQueue, $exp);
        }
        return $outputQueue;
    }

    public static function strToArray($str)
    {
        $i   = 0;
        $tt  = '';
        $arr = [];
        $len = strlen($str);
        while($i < $len){
            if(in_array(substr($str, $i,1), Self::EXP)){
                foreach(Self::EXP as $v){
                    if($v == substr($str, $i,1)){
                        if(!empty($tt)){
                            $arr[] = $tt;
                        }
                        $arr[] = substr($str, $i,1);
                        $tt = '';
                    }
                }
            }else{
                $tt .= substr($str, $i,1);
                if( $len-1 == $i){
                    $arr[] = $tt;
                }
            }
            $i++;
        }
        return $arr;
    }

    public static function init($str)
    {
        $strArray = self::strToArray($str);
        $suffix = self::infixToSuffix($strArray);
        return self::calculate($suffix);
    }
}
//var_dump(Calcu::init('1+(232-3)*4+122/5-6/3-(1008/3)+0.5'));die();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsy321123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值