<repo_name>Mattiamato/Array-based-stack<file_sep>/main.cpp
//
//  main.cpp
//  stack_arraybased
//
//  Created by <NAME> on 08.02.12.
//  Copyright (c) 2012 Universität Zürich. All rights reserved.
//

#include <iostream>
#include "stack_ab.h"

int main (int argc, const char * argv[])
{

    Stack_ab mystack;
    std::string temp;
    std::cout<<"initial size of stack: "<<mystack.size()<<"\n";
    try {
        mystack.push("Villa");
        mystack.push("Messi");
        mystack.push("Alexis");
        mystack.push("Iniesta");
        mystack.push("Xavi");
        mystack.push("Mascherano");
        mystack.push("<NAME>");
        mystack.push("Abidal");
        mystack.push("Pique");
        mystack.push("Puyol");
        mystack.push("Valdes");

    } catch (OverFlow& e) {
        e.overflowoutput();
    }
        
    std::cout<<"current size of stack: "<<mystack.size()<<"\n";

    std::cout<<"current top element is: "<<mystack.top()<<"\n";
    
    try {
        while (!mystack.empty()) {
            std::cout<<mystack.pop()<<" wurde gepopt\n";
            std::cout<<mystack.top()<<" is now on top\n";
            //in the console these sentences are funny =)
        }
    } catch (UnderFlow& e) {
        e.underflowoutput();  
    }
    
	delete &mystack;

    int i;
	std::cin >> i;

    return 0;
}

<file_sep>/stack_ab.cpp
//
//  stack_ab.cpp
//  stack_arraybased
//
//  Created by <NAME> on 16.02.12.
//  Copyright (c) 2012 Universität Zürich. All rights reserved.
//

#include <iostream>
#include "stack_ab.h"

//all implementations here!!!

Stack_ab::Stack_ab()					//constructor
{
	index = -1;
}

Stack_ab::~Stack_ab()					//destructor
{
	for(int i=0; i < StackMax; i++)
		data[i].clear();
	index = -1;
}

int Stack_ab::size()					//return stack size
{
	return index+1;
}

void Stack_ab::push(std::string value)	//add to stack
{
	try
	{
		if(index+1 < StackMax)
		{
			index++;
			data[index] = value;
		}
		else
		{
			OverFlow e;
			throw e;
		}
	}
	catch(OverFlow e)
	{
		e.overflowoutput();
	}
		
}

std::string Stack_ab::pop()				//remove from stack
{
	try
	{
		index--;
		return data[index+1];
	}
	catch(UnderFlow e)
	{
		e.underflowoutput();
	}
}

std::string Stack_ab::top()				//return top position
{
	try	
	{
		if(index != -1)
			return data[index];
		else
		{
			UnderFlow e;
			throw e;
		}
	}
	catch(UnderFlow e)
	{
		e.underflowoutput();
		return "Nothing";
	}
}

bool Stack_ab::empty()					//is_empty
{
	return index==-1;
}