Esempio n. 1
0
<?php

/**
 * Main file
 */
//AJAX requests only
//if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
//    header("HTTP/1.0 404 Not Found");
//    die();
//}
include 'includes/autoload.php';
$callback = null;
$response = '';
if (isset($_REQUEST['callback'])) {
    $callback = trim($_REQUEST['callback']);
}
if (!isset($_REQUEST['expr'])) {
    $response = 'Request error. No expression was given.';
} else {
    $calc = new \Calculator\Calculator();
    $response = $calc->solve(trim($_REQUEST['expr']));
}
$response = "{msg: {$response}}";
if ($callback) {
    echo "{$callback}({$response})";
} else {
    echo $response;
}
Esempio n. 2
0
<?php

require_once 'vendor/autoload.php';
class BiggerOperator extends \Calculator\Operators\BaseOperator
{
    protected $symbol = '>';
    protected $priority = 2;
    public function process($index, \SplDoublyLinkedList $list)
    {
        $first = $list->offsetGet($index - 1);
        $second = $list->offsetGet($index + 1);
        $result = (bool) ($first > $second);
        $list->offsetSet($index - 1, $result);
        unset($list[$index]);
        unset($list[$index]);
    }
}
$formula = '( ( 10 * x + y ) / z AND ( 20 OR 0 ) XOR ( x XNOR y )  ) OR 1 + 2';
$calculator = new \Calculator\Calculator($formula);
//$calculator->setFormula($formula);
\Calculator\Calculator::extend(BiggerOperator::class);
var_dump($calculator->result(['x' => 10, 'y' => 20, 'z' => 2]));