本文整理汇总了PHP中Stack::peek方法的典型用法代码示例。如果您正苦于以下问题:PHP Stack::peek方法的具体用法?PHP Stack::peek怎么用?PHP Stack::peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack::peek方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testPeek
public function testPeek()
{
// Remove the following lines when you implement this test.
$this->assertTrue($this->object->peek()->toNative() == 9);
$this->assertTrue($this->object->peek()->toNative() == 9);
$this->object->clear();
$this->assertTrue($this->object->peek() == null);
}
示例2: testStack
public function testStack()
{
$stack = new Stack();
$this->assertEquals(0, $stack->length());
$stack->push('foo');
$this->assertEquals('foo', $stack->peek());
$this->assertEquals(1, $stack->length());
$this->assertEquals('foo', $stack->pop());
$this->assertEquals(0, $stack->length());
}
示例3: in2post
/**
* Infix to Postfix
*
* Converts an infix (standard) equation to postfix (RPN) notation.
* Sets the internal variable $this->postFix for the Parser::solvePF()
* function to use.
*
* @link http://en.wikipedia.org/wiki/Infix_notation Infix Notation
* @link http://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish Notation
* @param String $infix A standard notation equation
* @throws \Exception When parenthesis are mismatched
* @return Array Fully formed RPN Stack
*/
public static function in2post($infix)
{
// if an equation was not passed, use the one that was passed in the constructor
//$infix = (isset($infix)) ? $infix : $this->inFix;
//check to make sure 'valid' equation
self::checkInfix($infix);
$pf = array();
$ops = new Stack();
//$vars = new Stack();
// remove all white-space
$infix = preg_replace("/\\s/", "", $infix);
// Create postfix array index
$pfIndex = 0;
//what was the last character? (useful for discerning between a sign for negation and subtraction)
$lChar = '';
//loop through all the characters and start doing stuff ^^
for ($i = 0; $i < strlen($infix); $i++) {
// pull out 1 character from the string
$chr = substr($infix, $i, 1);
// if the character is numerical
if (preg_match('/[0-9.]/i', $chr)) {
// if the previous character was not a '-' or a number
if (!preg_match('/[0-9.]/i', $lChar) && $lChar != "" && (isset($pf[$pfIndex]) && $pf[$pfIndex] != "-")) {
$pfIndex++;
}
// increase the index so as not to overlap anything
// Add the number character to the array
if (isset($pf[$pfIndex])) {
$pf[$pfIndex] .= $chr;
} else {
$pf[$pfIndex] = $chr;
}
} elseif (in_array($chr, self::$SEP['open'])) {
// if the last character was a number, place an assumed '*' on the stack
if (preg_match('/[0-9.]/i', $lChar)) {
$ops->push('*');
}
$ops->push($chr);
} elseif (in_array($chr, self::$SEP['close'])) {
// find what set it was i.e. matches ')' with '(' or ']' with '['
$key = array_search($chr, self::$SEP['close']);
// while the operator on the stack isn't the matching pair...pop it off
while ($ops->peek() != self::$SEP['open'][$key]) {
$nchr = $ops->pop();
if ($nchr) {
$pf[++$pfIndex] = $nchr;
} else {
throw new \Exception("Error while searching for '" . self::$SEP['open'][$key] . "' in " . self::$inFix, Math::E_NO_SET);
}
}
$ops->pop();
} elseif (in_array($chr, self::$ST)) {
while (in_array($ops->peek(), self::$ST)) {
$pf[++$pfIndex] = $ops->pop();
}
$ops->push($chr);
$pfIndex++;
} elseif (in_array($chr, self::$ST1)) {
while (in_array($ops->peek(), self::$ST1) || in_array($ops->peek(), self::$ST)) {
$pf[++$pfIndex] = $ops->pop();
}
$ops->push($chr);
$pfIndex++;
} elseif (in_array($chr, self::$ST2)) {
// if it is a '-' and the character before it was an operator or nothingness (e.g. it negates a number)
if ((in_array($lChar, array_merge(self::$ST1, self::$ST2, self::$ST, self::$SEP['open'])) || $lChar == "") && $chr == "-") {
// increase the index because there is no reason that it shouldn't..
$pfIndex++;
$pf[$pfIndex] = $chr;
} else {
while (in_array($ops->peek(), array_merge(self::$ST1, self::$ST2, self::$ST))) {
$pf[++$pfIndex] = $ops->pop();
}
$ops->push($chr);
$pfIndex++;
}
}
// make sure we record this character to be referred to by the next one
$lChar = $chr;
}
// if there is anything on the stack after we are done...add it to the back of the RPN array
while (($tmp = $ops->pop()) !== false) {
$pf[++$pfIndex] = $tmp;
}
// re-index the array at 0
$pf = array_values($pf);
// set the private variable for later use if needed
//.........这里部分代码省略.........