Advertisement
Guest User

Untitled

a guest
May 24th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.29 KB | None | 0 0
  1. #ifndef CONTAINER_H
  2. #define CONTAINER_H
  3. #include<iostream>
  4. using namespace std;
  5. template <class T>
  6. class aghContainer{
  7.   public:
  8.     virtual ~aghContainer(){};
  9.     virtual bool insert(int index, T const& val){};
  10.     virtual T& at(int index) const{};
  11.     virtual int size(void) const{};
  12.     virtual bool remove(int index){};
  13.     void append(T const& val){
  14.       insert(size(),val);
  15.     }
  16.    
  17.     void show() const{
  18.       for(int i=0;i<size();i++)
  19.         cout<<at(i)<<" - ";
  20.       cout<<endl;
  21.     }
  22.    
  23.     bool replace(int index, T const& val){
  24.       if(index<=size() || index<0)
  25.         return false;
  26.       remove(index);
  27.       insert(index,val);
  28.       return true;
  29.     }
  30.    
  31.     void clear(void){
  32.       while(size())
  33.         remove(0);
  34.     }
  35.    
  36.     bool isEmpty(void){
  37.       if(size()==0)
  38.         return true;
  39.      
  40.       return false;
  41.     }
  42.     int indexOf(T const& _value, int _from = 0) const{
  43.       if(_from<0 || _from>=size())
  44.         return -1;
  45.       for(int i=_from;i<size();i++)
  46.         if(at(i)== _value) return i; // zwroc indeks pierwszego napotkanego elementu o zadanej wartosci
  47.        
  48.       return -1;
  49.     }
  50.     bool contains(T const& _value, int _from = 0) const{
  51.       if(_from<0 || _from>=size())
  52.         return false;
  53.       for(int i=_from;i<size();i++)
  54.         if(at(i)== _value) return true; //zwroc prawde przy znalezieniu pierwszego elementu o zadanej wartosci
  55.      
  56.       return false;
  57.     }    
  58.     aghContainer<T>& operator=(const aghContainer<T>  &right){
  59.       if(*this!=right){
  60.             clear(); //usun zawartosc
  61.             for(int i=0;i< right.size(); i++)
  62.                 append(right.at(i));    //przekopiuj zawartosc prawego do this
  63.           return *this;
  64.         }
  65.     }
  66.     bool operator==(aghContainer<T> const& right){
  67.       if(size() != right.size()){
  68.         return false;
  69.       }
  70.       for(int i=0;i<size();i++)
  71.         if(at(i)!=right.at(i))
  72.           return false;  //zwroc falsz przy pierwszym napotkanym roznym elemencie
  73.      
  74.       return true;
  75.     }    
  76.     bool operator!=(aghContainer<T> const& right){
  77.       return !(*this == right);
  78.     }
  79.     T& operator[](int n) const{return at(n);}
  80.    
  81.  
  82.     aghContainer<T>& operator+=(aghContainer<T> const& right){
  83.       for(int i=0;i<right.size();i++)
  84.         append(right.at(i)); //dawanie kolejnych wartości na koniec
  85.      
  86.       return *this;
  87.     }
  88.     aghContainer<T>& operator+=(T const& element){
  89.       append(element); //dodanie jednej wartosci na koniec
  90.       return *this;
  91.     }
  92.     aghContainer<T>& operator<<(T const& element){
  93.       *this += element;  //operator "<<" robi to samo co operator "+="
  94.       return *this;
  95.     }
  96.     aghContainer<T>& operator<<(aghContainer<T> const& right){
  97.       *this +=right; //operator "<<" robi to samo co operator "+="
  98.      
  99.       return *this;
  100.     }
  101.     friend ostream& operator<<(ostream& output, aghContainer<T> const& right){
  102.       for(int i=0;i<size();i++)
  103.         output<<at(i)<<" "; //kolejne elementy pojemnika beda przedzielone spacjami
  104.       return output;
  105.    }
  106. };
  107.  
  108. #endif
  109.  
  110.  
  111.  
  112. /---------------------------------------------------
  113.  
  114.  
  115.  
  116.  
  117. #ifndef VECTOR_H
  118. #define VECTOR_H
  119. #include"aghContainer.h"
  120. #include"aghException.h"
  121. template<class T>
  122. class aghVector: public aghContainer<T>{
  123.   private:
  124.     T* tab;      /// tablica
  125.     int length;  /// rozmiar tablicy
  126.   public:
  127.   /// \brief konstruktor kopiujacy
  128.     aghVector<T> (const aghContainer<T> &obj){
  129.       tab=new T[obj.size()];
  130.       length=obj.size();
  131.       for(int i=0;i<length;i++)
  132.         at(i)=obj.at(i);
  133.     }
  134.   /// \brief konstruktor domyslny
  135.     aghVector<T> (int _length=0){
  136.       length=_length;
  137.       tab=new T[1];
  138.     }
  139.   /// \brief destruktor  
  140.     ~aghVector<T>(){
  141.       delete[] tab;
  142.       tab=NULL;
  143.       length=0;
  144.     }
  145.   //-------------------------------------------------
  146.   /// \brief wstaw element na wybrana pozycje
  147.   /// \param index - gdzie wstawic
  148.   /// \param val - co wstawic
  149.   /// \return true udalo sie wstawic , false nie udalo sie
  150.     bool insert(int index, T const& val){
  151.       if(index < 0) //proba wstawienia na ujemny indeks
  152.         return false;
  153.       if(index >= length){ //wstawianie na indeks wiekszy od rozmiaru
  154.         T* tempTab=new T[length];
  155.         //tablica pomocnicza ktora stanie sie kopia aktualnej
  156.         for(int i=0;i<length;i++)
  157.           tempTab[i]=tab[i];
  158.            
  159.         delete [] tab;
  160.         tab=new T[index+1];
  161.         tab[index]=val;
  162.         //stworz nowa tablice na o odpowiedniej dlugosci
  163.         for(int i=0;i<length;i++)
  164.           tab[i]=tempTab[i];
  165.          
  166.         delete [] tempTab;
  167.         length =index+1; //zwieksz dlugosc
  168.         //na tej tablicy znajdzie sie dodany element na odpowiednim indeksie
  169.       }
  170.       else{// index mniejszy od dlugosci
  171.         length++; // zwieksz dlugosc
  172.         T* tempTab=new T[length];
  173.         //stworz tablice pomocnicza
  174.         for(int i=0;i<index;i++)
  175.           tempTab[i]=tab[i];
  176.        
  177.         tempTab[index]=val;
  178.         //tablica pomocnicza jest tworzona w 2 etapach
  179.         //kopiuj elementy do indeksu - dodaj element z parametru - kopiuj elementy po indeksie
  180.         for(int i=index+1;i<length;i++)
  181.           tempTab[i]=tab[i-1];
  182.        
  183.         delete [] tab;
  184.         tab=new T[length];
  185.         //skopiuj elementy na aktualna tablice
  186.         for(int i=0;i<length;i++)
  187.           tab[i]=tempTab[i];
  188.          
  189.         delete [] tempTab;
  190.       }
  191.       return true;
  192.     }
  193.     //-------------------------------------------------
  194.   /// \brief zwroc referencje do indeksu
  195.   /// \param index - ktory indeks
  196.   /// \return zwroc referencje
  197.     T& at(int index) const{
  198.       if(index>=size() || index<0){
  199.         return tab[0];
  200.         throw aghException("Index out of range!",-1);
  201.       }
  202.       return tab[index];
  203.     }
  204.     //-------------------------------------------------
  205.   /// \brief zwroc rozmiar
  206.   /// \return rozmiar
  207.     int size(void) const{
  208.       return length;
  209.     }
  210.     //-------------------------------------------------
  211.   /// \brief usun element o wybranym indeksie
  212.   /// \param index - ktory indeks
  213.   /// \return true da sie usunac - false nie da sie
  214.     bool remove(int index){
  215.       if(index<0 || index>=length)
  216.         return false;
  217.        
  218.       length--;//zmniejsz rozmiar
  219.       T* tempTab=new T[length];
  220.       //tablica pomocnicza
  221.       for(int i=0;i<index;i++)
  222.         tempTab[i]=tab[i];
  223.          
  224.       for(int i=index;i<length;i++)
  225.         tempTab[i]=tab[i+1];          
  226.       //kopiowanie do tablicy podzielono na 2 etapy
  227.       //przed indeksem i po indeksie , pomijajac sam indeks
  228.       delete [] tab;
  229.       tab=new T[index];
  230.        
  231.       for(int i=0;i<length;i++)
  232.         tab[i]=tempTab[i];
  233.       //skopiuj do aktualnej tablicy    
  234.       delete [] tempTab;
  235.       return true;
  236.     }
  237.     //-------------------------------------------------
  238.   /// \brief operator porownania
  239.   /// \param right obiekt do ktorego przyrownamy
  240.   /// \return zwraca referencje do wskaznika this
  241.     aghVector<T>& operator=(const aghVector<T>& right){
  242.       if(*this!=right){
  243.         for(int i=0;i<size();i++)
  244.           remove(0);
  245.         for(int i=0;i<right.size();i++)
  246.           insert(i,right.at(i));  
  247.         return *this;
  248.       }
  249.     }
  250.     //-------------------------------------------------
  251. };    
  252. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement