#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "stack.h"

using namespace std;

//  ---------  funzioni membro della classe stack ------------
stack::stack()
{
    num=0;
    sp =-1;
    v=new int [SIZE];
}
    
stack::~stack()
{
    cout << "invocato distruttore classe stack" << endl;
    delete [] v;
}
    
bool stack::push(int val)
{
    if(num<SIZE)
    {
        num++;
        sp++;
        v[sp]=val;
        return true;
    }
    else
    {
        cout << "stack pieno" << endl;
        return false;
    }
}
    
bool stack::pop(int& val )
{
    if(num>0)
    {
        val=v[sp];
        sp--;
        num--;
        return true;
    }
    else
    {
        cout << "stack vuoto" << endl;
        return false;
    }
}


//  ---------  funzioni membro della classe stack_g ------------


stack_g::stack_g()
{
    num=0;
    sp =0;
    v=new char [SIZE];
}
    
stack_g::~stack_g()
{
    cout << "invocato distruttore classe stack_g" << endl;
    delete [] v;
}
    
bool stack_g::push(int val)
{
    if(sp+sizeof(int) <= SIZE)
    {
        num++;
        pi=(int*) v+sp;
        *pi=val;
        sp=sp+sizeof(int);
        return true;
    }
    else
    {
        cout << "stack pieno" << endl;
        return false;
    }
}
    
bool stack_g::push(char val)
{
    if(sp+sizeof(char) <= SIZE)
    {
        num++;
        pc=v+sp;
        *pc=val;
        sp=sp+sizeof(char);
        return true;
    }
    else
    {
        cout << "stack pieno" << endl;
        return false;
    }
}

bool stack_g::push(float val)
{
    if(sp+sizeof(float) <= SIZE)
    {
        num++;
        pf=(float*) v+sp;
        *pf=val;
        sp=sp+sizeof(float);
        return true;
    }
    else
    {
        cout << "stack pieno" << endl;
        return false;
    }
}

bool stack_g::pop(int& val )
{
    if( sp>=sizeof(int) )
    {
        pi=(int*) v+sp-sizeof(int);
        val=*pi;
        sp=sp-sizeof(int);
        num--;
        return true;
    }
    else
    {
        cout << "stack vuoto" << endl;
        return false;
    }
}

bool stack_g::pop(char& val )
{
    if( sp>=1 )
    {
        pc=v+sp-1;
        val=*pc;
        sp--;
        num--;
        return true;
    }
    else
    {
        cout << "stack vuoto" << endl;
        return false;
    }
}

bool stack_g::pop(float& val )
{
    if( sp>=sizeof(float) )
    {
        pf=(float*) v+sp-sizeof(float);
        val=*pf;
        sp=sp-sizeof(float);
        num--;
        return true;
    }
    else
    {
        cout << "stack vuoto" << endl;
        return false;
    }
}













