Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1.  
  2. //------------------------------------------------------------------
  3. #ifndef AGH_VECTOR
  4. #define AGH_VECTOR
  5.  
  6. //-------------------------------------------------------------------
  7.  
  8.  
  9.  
  10. template <class T>
  11. class aghVector : public aghContainer<T>
  12. {
  13. private:
  14.     T * tab;    ///< Stores a type T items
  15.     int sizetable;  ///< Stores a size of table
  16.     int howmanyitems; ///< Stores a number of items in tab
  17.  
  18. public:
  19.  
  20.     /// \brief The constructor which creating array (tab) with size = 1; sets sizetable on 1, and howmanyitems on 0.
  21.     aghVector();
  22.  
  23.     /// \brief The second constructor which casts other on type aghVector<T> and copies all content of it.
  24.     aghVector( const aghContainer<T>& other);
  25.  
  26.     /// \brief Destructor which sets howmanyitems on 1, and delete reserved memory by *tab.
  27.     ~aghVector();
  28.    
  29.    
  30.   /// \brief adds an item into Vector more precisely into tab
  31.   ///  \param n - position on which item should be added
  32.   ///  \param item - it is item which we want to add into vector
  33.   /// \return true if succeed or false if it fail.
  34.     bool insert(int n, T const& item);
  35.    
  36.   /// \brief Creat free place in Vector (tab) on n position
  37.   ///  \param n - position on which  should be free place in tab
  38.     void rozsun (int n);
  39.    
  40.     /// \brief Show on the screen content of aghVector
  41.     void show();
  42.  
  43.    /// \brief It show us how many items aghVector contains
  44.   /// \return number of items
  45.     int size() const;
  46.  
  47.    /// \brief Return item on n position
  48.   ///  \param n - position on which item shoulb be return
  49.   /// \return item on n position
  50.     T& at(int n) const;
  51.  
  52.    /// \brief Remove items from n position
  53.   ///  \param n - position on which item should be removed
  54.   /// \return true if succeed or false if n position is wrong
  55.     bool remove(int n);
  56.  
  57.   /// \brief Opposite of rozsun
  58.   ///  \param n - position on which vector should be fill
  59.     void zsun(int n);
  60.  
  61. /// \brief an overloaded operator = that copies content of right side object to left side object
  62. ///
  63. /// \param other - right side object which should be copied
  64. /// \return returns a lefside object;
  65.     aghVector<T> & operator=(aghVector<T> const & other);
  66. };
  67.  
  68.  
  69. //----------------------Konstruktor--------------------------------
  70.  
  71.  
  72. template <class T>
  73. aghVector<T>::aghVector()
  74. {
  75.     tab = new T[1];
  76.     sizetable = 1;
  77.     howmanyitems = 0;
  78.  
  79. }
  80. //----------------------Destruktor----------------------------------------
  81. template<class T>
  82. aghVector<T>::~aghVector()
  83. {
  84.     howmanyitems = 0;
  85.     delete [] tab;
  86. }
  87. //-------------------Konstruktor przeci��ony------------------------------
  88. template <class T>
  89. aghVector<T>::aghVector(  const aghContainer<T>& other)
  90. {
  91. const aghVector<T>& b = dynamic_cast<const aghVector<T>&>(other);
  92. sizetable = b.sizetable;
  93. howmanyitems = b.howmanyitems;
  94.  
  95. tab = new T[sizetable];
  96. for (int i = 0; i < sizetable; i ++)
  97. {
  98.     tab[i] = b.tab[i];
  99. }
  100.  
  101. }
  102.  
  103.  
  104. //--------------------funkcja size------------------------------------
  105. template <class T>
  106. int aghVector<T>::size() const
  107. {
  108.     return howmanyitems;
  109. }
  110.  
  111.  
  112. //-----------------------funkcja AT---------------------------------
  113. template <class T>
  114. T& aghVector<T>::at(int n) const
  115. {
  116.     if ( (n <0) || (n > sizetable))  throw aghException(0, "Index out of range", __FILE__, __LINE__);
  117.     return tab[n];
  118. }
  119.  
  120. //-----------------------Funkcja rozsun-------------------------------
  121.  
  122. template <class T>
  123. void aghVector<T>::rozsun(int n)
  124. {
  125.     if (sizetable >= 2)
  126.     {
  127.     for (int i = sizetable - 1; i >  n; i-- )
  128.     {
  129.    
  130.         tab[i] = tab[i - 1];
  131.     }
  132.     }
  133. }
  134.  
  135. //------------------------Zsun------------------------------------------
  136.  
  137. template <class T>
  138. void aghVector<T>::zsun(int n)
  139. {
  140.     if (sizetable >= 2)
  141.     {
  142.         for (int i = n; i < sizetable -1; i++)
  143.         {
  144.             tab[i] = tab[i+1];
  145.         }
  146.     }
  147.    
  148. }
  149.  
  150. //--------------------remove-----------------------------------------
  151. template <class T>
  152. bool aghVector<T>::remove(int n)
  153. {
  154.     if ((n >= 0) && (n <= (howmanyitems -1)))
  155.     {
  156.         zsun(n);
  157.         howmanyitems--;
  158.         return true;
  159.  
  160.     }
  161.    
  162.    
  163.     return false;
  164.    
  165. }
  166.  
  167. //-----------------------Wyswietlanie elementow-------------------------
  168.  
  169. template <class T>
  170. void aghVector<T>::show()
  171. {
  172.     for (int i = 0; i < sizetable; i ++)
  173.     {
  174.         cout << tab[i] << " ";
  175.     }
  176. }
  177.  
  178.  
  179. //-------------------Insert------------------------------------------
  180.  
  181. template <class T>
  182. bool aghVector<T>::insert(int n, T const& item)
  183. {
  184.     if (n < 0 ) return false;
  185.     if (sizetable == howmanyitems)
  186.     {
  187.         T * temp = new T[sizetable];
  188.         for (int i = 0; i < sizetable; i++)
  189.         {
  190.             temp[i] = tab[i];
  191.         }
  192.  
  193.         delete [] tab;
  194.         tab = new T[sizetable + 1];
  195.        
  196.         for (int i = 0; i < sizetable; i ++)
  197.         {
  198.             tab[i] = temp[i];
  199.         }
  200.         sizetable ++;
  201.  
  202.     }
  203.    
  204.  
  205.     rozsun (n);
  206.     tab[n] = item;
  207.     howmanyitems ++;
  208.  
  209.    
  210.  
  211.     return true;    
  212. }
  213.  
  214. //--------------------------OPERATOR=-------------------------------
  215. template <class T>
  216. aghVector<T> & aghVector<T>::operator=(aghVector<T> const & other)
  217. {
  218.     int stab = other.size();
  219.     if ( &other != this)
  220.     {
  221.         for (int i = 0; i < stab; i ++)
  222.         {
  223.             insert(i,other.at(i));
  224.         }
  225.     }
  226.     return *this;
  227. }
  228. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement