Showing posts with label array pop. Show all posts
Showing posts with label array pop. Show all posts

Wednesday, September 9, 2015

Reverse Polish notation Calculator using stack (for better understanding oop concept)

<?php

class Stack {
    public $array = array();
    public $index = -1;

    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }
    public function push($element)
    {
        $this->index++;
        $this->array[$this->index] = $element;
    }

    public function pop()
    {
        //return $this->array[$this->index--];
        if($this->isEmpty()){
           echo 'Nothing to pop';
        }else{
            $element = $this->array[$this->index];
            unset($this->array[$this->index]);
            $this->index--;
            return $element;

        }

    }
    public function calculator($value1, $value2, $operator){
        if($operator == "+"){
            $result = $value1 + $value2;
        } elseif($operator == "-"){
            $result = $value1 - $value2;
        } elseif($operator == "*"){
            $result = $value1 * $value2;
        } elseif($operator == "/"){
            $result = $value1 / $value2;
        }

        return $result;
    }

}
$stack = new Stack();
if(isset($_POST['numbers'])){
    $numbers = trim($_POST['numbers']);
    $numbers = explode(' ', $numbers);
    foreach ($numbers as $number) {
       if(is_numeric($number)) {
           $number = intval($number);
           $stack->push($number);
       }else{
           $value2 = $stack->pop();
           $value1 = $stack->pop();
           $result = $stack->calculator($value1, $value2, $number);
           $stack->push($result);
       }
    }
$result = $stack->pop();
    echo $result;
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<form method="post" action="">
    <input type="text" name="numbers" id="">
    <input type="submit" name="submit" id="">
</form>
</body>
</html>

array stack and queue function with initialize array using construct function

<?php
class Stack {
    private $stack_array = array();
    public $index = -1;

    public function __construct($array)
    {
        $this->stack_array = $array;
        $length = count($this->stack_array);
        $this->index = $length - 1;
    }
    public function print_array()
    {
        print_r($this->stack_array);
    }
    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }

    public function push($element)
    {
        $this->index++;
        $this->stack_array[$this->index] = $element;
    }

    public function pop()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[$this->index];
            unset($this->stack_array[$this->index]);
            $this->index--;
        }
    }

    public function pop_from_last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[0];
            unset($this->stack_array[0]);
            $this->index--;
            return $element;
        }
    }
    public function first()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[$this->index];
        }
    }

    public function last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[0];
        }
    }

    public function get_element($position)
    {
        if($position >= $this->index + 1){
            echo 'position is exceeding the array index';
        }else{
            $i = $this->index - ($position - 1);
            return $this->stack_array[$i];

        }
    }
}

$stack = new Stack([3, 4, 5, 8]);
$stack->print_array();
echo $stack->index;

Tuesday, September 8, 2015

array stack and queue function in a class to demonstrate push, pop, first, last, pop from last, get element

<?php
class Stack {
    private $stack_array = array();
    private $index = -1;

    public function print_array()
    {
        print_r($this->stack_array);
    }
    public function isEmpty()
    {
        if($this->index == -1){
            return true;
        }else{
            return false;
        }
    }

    public function push($element)
    {
        $this->index++;
        $this->stack_array[$this->index] = $element;
    }

    public function pop()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[$this->index];
            unset($this->stack_array[$this->index]);
            $this->index--;
        }
    }

    public function pop_from_last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            $element = $this->stack_array[0];
            unset($this->stack_array[0]);
            $this->index--;
            return $element;
        }
    }
    public function first()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[$this->index];
        }
    }

    public function last()
    {
        if($this->isEmpty()){
            echo 'The array doesn\'t contain any elemnet';
        }else{
            return $this->stack_array[0];
        }
    }

    public function get_element($position)
    {
        if($position >= $this->index + 1){
           echo 'position is exceeding the array index';
        }else{
            $i = $this->index - ($position - 1);
            return $this->stack_array[$i];

        }
    }
}

$stack = new Stack();
$stack->push(8);
$stack->push(5);
$stack->push(4);
$stack->push(9);
$stack->push(2);
$stack->print_array();
echo '<br>';
echo $stack->get_element(1);
echo '<br>';
echo $stack->get_element(2);
echo '<br>';
echo $stack->get_element(3);
echo '<br>';
echo $stack->get_element(30);

Monday, September 7, 2015

array stack class to demonstrate push pop first last

<?php
class Stack {
    private $array = array(2, 3, 4, 5);
    public function print_array(){
        print_r( $this->array );
    }
    public function push_to_array($value){
        $this->array[] = $value;
    }
    public function pop_from_array(){
        $count = count($this->array) - 1;
        if(empty($this->array[$count])){
           echo 'array is empty';
        } else{
            $pop_value = $this->array[$count];
            unset($this->array[$count]);
            return $pop_value;
        }
    }
    public function last(){
        if(empty($this->array[0])){
            return 'array is empty';
        } else{
            return $this->array[0];
        }
    }
    public function first(){
        $count = count($this->array) - 1;
        if(empty($this->array[$count])){
            return 'array is empty';
        } else{
            return $this->array[$count];
        }
    }

}

$array = new Stack();
echo $array->pop_from_array();
$array->print_array();

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }