stack = array($value); $this->index = 1; } public function push($value){ $this->stack[$this->index] = $value; $this->index = $this->index + 1; } public function pop(){ if($this->index>0){ $this->index = $this->index - 1; return $this->stack[$this->index]; } else { throw new Exception("Cannot pop from an empty stack!"); } } public function add() { $this->push($this->pop($this->stack) + $this->pop($this->stack)); } public function subtract() { $ergYounger =$this->pop(); $ergOlder = $this->pop(); $this->push($ergOlder - $ergYounger); } public function multiply() { $this->push($this->pop() * $this->pop()); } public function divide() { $ergYounger =$this->pop(); $ergOlder = $this->pop(); $this->push($ergOlder / $ergYounger); } public function value(){ if($this->index=1){ return $this->stack[0]; } else { throw new Exception("There are more than one value on the stack!"); } } } $mycalc = new MyCalculator(); echo 'Start with value: ' . $mycalc -> value() . '
'; $mycalc -> push(3); $mycalc -> add(); echo '0 + 3 = ' . $mycalc -> value() . '
'; $mycalc -> push(2); $mycalc -> multiply(); echo '3 * 2 = ' . $mycalc -> value() . '
'; $mycalc -> push(2); $mycalc -> divide(); echo '6 / 2 = ' . $mycalc -> value() . '
'; $mycalc -> push(3); $mycalc -> push(4); $mycalc -> add(); $mycalc -> multiply(); echo '3 * (3 + 4) = ' . $mycalc -> value() . '
'; ?>