diff options
| author | Yana Blashchishina <[email protected]> | 2024-03-11 16:43:04 -0700 |
|---|---|---|
| committer | Yana Blashchishina <[email protected]> | 2024-03-11 16:43:04 -0700 |
| commit | 88b1c26f4066734637853a90fcfd8f9eeb48ebed (patch) | |
| tree | 7ebc96e79f323b2c17b555569211d6d7242e8ff7 | |
| parent | CharArrays (diff) | |
| download | homework-8-yanablash-88b1c26f4066734637853a90fcfd8f9eeb48ebed.tar.xz homework-8-yanablash-88b1c26f4066734637853a90fcfd8f9eeb48ebed.zip | |
errors
| -rw-r--r-- | Homework 8/Homework 8/main.cpp | 91 | ||||
| -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 |
4 files changed, 158 insertions, 43 deletions
diff --git a/Homework 8/Homework 8/main.cpp b/Homework 8/Homework 8/main.cpp index 78ee54f..d26e2ed 100644 --- a/Homework 8/Homework 8/main.cpp +++ b/Homework 8/Homework 8/main.cpp @@ -8,9 +8,8 @@ #include "Contact.hpp" #include "ContactList.hpp" -sing namespace MyStructures; -using std::cout; -using std::cin; +using namespace MyStructures; + Contact NewContact(); void Prompt(); @@ -26,23 +25,29 @@ int main() { contact00.Zip(97089); contact00.Email("[email protected]"); + const Contact contact0 = contact00; + 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]"); + contact01.FirstName("Vlada"); + contact01.LastName("S"); + contact01.StreetAddress("12332 Ankey Woods"); + contact01.City("Portland"); + contact01.State("OR"); + contact01.Zip(97220); + contact01.Email("[email protected]"); + + const Contact contact1 = contact01; 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]"); + contact02.FirstName("Permata"); + contact02.LastName("Helmy"); + contact02.StreetAddress("123 Seasame Street"); + contact02.City("Lawrenceville"); + contact02.State("GA"); + contact02.Zip(30043); + contact02.Email("[email protected]"); + + const Contact contact2 = contact02; ContactList<Contact> addressBook(3); @@ -74,10 +79,42 @@ int main() { addressBook.Prepend(contact01); addressBook.Prepend(contact02); - addressBook[0] == contact02; + if (addressBook[0] == contact02) + { + cout << "The contacts are the same.\n"; + } + + if (addressBook[1] == contact01) + { + cout << "The contacts are the same.\n"; + } + + if (addressBook[2] == contact00) + { + cout << "The contacts are the same.\n"; + } + + addressBook.RemoveLast(); + + if (addressBook.Size() != 3) + { + cout << "Removelast did not work right\n"; + } + + addressBook.Append(contact00); + + if (addressBook.Size() == 4 && addressBook[3] == contact00) + { + cout << "Removelast did work\n"; + } + Contact newContact; + if (addressBook[5] != newContact) + { + + } @@ -96,11 +133,25 @@ int main() { addressBook.Print(); break; case 'X': + break; default: cout << "Bad Input" << endl; } - } + } while (input != 'x'); - return 0; + return 0 } +Contact NewContact() +{ + Contact newContact(); + + newContact.FirstName(PromptInput("First name:", MAX_CHAR)); + newContact.LastName(PromptInput("Last name: ", MAX_CHAR)); + newContact.StreetAddress(PromptInput("First name:", MAX_CHAR)); + newContact.City(PromptInput("First name: ", MAX_CHAR)); + newContact.State(PromptInput("First name: ", MAX_CHAR)); + newContact.Zip(ReadInt("Zip code: ")); + newContact.Email(PromptInput("First name: ", MAX_CHAR)); + +} 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]; } |