本文整理匯總了PHP中Stack::clear方法的典型用法代碼示例。如果您正苦於以下問題:PHP Stack::clear方法的具體用法?PHP Stack::clear怎麽用?PHP Stack::clear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Stack
的用法示例。
在下文中一共展示了Stack::clear方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testPoll
public function testPoll()
{
// Remove the following lines when you implement this test.
$this->assertTrue($this->object->poll()->toNative() == 9);
$this->assertTrue($this->object->poll()->toNative() == 8);
$this->object->clear();
$this->assertTrue($this->object->poll() == null);
}
示例2: evaluateExpression
/**
* Vyhodnocení výrazu
*
* @param array
* @return array
*/
protected function evaluateExpression(array $expr)
{
if ($expr[0] != 'expression') {
return $this->reduceValue($expr);
}
array_shift($expr);
//převod výrazu do postfixové notace
$postfix = array();
$stack = new Stack();
foreach ($expr as $symbol) {
if ($symbol == '(') {
$stack->push($symbol);
} elseif ($symbol == ')') {
while ($top = $stack->pop()) {
if ($top == '(') {
break;
}
$postfix[] = $top;
}
$top = $stack->top();
if ($top[0] == 'unary' && array_key_exists($top[1], Compiler::$unaryOperators)) {
$postfix[] = $stack->pop();
}
} elseif ($symbol[0] == 'binary' && array_key_exists($symbol[1], Compiler::$binaryOperators)) {
if ($stack->count() == 0) {
$stack->push($symbol);
continue;
}
$top = $stack->top();
while ($top != '(' && $stack->count() > 0 && $top[0] == 'binary' && Compiler::$binaryOperators[$symbol[1]] <= Compiler::$binaryOperators[$top[1]]) {
$postfix[] = $stack->pop();
$top = $stack->top();
}
$stack->push($symbol);
} elseif ($symbol[0] == 'unary' && array_key_exists($symbol[1], Compiler::$unaryOperators)) {
$stack->push($symbol);
} else {
$postfix[] = $this->reduceValue($symbol);
$top = $stack->top();
if ($top[0] == 'unary' && array_key_exists($top[1], Compiler::$unaryOperators)) {
$postfix[] = $stack->pop();
}
}
}
while ($stack->count() > 0) {
$postfix[] = $stack->pop();
}
//vyhodnocení výrazu
$stack->clear();
foreach ($postfix as $symbol) {
if ($symbol[0] == 'unary' && array_key_exists($symbol[1], Compiler::$unaryOperators)) {
if ($stack->count() < 1) {
throw new CompileException("Nedostatek operandů pro unární operátor '{$symbol['1']}'");
}
$symbol = $this->evaluateUnaryOperation($symbol[1], $stack->pop());
} elseif ($symbol[0] == 'binary' && array_key_exists($symbol[1], Compiler::$binaryOperators)) {
if ($stack->count() < 2) {
throw new CompileException("Nedostatek operandů pro binární operátor '{$symbol['1']}'");
}
$value2 = $stack->pop();
$symbol = $this->evaluateBinaryOperation($symbol[1], $stack->pop(), $value2);
}
$stack->push($symbol);
}
if ($stack->count() != 1) {
throw new CompileException("Výsledkem výrazu má být pouze 1 hodnota");
}
return $stack->pop();
}