예제 #1
0
    public function rcalc($operator, $operand) {
        $out = [];
        foreach ($this->values as $value) {
            $out[] = CalcOperation::calc($operator, $value, $operand);
        }

        return new CalcSet($out);
    }
예제 #2
0
 function calc($vars = array())
 {
     $stack = array();
     foreach ($this->rpn as $step) {
         if (is_object($step) || !isset($this->ooo[$step])) {
             $stack[] = $step;
         } else {
             //echo "Operation: {$step}\n";
             //print_r($stack);
             $r1 = array_pop($stack);
             $r2 = array_pop($stack);
             if (is_numeric($r1) && is_numeric($r2)) {
                 $stack[] = CalcOperation::calc($step, $r1, $r2);
             }
             if ($r1 instanceof CalcSet && is_numeric($r2)) {
                 $stack[] = $r1->calc($step, $r2);
             }
             if (is_numeric($r1) && $r2 instanceof CalcSet) {
                 $stack[] = $r2->rcalc($step, $r1);
             }
         }
     }
     if (count($stack) > 1) {
         return 'Missing operator near "' . $stack[1] . '".';
     } else {
         $out = reset($stack);
         if (is_bool($out)) {
             return $out ? '<span class="true">true</span>' : '<span class="false">false</span>';
         } else {
             return $out;
         }
     }
 }
예제 #3
0
    /**
     * @return mixed|string
     * @throws \Exception
     */
    public function __invoke() {

        $stack = [];

        foreach ($this->rpn as $step) {
            if (is_object($step) || !isset($this->ooo[$step])) {
                $stack[] = $step;
            } else {
                $r1 = array_pop($stack);
                $r2 = array_pop($stack);

                if (is_numeric($r1) && is_numeric($r2)) {
                    $stack[] = CalcOperation::calc($step, $r2, $r1);
                }
                if ($r1 instanceof CalcSet && is_numeric($r2)) {
                    $stack[] = $r1->calc($step, $r2);
                }
                if (is_numeric($r1) && $r2 instanceof CalcSet) {
                    $stack[] = $r2->rcalc($step, $r1);
                }
                if ($r1 instanceof CalcSet && $r2 instanceof CalcSet) {
                    $stack[] = $r1->mcalc($step, $r2);
                }
            }
        }

        if (count($stack) > 1) {
            throw new \Exception('Missing operator near "' . $stack[1] . '".');
        } else {
            $out = reset($stack);

            return $out;
        }
    }