diff options
Diffstat (limited to 'Homework 8/MyStructures')
| -rw-r--r-- | Homework 8/MyStructures/CharArrayHelpers.hpp | 1 | ||||
| -rw-r--r-- | Homework 8/MyStructures/Contact.hpp | 39 | ||||
| -rw-r--r-- | Homework 8/MyStructures/ContactList.hpp | 70 |
3 files changed, 87 insertions, 23 deletions
diff --git a/Homework 8/MyStructures/CharArrayHelpers.hpp b/Homework 8/MyStructures/CharArrayHelpers.hpp index 88cb083..62ecedd 100644 --- a/Homework 8/MyStructures/CharArrayHelpers.hpp +++ b/Homework 8/MyStructures/CharArrayHelpers.hpp @@ -1,3 +1,4 @@ +#include <iostream> inline bool OverwriteCharArray(char* dest, const char* source, size_t size = MAX_CHAR) { size_t newLength = strlen(source); diff --git a/Homework 8/MyStructures/Contact.hpp b/Homework 8/MyStructures/Contact.hpp index 26a0fcd..f0c632f 100644 --- a/Homework 8/MyStructures/Contact.hpp +++ b/Homework 8/MyStructures/Contact.hpp @@ -1,11 +1,16 @@ #ifndef CONTACT_HPP #define CONTACT_HPP +#include <cstring> + +#include "CharArrayHelpers.hpp" + +using std::strcpy; using std::cout; using std::endl; namespace MyStructures - + { class Contact @@ -52,6 +57,8 @@ namespace MyStructures bool operator!=(const Contact& rhs) const; bool operator==(const Contact& rhs) const; + void Delete(); + private: char* firstName_{ new char[MAX_CHAR] }; char* lastName_{ new char[MAX_CHAR] }; @@ -66,9 +73,6 @@ namespace MyStructures }; - - - inline Contact::Contact() { memset(firstName_, 0, MAX_CHAR); @@ -218,7 +222,7 @@ namespace MyStructures } - inline void Contact::State(const char* state) const + inline void Contact::State(const char* state) const { OverwriteCharArray(state_, state); } @@ -271,8 +275,8 @@ namespace MyStructures if (std::strcmp(lastName_, rhs.lastName_) != 0) { return true; } if (std::strcmp(streetAddress_, rhs.streetAddress_) != 0) { return true; } if (std::strcmp(city_, rhs.city_) != 0) { return true; } - if (std::strcmp(state_, rhs.state_)! = 0) {return true;} - if (std::strcmp(email_, rhs.email_)!= 0) { return true; } + if (std::strcmp(state_, rhs.state_)!= 0) { return true; } + if (std::strcmp(email_, rhs.email_) != 0) { return true; } if (zip_ != rhs.zip_) { return true; } return false; @@ -284,11 +288,28 @@ namespace MyStructures if (std::strcmp(lastName_, rhs.lastName_) != 0) { return false; } if (std::strcmp(streetAddress_, rhs.streetAddress_) != 0) { return false; } if (std::strcmp(city_, rhs.city_) != 0) { return false; } - if (std::strcmp(state_, rhs.state_)!= 0) { return false; } + if (std::strcmp(state_, rhs.state_) != 0) { return false; } if (std::strcmp(email_, rhs.email_) != 0) { return false; } if (zip_ != rhs.zip_) { return true; } return true; - } + } + + + inline void Contact::Delete() + { + memset(firstName_, 0, MAX_CHAR); + + memset(lastName_, 0, MAX_CHAR); + + memset(streetAddress_, 0, MAX_CHAR); + + memset(city_, 0, MAX_CHAR); + + memset(state_, 0, MAX_CHAR); + + memset(email_, 0, MAX_CHAR); + } +} #endif
\ No newline at end of file diff --git a/Homework 8/MyStructures/ContactList.hpp b/Homework 8/MyStructures/ContactList.hpp index 887ebfc..3824a0e 100644 --- a/Homework 8/MyStructures/ContactList.hpp +++ b/Homework 8/MyStructures/ContactList.hpp @@ -3,7 +3,7 @@ #include <algorithm> -namespace MySStructures +namespace MyStructures { template<class C> class ContactList @@ -13,8 +13,8 @@ namespace MySStructures * Contains an array of Contacts or othe C class objects */ ContactList() = default; - ContactList(size_t length); //Creates contact list of this size - ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list + ContactList(size_t length); //Creates contact list of this size + ContactList(const C* contacts, const size_t& length, const size_t size); //Makes a copy of another contact list ContactList(const C* storage, const size_t& length); ~ContactList(); @@ -35,12 +35,13 @@ namespace MySStructures void InsertAfter(const C& data, const C& after); //insert contact data after 'after' contact void InsertBefore(const C& data, const C& before); //insert contact data before 'before' contact void Clear(); //Deletes all contacts, empties the list, nothing left! - + void ContactDelete(const size_t& elementNumber); + C& Last(); //returns last contact reference in list C Last() const; //returns copy of last contact in list C& First(); //returns first contact reference in lsit C First() const; - C First() cosnt; //returns first contact copy + //returns first contact copy C& operator[](const int& index); //returns contact reference via [] overload C operator[](const int& index) const; //returns contact copy via [] overload @@ -61,19 +62,30 @@ namespace MySStructures C* AllocateContactList(const size_t& length); }; + + template<class C> + void ContactList<C>::ContactDelete(const size_t& elementNumber) + { + contacts_[elementNumber].Delete(); + } + template <class C> - ContactList<C>::ContactList(size_t length) + ContactList<C>::ContactList(const C* contacts, const size_t& length) { contacts_ = AllocateContactList(length); + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = contacts[i]; + } } template <class C> - ContactList<C>::ContactList(const C* contacts, const size_t& length) + ContactList<C>::ContactList(const C* contacts, const size_t& length, const size_t size) { contacts_ = AllocateContactList(length); - for (auto i = 0u; i < size_; ++i) + for (auto i = 0u; i < length_; ++i) { contacts_[i] = contacts[i]; } @@ -162,15 +174,27 @@ namespace MySStructures void ContactList<C>::Append(const C& data) { - size_t index = 0; + index = size_; + + contacts_[index] = data; + size_++; + return; + // whatifs; //whatif: end of the array is the beginning //whatif: in the body of the array - if (size_ < length_) + if (size_ >= length_) { index = size_; } + + if (size_ == 0) + { + contacts_[index] = data; + size_++; + return; + } //whatif: end of the array //length_ == 10; // size_ ==10 @@ -188,6 +212,7 @@ namespace MySStructures // Add to end of the array contacts_[index] = data; size_++; + return; } template <class C> @@ -195,6 +220,10 @@ namespace MySStructures { if (Empty()) { + if(contacts_ == nullptr) + { + + } Append(data); return; } @@ -202,7 +231,7 @@ namespace MySStructures if (!Full()) { - for (auto i = size_-1; i >= 0; i--) + for (int i = static_cast<int> (size_)-1; i >= 0; i--) { contacts_[i + 1] = contacts_[i]; } @@ -210,7 +239,7 @@ namespace MySStructures return; } - auto temp = ContactList<C>(length_ * 2); + auto temp = ContactList<C>(length_); temp.Append(data); for (auto i = 0; i < size_ ++i) { @@ -227,7 +256,10 @@ namespace MySStructures template <class C> void ContactList<C>::RemoveLast() { + if (Empty()) return; + ContactDelete(size_ - 1); + size_--; } template <class C> @@ -290,13 +322,23 @@ namespace MySStructures template<class C> inline C& ContactList<C>::operator[](const int& index) { - // TODO: insert return statement here + if (index < 0 || index >= static_cast<int>(length_)) + { + cerr << "The index exceeds the array length" << endl; + return emptyContact; + } + Contact contact = contacts_[index]; + return contact; } template <class C> C ContactList<C>::operator[](const int& index) const { - //TODO CHECK RANGE + if (index < 0 || index >= static_cast<int>(size_)) + { + cerr << "The index exceeds the array length" << endl; + return NULL;; + } return contacts_[index]; } |