aboutsummaryrefslogtreecommitdiff
path: root/Homework8/MyStructures
diff options
context:
space:
mode:
authorrPatrickWarner <[email protected]>2024-03-07 14:47:52 -0800
committerrPatrickWarner <[email protected]>2024-03-07 14:47:52 -0800
commit15dea25af082591f36c3036df04bc06113549385 (patch)
tree868f3467b672262311c9bd1c08203cba51d45eb1 /Homework8/MyStructures
parentchanges (diff)
downloadhomework-8-reecepwarner-15dea25af082591f36c3036df04bc06113549385.tar.xz
homework-8-reecepwarner-15dea25af082591f36c3036df04bc06113549385.zip
more changes/coming along
Diffstat (limited to 'Homework8/MyStructures')
-rw-r--r--Homework8/MyStructures/Contact.hpp78
-rw-r--r--Homework8/MyStructures/ContactList.hpp129
-rw-r--r--Homework8/MyStructures/MenuHelper.hpp20
3 files changed, 147 insertions, 80 deletions
diff --git a/Homework8/MyStructures/Contact.hpp b/Homework8/MyStructures/Contact.hpp
index ac480be..fdc2921 100644
--- a/Homework8/MyStructures/Contact.hpp
+++ b/Homework8/MyStructures/Contact.hpp
@@ -22,7 +22,7 @@ namespace MyStructures //variables declared here act as a global
Contact() = default;
Contact(char* name, const short age);
- ~Contact();
+ ~Contact() = default;
Contact(const Contact& copy); //copy constructor
Contact& operator=(const Contact& rhs); //copy assignment
@@ -52,10 +52,9 @@ namespace MyStructures //variables declared here act as a global
int GetZip();
void SetZip(int zip);
- /*int InputInt(const char* prompt);*/
- /*char* PromptCharInput(const char* prompt, long long maxlen);*/
-
+
bool operator != (const Contact & rhs) const;
+ bool operator == (const Contact& rhs) const;
void Print() const;
@@ -80,17 +79,11 @@ namespace MyStructures //variables declared here act as a global
}
- inline Contact::~Contact()
- {
+ //inline Contact::~Contact()
+ //{
- delete[] firstname_;
- delete[] lastname_;
- delete[] email_;
- delete[] street_;
- delete[] state_;
- delete[] city_;
-
- }
+
+ //}
inline Contact::Contact(const Contact& copy)
@@ -134,7 +127,7 @@ namespace MyStructures //variables declared here act as a global
inline Contact::Contact(Contact&& move)
{
- *this = move;
+ *this = std::move(move);
}
@@ -225,49 +218,6 @@ namespace MyStructures //variables declared here act as a global
}
- /*inline int Contact::InputInt(const char* prompt)
- {
- std::cout << prompt << std::endl;
-
- std::cout.flush();
-
- int data = 0;
- std::cin >> data;
-
- while (!std::cin)
- {
- std::cout << prompt << std::endl;
- std::cin.clear();
-
- cin.ignore(MAX_STREAM_SIZE, '\n');
- std::cin >> data;
-
- }
-
- return data;
- }*/
-
- //inline char* Contact::PromptCharInput(const char* prompt, long long maxlen)
- //{
- // char* input = new char[maxlen];
- // std::cout.flush();
-
- // std::cout << prompt << std::endl;
- // cin.get(input, maxlen, '\n');
- // cin.ignore(MAX_STREAM_SIZE, '\n');
- //
- // while (!std::cin)
- // {
- // std::cout << prompt << std::endl;
-
- // std::cin.clear();
- //
- // cin.get(input, maxlen, '\n');
-
- // }
- //
- // return input;
- //}
inline bool Contact::operator!=(const Contact& rhs) const
{
@@ -282,6 +232,18 @@ namespace MyStructures //variables declared here act as a global
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(email_, rhs.email_) != 0) { return false; }
+ if (std::strcmp(street_, rhs.street_) != 0) { return false; }
+ if (std::strcmp(state_, rhs.state_) != 0) { return false; }
+ if (std::strcmp(city_, rhs.city_) != 0) { return false; }
+ if (zip_ != rhs.zip_) { return false; }
+ return true;
+ }
+
void Contact::Print() const
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp
index e7959a8..bc2240f 100644
--- a/Homework8/MyStructures/ContactList.hpp
+++ b/Homework8/MyStructures/ContactList.hpp
@@ -31,8 +31,8 @@ namespace MyStructures
void Extract(const C& data); //delete this data
void InsertAfter(const C& data, const C& after); //Insert contact data after 'after'
void InsertBefore(const C& data, const C& before); //Insert contact data before 'before'
- void Clear(); // //deletes all contacts, empties list, nothing left
-
+ void Clear(); //deletes all contacts, empties list, nothing left
+ void ContactDeleter(const size_t& size);
C& Last(); //returning a reference to contact, it gives last contact in list
C Last() const; //const version of this, unchanging because its const and its a copy
@@ -47,7 +47,7 @@ namespace MyStructures
bool operator==(const ContactList<C>& rhs) const; //overloading == e.g if(newcontactlist == oldcontactlist)
bool Empty() const; //is the list empty
-
+ bool Full() const; //returns true if allocated
private:
C* contacts_{ nullptr };
size_t length_{ 0 }; //entire length of array
@@ -66,7 +66,7 @@ namespace MyStructures
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];
}
@@ -76,19 +76,30 @@ namespace MyStructures
{
delete[] contacts_;
contacts_ = nullptr;
+
+
}
template<class C>
ContactList<C>::ContactList(const ContactList& copy)
{
- length_ = copy.length_;
+ //length_ = copy.length_;
+ //size_ = copy.size_;
+ //contacts_ = AllocateContactList(copy.length_);
+
+ //contacts_ = AllocateContactList(length_);
+ //for (auto i = 0u; i < length_; ++i)
+ //{
+ // contacts_[i] = copy[i];
+ //}
+
+ delete[] contacts_;
+ contacts_ = nullptr;
+
+ auto temp = ContactList<C>{ copy.contacts_, copy.length_ };
+ *this = std::move(temp);
size_ = copy.size_;
- contacts_ = AllocateContactList(copy.length_);
- contacts_ = AllocateContactList(length_);
- for (auto i = 0u; i < length_; ++i)
- {
- contacts_[i] = copy[i];
- }
+
}
template<class C>
@@ -145,7 +156,7 @@ namespace MyStructures
template<class C>
void ContactList<C>::PrintList() const
{
- for (auto i = 0u; i < length_; ++i)
+ for (auto i = 0u; i < size_; ++i)
{
contacts_[i].Print();
}
@@ -162,6 +173,7 @@ namespace MyStructures
{
C* storage = nullptr;
length_ = length;
+
storage = new C[length]{};
return storage;
@@ -193,15 +205,78 @@ namespace MyStructures
template<class C>
void ContactList<C>::Prepend(const C& data)
{
+ //data goes to element zero
+
+ if (Empty())
+ {
+ if (contacts_ == nullptr)
+ {
+
+ }
+
+ Append(data);
+ size_++;
+ return;
+ }
+ //move all elements +1 element
+ //check for sizing
+ //check for length
+ if (!Full())
+ {
+ for (auto i = 0; i<size_; ++i)
+ {
+ contacts_[i+1] = contacts_[i];
+ }
+ contacts_[0] = data;
+ size_++;
+ return;
+ }
+ if (Full())
+ {
+ auto temp = ContactList<C>{length_};
+ temp.Append(data);
+ for (auto i = 0; i <= size_; ++i)
+ {
+ temp.Append(contacts_[i]);
+ }
+
+ delete[] contacts_;
+ contacts_ = nullptr;
+
+ *this = std::move(temp);
+ }
+ size_++;
+
}
template<class C>
void ContactList<C>::RemoveLast()
{
+ if (Empty()) return;
+
+ //clear the last member
+ ContactDeleter(size_ - 1);
+
+ size_--;
+
}
template<class C>
void ContactList<C>::RemoveFirst()
{
+ if (Empty()) return;
+
+ //clear the first member
+ ContactDeleter(0);
+ //shift so the program doesn't throw in the print function
+ for (auto i = 0u; i < size_; i++)
+ {
+ contacts_[i] = contacts_[i + 1];
+ }
+ //just to eliminate the last cell from the copy assignment above^^^^
+ ContactDeleter(size_ - 1);
+ size_--;
+
+
}
template<class C>
void ContactList<C>::Extract(const C& data)
@@ -218,10 +293,27 @@ namespace MyStructures
template<class C>
void ContactList<C>::Clear()
{
+ size_ = 0;
+ length_ = 0;
+ delete[] contacts_;
+ contacts_ = nullptr;
}
template<class C>
+ inline void ContactList<C>::ContactDeleter(const size_t& size)
+ {
+ contacts_[size].SetFirstName(nullptr);
+ contacts_[size].SetLastName(nullptr);
+ contacts_[size].SetEmail(nullptr);
+ contacts_[size].SetStreet(nullptr);
+ contacts_[size].SetState(nullptr);
+ contacts_[size].SetCity(nullptr);
+ contacts_[size].SetZip(0);
+ }
+
+ template<class C>
inline C& ContactList<C>::Last()
{
+ return contacts_[size_-1];
// TODO: insert return statement here
}
template<class C>
@@ -258,11 +350,11 @@ namespace MyStructures
template<class C>
inline bool ContactList<C>::operator==(const ContactList<C>& rhs) const
{
- if (contacts_ == rhs.contacts_) return true;
+ if (&contacts_ == &rhs.contacts_) return true;
- if (size_ != rhs.size_) return false;
+ if (&size_ != &rhs.size_) return false;
- if (length_ != rhs.length_) return false;
+ if (&length_ != &rhs.length_) return false;
for (auto i = 0u; i < size_; ++i)
{
@@ -275,6 +367,13 @@ namespace MyStructures
{
return size_ == 0;
}
+ template<class C>
+ bool ContactList<C>::Full() const
+ {
+ if (size_ < length_) return false;
+
+ return true;
+ }
};
diff --git a/Homework8/MyStructures/MenuHelper.hpp b/Homework8/MyStructures/MenuHelper.hpp
index 81d11e0..f13656e 100644
--- a/Homework8/MyStructures/MenuHelper.hpp
+++ b/Homework8/MyStructures/MenuHelper.hpp
@@ -21,7 +21,7 @@ Contact NewContact();
void PrintContact(ContactList<Contact>& contacts);
-
+bool OverWriteCharArray();
void MainMenu()
{
@@ -84,6 +84,11 @@ void PrintContact(ContactList<Contact>& contacts)
contacts.PrintList();
}
+inline bool OverWriteCharArray()
+{
+ return false;
+}
+
Contact NewContact()
@@ -101,14 +106,15 @@ char* PromptCharInput(const char* prompt, long long maxlen)
std::cout << prompt << std::endl;
char* input = new char[maxlen];
-
- do
+ cin >> input;
+ while (!std::cin)
{
- std::cin.clear();
- cin.get(input, maxlen, '\n');
- cin.ignore(MAX_STREAM_SIZE, '\n');
+ cout << prompt << std::endl;
+ cin.clear();
- } while (!std::cin);
+ cin.ignore(MAX_STREAM_SIZE, '\n');
+ cin.get(input, MAX_STREAM_SIZE, '\n');
+ }
return input;