本文整理汇总了PHP中Stack::nth方法的典型用法代码示例。如果您正苦于以下问题:PHP Stack::nth方法的具体用法?PHP Stack::nth怎么用?PHP Stack::nth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack::nth方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testInsertAndRemove
public function testInsertAndRemove()
{
$Stack = new Stack();
$Stack->push('Radig');
$this->assertIdentical($Stack->size(), 1);
$this->assertIdentical($Stack->nth(), 'Radig');
$Stack->push('CakePHP');
$this->assertIdentical($Stack->size(), 2);
// testa com 1-lookahead
$this->assertIdentical($Stack->nth(), 'CakePHP');
// teste com 2-lookahead
$this->assertIdentical($Stack->nth(2), 'Radig');
$this->assertIdentical($Stack->pop(), 'CakePHP');
$this->assertIdentical($Stack->pop(), 'Radig');
$this->assertIdentical($Stack->pop(), null);
unset($Stack);
}
示例2: nfx
/**
* Convert infix to postfix notation
*
* @param string $expr
*/
protected function nfx($expr)
{
$index = 0;
$stack = new Stack();
$output = array();
// postfix form of expression, to be passed to pfx()
$expr = trim($expr);
$ops = array('+', '-', '*', '/', '^', '_');
$ops_r = array('+' => 0, '-' => 0, '*' => 0, '/' => 0, '^' => 1);
// right-associative operator?
$ops_p = array('+' => 0, '-' => 0, '*' => 1, '/' => 1, '_' => 1, '^' => 2);
// operator precedence
// we use this in syntax-checking the expression
// and determining when a - is a negation
$expecting_op = false;
// make sure the characters are all good
if (preg_match("/[^\\w\\s+\\*\\^\\/\\(\\)\\.,\\-]/", $expr, $matches)) {
return $this->error("Illegal character '{$matches[0]}'");
}
// 1 Infinite Loop ;)
while (1) {
// get the first character at the current index
$op = substr($expr, $index, 1);
// find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
$ex = preg_match('/^([a-zA-Z]\\w*\\(?|\\d+(?:\\.\\d*)?|\\.\\d+|\\()/', substr($expr, $index), $match);
// is it a negation instead of a minus?
if ($op == '-' and !$expecting_op) {
// put a negation on the stack
$stack->push('_');
$index++;
} elseif ($op == '_') {
// but not in the input expression
return $this->error("Illegal character '_'");
} elseif ((in_array($op, $ops) or $ex) and $expecting_op) {
// are we expecting an operator but have a number/variable/function/opening parethesis?
if ($ex) {
// it's an implicit multiplication
$op = '*';
$index--;
}
// heart of the algorithm:
while ($stack->size() > 0 && ($o2 = $stack->nth()) && in_array($o2, $ops) && ($ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2])) {
// pop stuff off the stack into the output
$output[] = $stack->pop();
}
// many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail
// finally put OUR operator onto the stack
$stack->push($op);
$index++;
$expecting_op = false;
} elseif ($op == ')' and $expecting_op) {
// pop off the stack back to the last (
while (($o2 = $stack->pop()) != '(') {
if (is_null($o2)) {
return $this->error("Unexpected ')'");
} else {
$output[] = $o2;
}
}
// did we just close a function?
if (preg_match("/^([a-zA-Z]\\w*)\\(\$/", $stack->nth(2), $matches)) {
// get the function name
$fnn = $matches[1];
// see how many arguments there were (cleverly stored on the stack, thank you)
$arg_count = $stack->pop();
// pop the function and push onto the output
$output[] = $stack->pop();
// check the argument count
if (in_array($fnn, $this->fb)) {
if ($arg_count > 1) {
return $this->error("Too many arguments ({$arg_count} given, 1 expected)");
}
} elseif (array_key_exists($fnn, $this->f)) {
if ($arg_count != count($this->f[$fnn]['args'])) {
return $this->error("Wrong number of arguments ({$arg_count} given, " . count($this->f[$fnn]['args']) . " expected)");
}
} else {
return $this->error("Internal error");
}
}
$index++;
} elseif ($op == ',' and $expecting_op) {
while (($o2 = $stack->pop()) != '(') {
if (is_null($o2)) {
// oops, never had a (
return $this->error("Unexpected ','");
} else {
// pop the argument expression stuff and push onto the output
$output[] = $o2;
}
}
// make sure there was a function
if (!preg_match("/^([a-zA-Z]\\w*)\\(\$/", $stack->nth(2), $matches)) {
return $this->error("Unexpected ','");
}
//.........这里部分代码省略.........