Advertisement
Guest User

ListRemake for class

a guest
Nov 21st, 2010
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. // Section CSC-160-500 - Computer Science I: C++
  2. // Student: Caleb Jares
  3. // Homework Number: 11-11
  4. // Description: Programming Project
  5. // Last Changed: November 21, 2010
  6.  
  7. #include <iostream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. int DEFAULT_CAPACITY = 8;
  13.  
  14. template <class T>
  15. class ListRemake
  16. {
  17. private:
  18.     int capacity;
  19.     int count;
  20.     T* list;
  21.  
  22. public:
  23.     ListRemake(): capacity(DEFAULT_CAPACITY), count(0) { list = new T[capacity]; }
  24.     ListRemake(int capacity): capacity(capacity), count(0) { list = new T[capacity]; }
  25.     ListRemake(ListRemake &copy)
  26.     {
  27.         VectorRemake create = VectorRemake(copy.capacity);
  28.         create.list = new T[create.capacity];
  29.         create.count = copy.count;
  30.         for (int i = 0; i < create.count; i++)
  31.             create.list[i] = copy.list[i];
  32.         return create;
  33.     }
  34.     ~ListRemake() {delete [] list;}
  35.  
  36.     void sort()
  37.     {
  38.         T *end = list + count - 1;
  39.         std::sort(list, list + count);
  40.     }
  41.     inline int get_count() {return count;}
  42.     inline int get_capacity() {return capacity;}
  43.     bool reserve(int size)
  44.     {
  45.         if (size <= capacity) return false;
  46.         resize(size);
  47.         return true;
  48.     }
  49.     void resize(int size, T t=T())
  50.     {
  51.         if (size < capacity)
  52.         {
  53.             for (int i = size; i < capacity; i++)
  54.                 list[i] = 0;
  55.             count = size;
  56.         }
  57.         else if(size > capacity)
  58.         {
  59.             T *newlist = new T[size];
  60.  
  61.             for (int i = 0; i < count; i++) newlist[i] = list[i];
  62.             for (int i = count; i < size; i++) newlist[i] = t;
  63.  
  64.             delete [] list;
  65.             list = newlist;
  66.         }
  67.         else return;
  68.         capacity = size;
  69.     }
  70.     void add(T &item)
  71.     {
  72.         if (count == capacity) resize(capacity * 2);
  73.         list[count] = item;
  74.         count++;
  75.     }
  76.     T& value_at(int index)
  77.     {
  78.         if (index > count || index < 0) throw std::out_of_range();
  79.         return *this->[index];
  80.     }
  81.     void set_value_at(int index, T &value)
  82.     {
  83.         if (index > count || index < 0) throw std::out_of_range();
  84.         *this->[index] = value;
  85.     }
  86.     void del_last()
  87.     {
  88.         list[count] = 0;
  89.         count--;
  90.     }
  91.  
  92.     T& operator [](int index) {return list[index];}
  93.     ListRemake operator =(const ListRemake &right)
  94.     {
  95.         if (right.count > size) resize(right.count);
  96.  
  97.         delete [] list;
  98.         list = right.list;
  99.     }
  100.     bool operator ==(const ListRemake &right)
  101.     {
  102.         if (count == right.count)
  103.         {
  104.             for (int i = 0; i < count; i++)
  105.                 if (this->[i] != right[i])
  106.                     return false;
  107.         }
  108.         else return false;
  109.  
  110.         return true;
  111.     }
  112.     inline bool operator !=(const ListRemake &right) {return !(this == right);}
  113.  
  114.     friend ostream& operator << (ostream& out, const ListRemake<T>& obj);
  115. };
  116.  
  117. template <class T>
  118. ostream& operator << (ostream& out, const ListRemake<T>& obj)
  119. {
  120.     for (int i = 0; i < obj.size; i++)
  121.         out << obj[i] << '\n';
  122.     return out;
  123. }
  124.  
  125. int main()
  126. {
  127.     int num;
  128.     double temp;
  129.     cout << "How many values? ";
  130.     cin >> num;
  131.     num = num < 1 ? 1 : num;
  132.     cout << '\n';
  133.  
  134.     ListRemake<double> list;
  135.     for (int i = 0; i < num; i++)
  136.     {
  137.         cout << "Value: ";
  138.         cin >> temp;
  139.         list.add(temp);
  140.     }
  141.    
  142.     cout << "\nThere are " << list.get_capacity() << " values on the list.\n"
  143.         << "The first value is " << list[0] << '.'
  144.         << "\nThe last value is " << list[list.get_count()] << '.';
  145.     list.del_last();
  146.     cout << "\nAfter deleting the last value, there are " << list.get_capacity() << " values left.\n"
  147.         << "The new list is:\n" << list;
  148.  
  149.     double total = 0;
  150.     for (int i = 0; i < list.get_capacity(); i++)
  151.         total += list[i];
  152.     cout << "\nThe list totals " << total << '\n';
  153.  
  154.     list.resize(num + 9);
  155.  
  156.     cout << "Added 10 items to the list for a total of " << list.get_count();
  157.  
  158.     system("Pause");
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement