Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
h>
#define INC_CAPACITY 10
using namespace std;
template<class T>
class Array
{
private:
T** List; // lista cu pointeri la obiecte de tipul T*
int Capacity; // dimensiunea listei de pointeri
int Size; // cate elemente sunt in lista
public:
class ArrayIterator
{
private:
int Current; //indicele elementului curent
(Array<T>)* MyArray; // pointer catre obiectul de tip Array
public:
ArrayIterator()
{
Current = -1;
MyArray = NULL;
}
ArrayIterator(int Index, Array<T> * ParamArray)
{
Current = Index;
MyArray = ParamArray;
}
//prefix
ArrayIterator& operator ++ ()
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//postfix
ArrayIterator& operator ++ (int val)
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//prefix
ArrayIterator& operator -- ()
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
//postfix
ArrayIterator& operator -- (int val)
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
bool operator= (ArrayIterator & ParamIterator)
{
//if (ParamIterator.Current = -1 || ParamIterator.MyArra
y == NULL)return false;
this->Current = ParamIterator.Current;
this->MyArray = ParamIterator.MyArray;
return true;
}
bool operator!=(ArrayIterator & ParamIterator)
{
if (this->Current <= ParamIterator.Current || this->MyAr
ray != ParamIterator.MyArray)return true;
return false;
}
T* GetElement()
{
if (!this->MyArray || this->Current == -1) {
printf("Err: Iterator neinitializat!\n");
return NULL;
}
return &(MyArray->operator[](Current));
}
};
// Lista nu e alocata, Capacity si Size = 0
Array()
{
Capacity = 0;
Size = 0;
List = NULL;
}
// destructor
~Array(){};
ArrayIterator GetBeginIterator()
{
ArrayIterator *Temp = new ArrayIterator(0,this);
return *Temp;
}
//returneaza End Iterator
ArrayIterator GetEndIterator()
{
ArrayIterator *Temp = new ArrayIterator(this->GetSize()-1, this)
;
return *Temp;
}
};
________________________________________________
Lucaniuc Dragos Lucian
________________________________________________
________________________________________________
________________________________________________
#include <stdio.h>
#define INC_CAPACITY 10
using namespace std;
template<class T>
class Array
{
private:
T** List; // lista cu pointeri la obiecte de tipul T*
int Capacity; // dimensiunea listei de pointeri
int Size; // cate elemente sunt in lista
public:
class ArrayIterator
{
private:
int Current; //indicele elementului curent
(Array<T>)* MyArray; // pointer catre obiectul de tip Array
public:
ArrayIterator()
{
Current = -1;
MyArray = NULL;
}
ArrayIterator(int Index, Array<T> * ParamArray)
{
Current = Index;
MyArray = ParamArray;
}
//prefix
ArrayIterator& operator ++ ()
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//postfix
ArrayIterator& operator ++ (int val)
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//prefix
ArrayIterator& operator -- ()
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
//postfix
ArrayIterator& operator -- (int val)
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
bool operator= (ArrayIterator & ParamIterator)
{
//if (ParamIterator.Current = -1 || ParamIterator.MyArra
y == NULL)return false;
this->Current = ParamIterator.Current;
this->MyArray = ParamIterator.MyArray;
return true;
}
bool operator!=(ArrayIterator & ParamIterator)
{
smal)!\n");
if (index >= Size)printf("Err: Could not delete, index out of SI
ZE!\n");
for (int i = index; i < Size - 1; i++)
{
List[index] = List[index + 1];
}
Size--;
return (*this);
}
//operator de copiere
bool operator=(const Array<T> & ParamList)
{
this->Size = ParamList.Size;
this->Capacity = ParamList.Capacity;
T** Temp = new T*[Capacity];
List = Temp;
for (int i = 0; i < Size; i++)
{
List[i] = ParamList.List[i];
}
return true;
}
// sorteaza folosind comparatia intre elementele din T
void Sort()
{
for (int i = 0; i < Size - 1; i++)
{
for (int j = i + 1; j < Size; j++)
{
if (*List[i]>*List[j]) {
T* aux = List[i];
List[i] = List[j];
List[j] = aux;
}
}
}
}
//returneaza marimea array-ului
int GetSize()
{
return Size;
}
//returneaza capacitatea array-ului
int GetCapacity()
{
return Capacity;
}
// cauta un element folosind binary search in Array
int BinarySearch(const T& ParamElement)
{
this->Sort();
int Low = 0, Up = Size - 1;