Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Section CSC-160-500 - Computer Science I: C++
- // Student: Caleb Jares
- // Homework Number: 11-11
- // Description: Programming Project
- // Last Changed: November 21, 2010
- #include <iostream>
- #include <string>
- using namespace std;
- int DEFAULT_CAPACITY = 8;
- template <class T>
- class ListRemake
- {
- private:
- int capacity;
- int count;
- T* list;
- public:
- ListRemake(): capacity(DEFAULT_CAPACITY), count(0) { list = new T[capacity]; }
- ListRemake(int capacity): capacity(capacity), count(0) { list = new T[capacity]; }
- ListRemake(ListRemake ©)
- {
- VectorRemake create = VectorRemake(copy.capacity);
- create.list = new T[create.capacity];
- create.count = copy.count;
- for (int i = 0; i < create.count; i++)
- create.list[i] = copy.list[i];
- return create;
- }
- ~ListRemake() {delete [] list;}
- void sort()
- {
- T *end = list + count - 1;
- std::sort(list, list + count);
- }
- inline int get_count() {return count;}
- inline int get_capacity() {return capacity;}
- bool reserve(int size)
- {
- if (size <= capacity) return false;
- resize(size);
- return true;
- }
- void resize(int size, T t=T())
- {
- if (size < capacity)
- {
- for (int i = size; i < capacity; i++)
- list[i] = 0;
- count = size;
- }
- else if(size > capacity)
- {
- T *newlist = new T[size];
- for (int i = 0; i < count; i++) newlist[i] = list[i];
- for (int i = count; i < size; i++) newlist[i] = t;
- delete [] list;
- list = newlist;
- }
- else return;
- capacity = size;
- }
- void add(T &item)
- {
- if (count == capacity) resize(capacity * 2);
- list[count] = item;
- count++;
- }
- T& value_at(int index)
- {
- if (index > count || index < 0) throw std::out_of_range();
- return *this->[index];
- }
- void set_value_at(int index, T &value)
- {
- if (index > count || index < 0) throw std::out_of_range();
- *this->[index] = value;
- }
- void del_last()
- {
- list[count] = 0;
- count--;
- }
- T& operator [](int index) {return list[index];}
- ListRemake operator =(const ListRemake &right)
- {
- if (right.count > size) resize(right.count);
- delete [] list;
- list = right.list;
- }
- bool operator ==(const ListRemake &right)
- {
- if (count == right.count)
- {
- for (int i = 0; i < count; i++)
- if (this->[i] != right[i])
- return false;
- }
- else return false;
- return true;
- }
- inline bool operator !=(const ListRemake &right) {return !(this == right);}
- friend ostream& operator << (ostream& out, const ListRemake<T>& obj);
- };
- template <class T>
- ostream& operator << (ostream& out, const ListRemake<T>& obj)
- {
- for (int i = 0; i < obj.size; i++)
- out << obj[i] << '\n';
- return out;
- }
- int main()
- {
- int num;
- double temp;
- cout << "How many values? ";
- cin >> num;
- num = num < 1 ? 1 : num;
- cout << '\n';
- ListRemake<double> list;
- for (int i = 0; i < num; i++)
- {
- cout << "Value: ";
- cin >> temp;
- list.add(temp);
- }
- cout << "\nThere are " << list.get_capacity() << " values on the list.\n"
- << "The first value is " << list[0] << '.'
- << "\nThe last value is " << list[list.get_count()] << '.';
- list.del_last();
- cout << "\nAfter deleting the last value, there are " << list.get_capacity() << " values left.\n"
- << "The new list is:\n" << list;
- double total = 0;
- for (int i = 0; i < list.get_capacity(); i++)
- total += list[i];
- cout << "\nThe list totals " << total << '\n';
- list.resize(num + 9);
- cout << "Added 10 items to the list for a total of " << list.get_count();
- system("Pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement