[ create a new paste ] login | about

Link: http://codepad.org/os02nO03    [ raw code | fork ]

C++, pasted on Jun 2:
//Data.h

#ifndef DATA_H
#define DATA_H
#include <iostream>
using namespace std;


class Data{
	unsigned key;
public:
	Data(unsigned k=0) : key(k) {}
	bool operator==(Data &d) {return key == d.key;}
	bool operator < (Data &d) {return key < d.key;}
	friend ostream& operator<< (ostream& s, Data &d);
};


inline ostream& operator<< (ostream& s, Data &d)
{
	cout << d.key << endl;
	return s;
}


#endif





//Sequence.h

#ifndef SEQUENCE_H
#define SEQUENCE_H


#pragma warning( disable : 4290) 

class InvalidIndex { };
class SequenceOverflow { };

template<class AD>
class Sequence {
private:
	AD *seq;
	unsigned max;
	unsigned len;
public:
	Sequence(unsigned n);
	~Sequence(void);
	unsigned size(void) const;
	bool isEmpty(void) const;
	bool isFull(void) const;
	AD& operator[] (unsigned pos) throw (InvalidIndex);
	void pushBack(AD &el) throw (SequenceOverflow);
	void insert(AD &el) throw (SequenceOverflow);
	bool remove(AD &el);
};


#endif





//Sequence.cpp

#include "sequence.h"


template<class AD>
Sequence<AD>::Sequence(unsigned n)
{
	max=n;
	seq= new AD[max];
	len=0;
}

template<class AD>
Sequence<AD>::~Sequence(void)
{
	delete [] seq;
}

template<class AD>
unsigned Sequence<AD>::size(void) const 
{
	return len;
}

template<class AD>
bool Sequence<AD>::isEmpty(void) const
{
	return len==0;
}

template<class AD>
bool Sequence<AD>::isFull(void) const
{
	return len==max;
}

template<class AD>
AD& Sequence<AD>::operator[](unsigned pos) throw (InvalidIndex)
{
	
	if (pos>=len)
		throw InvalidIndex();
	
	return seq[pos];
}

template<class AD>
void Sequence<AD>::pushBack(AD &el) throw (SequenceOverflow)
{
	if (isFull())
	  throw SequenceOverflow();

	seq[len+1]=el;
	len++;
}

template<class AD>
void Sequence<AD>::insert(AD &el) throw (SequenceOverflow)
{
	if (isFull)
		throw SequenceOverflos();

	unsigned pos,i;
	
	for(pos=0;pos<len;pos++)
		if(el<seq)
			break;

	for(i=len;i>pos;i--)
		seq[i+1]=seq[i];

	seq[pos]=el;

	len++;
}

template<class AD>
bool Sequence<AD>::remove(AD &el)
{
	unsigned i,j;

	for(i=0;i<len;i++)
		if(seq[i]==el)
			break;
		else
			return false;
	for(j=len;j>len;j--)
		seq[j-1] = seq[j];
	len--;
	return true;
}





//main.cpp

#include <iostream>
#include"data.h"
#include "sequence.h"
using namespace std;

typedef Sequence<Data> DataSeq;

void listAll(DataSeq& dataSeq);

void main()
{
  DataSeq dataSeq(5);
  Data data;

  try
  {
	dataSeq.pushBack(Data(1));
	dataSeq.pushBack(Data(2));
	dataSeq.pushBack(Data(4));
	dataSeq.pushBack(Data(5));
  }
  catch (SequenceOverflow )
  {
	cout << "Sequenceoverflow" << endl;
  }

  listAll (dataSeq);


  try
  {
		dataSeq.insert(Data(3));
  }
  catch (SequenceOverflow)
  {
	  cout << "Sequenceoverflow" << endl;
  }

   listAll (dataSeq);



   dataSeq.remove(Data(2));

   listAll (dataSeq);
   

}



void listAll(DataSeq& dataSeq)
{
	cout <<"All data items: ";
	for(unsigned i=0;i<dataSeq.size();i++)
		cout << dataSeq[i]<<" ";
	cout << endl;
}


Create a new paste based on this one


Comments: