本文整理汇总了PHP中Calculator::calc方法的典型用法代码示例。如果您正苦于以下问题:PHP Calculator::calc方法的具体用法?PHP Calculator::calc怎么用?PHP Calculator::calc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Calculator
的用法示例。
在下文中一共展示了Calculator::calc方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: calc
break;
}
if ($need_push) {
array_push($this->stack, $r);
}
}
}
function calc($expression)
{
$this->stack = [];
$this->parser->parse($expression, [$this, '_calc']);
return $this->stack[0];
}
function tree($expression)
{
$this->parser->printTree($expression);
}
}
/////////////////////////////////////////////////////////////
if (realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) {
$calc = new Calculator();
while ($exp = fgets(STDIN)) {
try {
$result = $calc->calc($exp);
echo "{$result}\n";
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
}
exit;
}
示例2: testCalc
function testCalc()
{
$calc = new Calculator();
$result = $calc->calc('123');
$this->assertEquals(123, $result);
$result = $calc->calc('100 - (35 - 4)');
$this->assertEquals(100 - (35 - 4), $result);
$result = $calc->calc('2 * (3 + 4)');
$this->assertEquals(2 * (3 + 4), $result);
$result = $calc->calc('9 / 3 * 2');
$this->assertEquals(9 / 3 * 2, $result);
$result = $calc->calc('(4+1) * (6 - 1)');
$this->assertEquals((4 + 1) * (6 - 1), $result);
$result = $calc->calc('-(11+29)');
$this->assertEquals(-(11 + 29), $result);
$result = $calc->calc('4 - -3 * 4');
$this->assertEquals(4 - -3 * 4, $result);
$result = $calc->calc('-1 * -1');
$this->assertEquals(-1 * -1, $result);
$result = $calc->calc('2.5 * 8');
$this->assertEquals(2.5 * 8, $result);
$result = $calc->calc('cos pi/2');
$this->assertEquals(cos(M_PI) / 2, $result);
$result = $calc->calc('4 pi-1');
$this->assertEquals(4 * M_PI - 1, $result);
$result = $calc->calc('2cos -pi');
$this->assertEquals(2 * cos(-M_PI), $result);
}
示例3: switch
switch ($this->operator) {
case "+":
return $this->num1 + $this->num2;
break;
case "-":
return $this->num1 - $this->num2;
break;
case "*":
return $this->num1 * $this->num2;
break;
case "/":
if ($this->num2 == 0) {
echo "<script>alert('除数不能为0!');</script>";
return "除数不能为0!";
} else {
return $this->num1 / $this->num2;
}
break;
default:
break;
}
}
}
if (isset($_POST["btn"])) {
if (isset($_POST["op"])) {
$calculator1 = new Calculator($_POST["num1"], $_POST["num2"], $_POST["op"]);
echo "计算结果:" . $calculator1->calc();
} else {
echo "<script>alert('请选择操作!');</script>";
}
}