diff options
| -rw-r--r-- | Homework 8/Homework 8/main.cpp | 70 | ||||
| -rw-r--r-- | Homework 8/MyStructures/CharArrayHelpers.cpp | 0 | ||||
| -rw-r--r-- | Homework 8/MyStructures/CharArrayHelpers.hpp | 17 | ||||
| -rw-r--r-- | Homework 8/MyStructures/Contact.hpp | 316 | ||||
| -rw-r--r-- | Homework 8/MyStructures/ContactList.hpp | 80 | ||||
| -rw-r--r-- | Homework 8/MyStructures/MyStructures.vcxproj | 1 | ||||
| -rw-r--r-- | Homework 8/MyStructures/MyStructures.vcxproj.filters | 1 |
7 files changed, 383 insertions, 102 deletions
diff --git a/Homework 8/Homework 8/main.cpp b/Homework 8/Homework 8/main.cpp index 18941d7..78ee54f 100644 --- a/Homework 8/Homework 8/main.cpp +++ b/Homework 8/Homework 8/main.cpp @@ -8,8 +8,12 @@ #include "Contact.hpp" #include "ContactList.hpp" -using namespace MyStructures; +sing namespace MyStructures; using std::cout; +using std::cin; + +Contact NewContact(); +void Prompt(); int main() { @@ -22,11 +26,32 @@ int main() { contact00.Zip(97089); contact00.Email("[email protected]"); + Contact contact01; + contact00.FirstName("Vlada"); + contact00.LastName("S"); + contact00.StreetAddress("12332 Ankey Woods"); + contact00.City("Portland"); + contact00.State("OR"); + contact00.Zip(97220); + contact00.Email("[email protected]"); + + Contact contact02; + contact00.FirstName("Permata"); + contact00.LastName("Helmy"); + contact00.StreetAddress("123 Seasame Street"); + contact00.City("Lawrenceville"); + contact00.State("GA"); + contact00.Zip(30043); + contact00.Email("[email protected]"); + ContactList<Contact> addressBook(3); addressBook.Append(contact00); - addressBook.Append(contact00); - addressBook.Append(contact00); + addressBook.Append(contact01); + addressBook.Append(contact02); + + + if (addressBook.Size() != 3) { @@ -38,9 +63,44 @@ int main() { } ContactList<Contact> secondAddressBook(addressBook); - + if (addressBook == secondAddressBook) {} + if (addressBook.Full()) { } + + addressBook.Clear(); + + addressBook.Prepend(contact00); + addressBook.Prepend(contact01); + addressBook.Prepend(contact02); + + addressBook[0] == contact02; + + + + + + + do + { + Prompt(); + + cin >> input; + + switch (toupper(input)) + { + case'N': + addressBook.Append(NewContact()); + break; + case 'P': + addressBook.Print(); + break; + case 'X': + default: + cout << "Bad Input" << endl; + } + } return 0; -}
\ No newline at end of file +} + diff --git a/Homework 8/MyStructures/CharArrayHelpers.cpp b/Homework 8/MyStructures/CharArrayHelpers.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Homework 8/MyStructures/CharArrayHelpers.cpp diff --git a/Homework 8/MyStructures/CharArrayHelpers.hpp b/Homework 8/MyStructures/CharArrayHelpers.hpp new file mode 100644 index 0000000..88cb083 --- /dev/null +++ b/Homework 8/MyStructures/CharArrayHelpers.hpp @@ -0,0 +1,17 @@ +inline bool OverwriteCharArray(char* dest, const char* source, size_t size = MAX_CHAR) +{ + size_t newLength = strlen(source); + + if (newLength >= size) + { + std::cerr << "Error: Buffer size exceeded.\n"; + return false; + } + + memset(dest, 0, size); + + strcpy(dest, source); + + return true; + +}
\ No newline at end of file diff --git a/Homework 8/MyStructures/Contact.hpp b/Homework 8/MyStructures/Contact.hpp index e079797..26a0fcd 100644 --- a/Homework 8/MyStructures/Contact.hpp +++ b/Homework 8/MyStructures/Contact.hpp @@ -1,118 +1,294 @@ #ifndef CONTACT_HPP #define CONTACT_HPP +using std::cout; +using std::endl; + namespace MyStructures + { - template<typename T> + class Contact { public: - Contact() = default; - Contact(const char* name, short age); + Contact(); + ~Contact(); - ~Contact() = default; + Contact(const Contact& copy)noexcept(false); + Contact& operator=(const Contact& rhs)noexcept(false); - Contact(const Contact& copy); - Contact& operator=(const Contact& rhs); + Contact(Contact&& move) noexcept; + Contact& operator=(Contact&& rhs) noexcept; - Contact(Contact&& move); - Contact& operator=(Contact&& rhs); + void Print(); - short GetAge(); - const char* GetName(); + const char* FirstName() const; + void FirstName(char* firstName); + void FirstName(const char* firstName) const; - void SetAge(short age); - void SetName(const char* name); + const char* LastName() const; + void LastName(char* lastName); + void LastName(const char* lastName) const; - T GetCustomValue(); - void SetCustomValue(T& newValue); + const char* StreetAddress() const; + void StreetAddress(char* streetAddress); + void StreetAddress(const char* streetAddress)const; - void Print(); + const char* City() const; + void City(char* city); + void City(const char* City)const; - private: - const char* name_{}; - short age_{ 0 }; - T custom_value_{}; + const char* State() const; + void State(char* state); + void State(const char* state)const; + + const char* Email() const; + void Email(char* email); + void Email(const char* email)const; + + int Zip() const; + void Zip(int zip); + bool operator!=(const Contact& rhs) const; + bool operator==(const Contact& rhs) const; + + private: + char* firstName_{ new char[MAX_CHAR] }; + char* lastName_{ new char[MAX_CHAR] }; + char* streetAddress_{ new char[MAX_CHAR] }; + char* city_{ new char[MAX_CHAR] }; + char* state_{ new char[MAX_CHAR] }; + char* email_{ new char[MAX_CHAR] }; + int zip_{ 0 }; }; -} -template <typename T> -Contact<T>::Contact(const char* name, short age) -{ -} -template <typename T> -Contact<T>::Contact(const Contact& copy) -{ -} -template <typename T> -Contact<T>& Contact<T>::operator=(const Contact& rhs) -{ -} -template <typename T> -Contact<T>::Contact(Contact&& move) -{ -} + inline Contact::Contact() + { + memset(firstName_, 0, MAX_CHAR); -template <typename T> -Contact<T>& Contact<T>::operator=(Contact&& rhs) -{ + memset(lastName_, 0, MAX_CHAR); -} + memset(streetAddress_, 0, MAX_CHAR); -template <typename T> -short Contact<T>::GetAge() -{ + memset(city_, 0, MAX_CHAR); -} + memset(state_, 0, MAX_CHAR); -template <typename T> -const char* Contact<T>::GetName() -{ + memset(email_, 0, MAX_CHAR); -} + } -template <typename T> -void Contact<T>::SetAge(short age) -{ -} + inline Contact::~Contact() + { + delete[] firstName_; + delete[] lastName_; + delete[] streetAddress_; + delete[] city_; + delete[] state_; + delete[] email_; + } + inline Contact::Contact(const Contact& copy) noexcept(false) + { + OverwriteCharArray(firstName_, rhs.firstName_); + OverwriteCharArray(lastName_, rhs.lastName_); + OverwriteCharArray(streetAddress_, rhs.streetAddress_); + OverwriteCharArray(city_, rhs.city_); + OverwriteCharArray(state_, rhs.state_); + OverwriteCharArray(email_, rhs.email_); + zip_ = rhs.zip_; + } -template<typename T> -void Contact<T>::SetName(const char* name) -{ -} + inline Contact& Contact::operator=(const Contact& rhs) noexcept(false) + { + if (this != &rhs) + { + OverwriteCharArray(firstName_, rhs.firstName_); + OverwriteCharArray(lastName_, rhs.lastName_); + OverwriteCharArray(streetAddress_, rhs.streetAddress_); + OverwriteCharArray(city_, rhs.city_); + OverwriteCharArray(state_, rhs.state_); + OverwriteCharArray(email_, rhs.email_); + zip_ = rhs.zip_; + } + return *this; + } -template<typename T> -T Contact<T>::GetCustomValue() -{ - return custome_value_; -} + inline Contact::Contact(Contact&& move) noexcept + { + *this = std::move(move); + } -template<typename T> -void Contact<T>::SetCustomValue(T& newValue) -{ - custom_value_ = newValue; -} + inline Contact& Contact::operator=(Contact&& rhs) noexcept + { + if (this != &rhs) + { + firstName_ = rhs.firstName_; + lastName_ = rhs.lastName_; + streetAddress_ = rhs.streetAddress_; + city_ = rhs.city_; + state_ = rhs.state_; + email_ = rhs.email_; + zip_ = rhs.zip_; + } + } + + + inline void Contact::Print() + { + cout << firstName_ << " , " << lastName_ << endl; + cout << streetAddress_ << endl; + cout << city_ << " , " << state_ << " " << zip_ << endl << endl; + } + + + inline const char* Contact::FirstName() const + { + return firstName_; + } + + + inline void Contact::FirstName(char* firstName) + { + firstName_ = firstName; + } + + + inline void Contact::FirstName(const char* firstName) const + { + OverwriteCharArray(firstName_, firstName); + } + + + inline const char* Contact::LastName() const + { + return lastName_; + } + + + inline void Contact::LastName(char* lastName) + { + lastName_ = lastName; + } -template<typename T> -void Contact<T>::Print() + inline void Contact::LastName(const char* lastName) const { - std::cout << "Name: " << name_ << std:: endl; - std::cout << "Age: " << age_ << std::endl; + OverwriteCharArray(lastName_, lastName); } + inline void Contact::StreetAddress(char* streetAddress) + { + streetAddress_ = streetAddress; + } + + + inline void Contact::StreetAddress(const char* streetAddress) const + { + OverwriteCharArray(streetAddress_, streetAddress); + } + + + inline const char* Contact::City() const + { + return city_; + } + + inline void Contact::City(char* city) + { + city_ = city; + } + + + inline void Contact::City(const char* city) const + { + OverwriteCharArray(city_, city); + } + + + inline void Contact::State(const char* state) const + { + OverwriteCharArray(state_, state); + } + + + inline void Contact::Email(const char* email) const + { + OverwriteCharArray(email_, email); + } + + + inline const char* Contact::State() const + { + return state_; + } + + + inline void Contact::State(char* state) + { + state_ = state; + } + + + inline const char* Contact::Email() const + { + return email_; + } + + + inline void Contact::Email(char* email) + { + email_ = email; + } + + + inline int Contact::Zip() const + { + return zip_; + } + + + inline void Contact::Zip(int zip) + { + zip_ = zip; + } + + inline bool Contact::operator!=(const Contact& rhs) const + { + if (std::strcmp(firstName_, rhs.firstName_) != 0) { return true; } + 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 (zip_ != rhs.zip_) { return true; } + + return false; + + } + inline bool Contact::operator==(const Contact& rhs) const + { + if (std::strcmp(firstName_, rhs.firstName_) != 0) { return false; } + 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(email_, rhs.email_) != 0) { return false; } + if (zip_ != rhs.zip_) { return true; } + + return true; + } + #endif
\ No newline at end of file diff --git a/Homework 8/MyStructures/ContactList.hpp b/Homework 8/MyStructures/ContactList.hpp index 0cee43f..887ebfc 100644 --- a/Homework 8/MyStructures/ContactList.hpp +++ b/Homework 8/MyStructures/ContactList.hpp @@ -15,6 +15,7 @@ namespace MySStructures 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(const C* storage, const size_t& length); ~ContactList(); ContactList(const ContactList& copy); //deep copy constructor @@ -38,6 +39,7 @@ namespace MySStructures 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 C& operator[](const int& index); //returns contact reference via [] overload @@ -46,7 +48,7 @@ namespace MySStructures bool operator==(const ContactList<C>& rhs) const; //if (newContact == old Contact) bool Empty() const; //is the list empty - + bool Full() const; //returns true if all allocated @@ -65,12 +67,13 @@ namespace MySStructures contacts_ = AllocateContactList(length); } + template <class C> ContactList<C>::ContactList(const C* contacts, const size_t& length) { contacts_ = AllocateContactList(length); - for (auto i = 0u; i < length; ++i) + for (auto i = 0u; i < size_; ++i) { contacts_[i] = contacts[i]; } @@ -86,13 +89,15 @@ namespace MySStructures template <class C> ContactList<C>::ContactList(const ContactList& copy) { - length_ = copy.length_; + delete[] contacts_; + contacts_ = nullptr; + + auto temp = ContactList<C>{ copy.contacts_, copy.length_ }; + + *this = std::move(temp); + size_ = copy.size_; - contacts_ = AllocateContactList(copy.length_); - for (auto i = 0u; i < length_; ++i) - { - contacts_[i] = copy[i]; - } + } template <class C> @@ -101,15 +106,14 @@ namespace MySStructures if (this != &rhs) { delete[] contacts_; + contacts_ = nullptr; - contacts_ = AllocateContactsList(rhs.length_); - size_ = rhs.size_; + auto temp = ContactList<C>{ rhs.contacts_, rhs.length_ }; - for (auto i = 0u; i < length_; ++i) - { - contacts_[i] = copy[i]; - } + *this = std::move(temp); + + size_ = rhs.size_; } return *this; } @@ -121,7 +125,7 @@ namespace MySStructures } template <class C> - ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) + ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) noexcept { if (this != &rhs) { @@ -151,7 +155,7 @@ namespace MySStructures template <class C> size_t ContactList<C>::Size() const { - + return size_; } template <class C> @@ -176,7 +180,8 @@ namespace MySStructures if (size_ >= length_) { //increase size of array - *this = ContactList(contacts_, length_ * 2); + ContactList newContacts(contacts_, length_ * 2); + *this = newContacts } @@ -193,16 +198,21 @@ namespace MySStructures Append(data); return; } + + if (!Full()) { - for (auto i = size_ - 1; i >= 0; i--) + for (auto i = size_-1; i >= 0; i--) { - contacts_[i + 1] = contacts_[if;] + contacts_[i + 1] = contacts_[i]; } - } - auto temp = ContactList<C>(length_); + contacts_[0] = data; + return; + } + + auto temp = ContactList<C>(length_ * 2); temp.Append(data); - for (auto i = 0; i < size_; ++i) + for (auto i = 0; i < size_ ++i) { temp.Append(contacts_[i]); } @@ -210,6 +220,8 @@ namespace MySStructures contacts_ = nullptr; *this = std::move(temp); + + } template <class C> @@ -230,14 +242,14 @@ namespace MySStructures } - template <class C> - void ContactList<C>::InsertAfter(const C& data const C& data) + template<class C> + inline void ContactList<C>::InsertAfter(const C& data, const C& after) { } - template <class C> - void ContactList<C>::InsertBefore(const C& data const C& data) + template<class C> + inline void ContactList<C>::InsertBefore(const C& data, const C& before) { } @@ -275,6 +287,12 @@ namespace MySStructures return contacts_[0]; } + template<class C> + inline C& ContactList<C>::operator[](const int& index) + { + // TODO: insert return statement here + } + template <class C> C ContactList<C>::operator[](const int& index) const { @@ -292,7 +310,7 @@ namespace MySStructures template <class C> bool ContactList<C>::operator==(const ContactList<C>& rhs) const { - //TODO check both arrays to see if equivalent + //Performance //shortcut easy ways of knowing the same if (contacts_ == rhs.contacts_)return true; @@ -314,6 +332,14 @@ namespace MySStructures } template <class C> + bool ContactList<C>::Full() const + { + if (size_ < length_) return false; + + return true; + } + + template <class C> C* ContactList<C>::AllocateContactList(const size_t& length) { C* storage = nullptr; diff --git a/Homework 8/MyStructures/MyStructures.vcxproj b/Homework 8/MyStructures/MyStructures.vcxproj index ee62017..e2a9274 100644 --- a/Homework 8/MyStructures/MyStructures.vcxproj +++ b/Homework 8/MyStructures/MyStructures.vcxproj @@ -127,6 +127,7 @@ </Link> </ItemDefinitionGroup> <ItemGroup> + <ClInclude Include="CharArrayHelpers.hpp" /> <ClInclude Include="Contact.hpp" /> <ClInclude Include="ContactList.hpp" /> </ItemGroup> diff --git a/Homework 8/MyStructures/MyStructures.vcxproj.filters b/Homework 8/MyStructures/MyStructures.vcxproj.filters index deeda6d..a700ddf 100644 --- a/Homework 8/MyStructures/MyStructures.vcxproj.filters +++ b/Homework 8/MyStructures/MyStructures.vcxproj.filters @@ -3,5 +3,6 @@ <ItemGroup> <ClInclude Include="Contact.hpp" /> <ClInclude Include="ContactList.hpp" /> + <ClInclude Include="CharArrayHelpers.hpp" /> </ItemGroup> </Project>
\ No newline at end of file |