Example #1
0
 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);
 }
Example #2
0
 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());
 }
Example #3
0
 /**
  * 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
     //self::$postFix = $pf;
     // return the RPN array in case developer wants to use it for some insane reason (bug testing ;] )
     // Also... because we pass it right in to the RPN solver.  So I guess there's that too.
     return $pf;
 }