aboutsummaryrefslogtreecommitdiff
path: root/Homework8
diff options
context:
space:
mode:
authorrPatrickWarner <[email protected]>2024-03-11 20:44:18 -0700
committerrPatrickWarner <[email protected]>2024-03-11 20:44:18 -0700
commit7447eb346f8029d1f972e605c1b1ae0917836bb1 (patch)
tree7de35f759150f15ac401633f9d8c7e4ba7a0f14b /Homework8
parentsome changes (diff)
downloadhomework-8-reecepwarner-7447eb346f8029d1f972e605c1b1ae0917836bb1.tar.xz
homework-8-reecepwarner-7447eb346f8029d1f972e605c1b1ae0917836bb1.zip
all of the functions are complete
Diffstat (limited to 'Homework8')
-rw-r--r--Homework8/Homework8/main.cpp55
-rw-r--r--Homework8/MyStructures/ContactList.hpp167
-rw-r--r--Homework8/MyStructures/MenuHelper.hpp32
3 files changed, 162 insertions, 92 deletions
diff --git a/Homework8/Homework8/main.cpp b/Homework8/Homework8/main.cpp
index f7185b9..ae883e9 100644
--- a/Homework8/Homework8/main.cpp
+++ b/Homework8/Homework8/main.cpp
@@ -13,60 +13,7 @@ using namespace MyStructures;
int main()
{
- Contact NewContact;
- NewContact.SetFirstName(PromptCharInput("Whats your first name?", 101));
- NewContact.SetLastName(PromptCharInput("Whats your last name?", 101));
- NewContact.SetEmail(PromptCharInput("Whats your email?", 101));
- NewContact.SetStreet(PromptCharInput("Whats your street address?", 101));
- NewContact.SetState(PromptCharInput("What state do you live in?", 101));
- NewContact.SetCity(PromptCharInput("What city do you live in?", 101));
- NewContact.SetZip(InputInt("What is your zip code?"));
- ContactList<Contact> AddressBook(3);
-
- AddressBook.Append(NewContact);
-
- Contact NextContact;
- NextContact.SetFirstName(PromptCharInput("Whats your first name?", 101));
- NextContact.SetLastName(PromptCharInput("Whats your last name?", 101));
- NextContact.SetEmail(PromptCharInput("Whats your email?", 101));
- NextContact.SetStreet(PromptCharInput("Whats your street address?", 101));
- NextContact.SetState(PromptCharInput("What state do you live in?", 101));
- NextContact.SetCity(PromptCharInput("What city do you live in?", 101));
- NextContact.SetZip(InputInt("What is your zip code?"));
- AddressBook.Prepend(NextContact);
-
-
- Contact AnothaContact;
- AnothaContact.SetFirstName(PromptCharInput("Whats your first name?", 101));
- AnothaContact.SetLastName(PromptCharInput("Whats your last name?", 101));
- AnothaContact.SetEmail(PromptCharInput("Whats your email?", 101));
- AnothaContact.SetStreet(PromptCharInput("Whats your street address?", 101));
- AnothaContact.SetState(PromptCharInput("What state do you live in?", 101));
- AnothaContact.SetCity(PromptCharInput("What city do you live in?", 101));
- AnothaContact.SetZip(InputInt("What is your zip code?"));
- AddressBook.Append(AnothaContact);
-
- AddressBook.PrintList();
-
-
-
- AddressBook.RemoveFirst();
-
-
-
- AddressBook.PrintList();
-
- if (AddressBook[0] == NewContact)
- {
- cout << "They are the same";
- }
-
-
-
- //MainMenu();
-
-
-
+ MainMenu();
return 0;
} \ No newline at end of file
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp
index b324064..a2d1efa 100644
--- a/Homework8/MyStructures/ContactList.hpp
+++ b/Homework8/MyStructures/ContactList.hpp
@@ -28,17 +28,18 @@ namespace MyStructures
void Prepend(const C& data); //add contact as the first item
void RemoveLast(); //remove last
void RemoveFirst(); //remove first
- void Extract(const C& data); //delete this data, slash collecting its data
+ void Extract(const C& data); //delete this data, slash collecting its data ;;return 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 ContactDeleter(const size_t& size);
+ void DeleteContact(const size_t& choice);
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
C& First(); //returning first contact in list
C First() const; //returning first contact copy
-
+ void ContactDoubler();
C& operator[](const int& index); //like a getter/ returns contact reference via squarebracket overload
C operator[](const int& index) const; //returns a contact copy via squarebracket overload
@@ -158,7 +159,9 @@ namespace MyStructures
{
for (auto i = 0u; i < size_; ++i)
{
+
contacts_[i].Print();
+
}
}
@@ -194,13 +197,25 @@ namespace MyStructures
index_locate = size_;
}
- if (size_ >= length_)
+ if (Full())
{
- *this = ContactList(contacts_, length_ * 2);
+ auto temp = ContactList<C>{ length_ * 2 };
+ for (auto i = 0; i <= size_; ++i)
+ {
+ temp[i] = contacts_[i];
+ }
+ temp[size_] = data;
+
+ delete[] contacts_;
+ contacts_ = nullptr;
+
+ *this = std::move(temp);
+ size_+=4;
+ return;
}
//Add to end of the array
contacts_[index_locate] = data;
- size_++;
+ ++size_;
}
template<class C>
void ContactList<C>::Prepend(const C& data)
@@ -228,7 +243,7 @@ namespace MyStructures
}
if (Full())
{
- auto temp = ContactList<C>{length_};
+ auto temp = ContactList<C>{length_*2};
temp.Append(data);
for (auto i = 0; i <= size_; ++i)
{
@@ -276,19 +291,113 @@ namespace MyStructures
template<class C>
void ContactList<C>::Extract(const C& data)
{
+ if (Empty())return;
+ auto tempo = ContactList<C>{ length_ };
+ for (auto i = 0u; i < size_; i++)
+ {
+ if (contacts_[i] == data)
+ {
+ tempo.Append(data); //the extraction
+ ContactDeleter(i);
+ for (auto j = size_; j > i; j--)
+ {
+ contacts_[j - 1] = contacts_[j];
+ }
+
+ }
+
+ }
+ size_--;
+ //find where data is in the contacts
+ //extract data
+ //remove data
+ //decrement size after
+ //if there are elements above data, shift down after
+
+
}
template<class C>
void ContactList<C>::InsertAfter(const C& data, const C& after)
{
- if (Empty()) return;
-
- //if full, double size
-
+ if (Empty()) Append(data); return;
+ if (Full()) ContactDoubler();
+
+ if (after == contacts_[0]) //checks if after is the first contact
+ {
+ for (auto i = 1u; i < size_; i++)
+ {
+ contacts_[i + 1] = contacts_[i]; // shift the contacts up to make space for the incoming data
+ }
+ contacts_[1] = data;
+ size_++;
+ return;
+ }
+
+ if (after == contacts_[size_ - 1]) //checks if it is after the last contact
+ {
+ contacts_[size_] = data;
+ size_++;
+ return;
+ }
+
+ else
+ {
+ for (auto i = size_-1; i>0; i--) //shifts the content after "after"
+ {
+ if (contacts_[i] != after)
+ {
+ contacts_[i + 1] = contacts_[i];
+ }
+ }
+
+ for (auto j = 0u; j < size_; j++) //assigns the data after "after"
+ {
+ if (contacts_[j] == after)
+ {
+ contacts_[j + 1] = data;
+ }
+ }size_++;
+ return;
+ }
}
+
template<class C>
void ContactList<C>::InsertBefore(const C& data, const C& before)
{
+ if (Empty()) Append(data); return;
+ bool found = false;
+ for (auto i = 0u; i < size_ ; i++)
+ {
+ found = true;
+ if (contacts_[i] == before)
+ {
+ switch (i)
+ {
+ default:
+ auto temp = ContactList<C>{ length_ };
+ for (auto j = 0u; j < i; ++j)
+ {
+ temp.Append(contacts_[j]);
+ }
+ temp.Append(data);
+ for (auto k = i; k < size_; ++k)
+ {
+ temp.Append(contacts_[k]);
+ }
+ delete[] contacts_;
+ contacts_ = nullptr;
+ *this = std::move(temp);
+ }
+ }
+ }
+
+ if (!found)
+ {
+ cout << "Error: Before data not found" << endl;
+ }
+
}
+
template<class C>
void ContactList<C>::Clear()
{
@@ -297,6 +406,7 @@ namespace MyStructures
delete[] contacts_;
contacts_ = nullptr;
}
+
template<class C>
inline void ContactList<C>::ContactDeleter(const size_t& size)
{
@@ -310,21 +420,37 @@ namespace MyStructures
}
template<class C>
+ inline void ContactList<C>::DeleteContact(const size_t& choice)
+ {
+ ContactDeleter(choice);
+ for (auto i = size_; i >= choice; i--)
+ {
+ contacts_[i+1] = contacts_[i];
+ }
+ size_--;
+
+ }
+
+
+
+ template<class C>
inline C& ContactList<C>::Last()
{
return contacts_[size_-1];
- // TODO: insert return statement here
+
}
+
template<class C>
inline C ContactList<C>::Last() const
{
return contacts_[size_-1];
}
+
template<class C>
inline C& ContactList<C>::First()
{
return contacts_[0];
- // TODO: insert return statement here
+
}
template<class C>
inline C ContactList<C>::First() const
@@ -332,6 +458,20 @@ namespace MyStructures
return contacts_[0];
}
template<class C>
+ inline void ContactList<C>::ContactDoubler()
+ {
+ auto temp = ContactList<C>{ length_ * 2 }; //doubles the length
+ for (auto i = 0u; i <= size_; ++i)
+ {
+ temp.Append(contacts_[i]);
+ }
+ delete[] contacts_;
+ contacts_ = nullptr;
+
+ *this = std::move(temp);
+ size_--;
+ }
+ template<class C>
inline C& ContactList<C>::operator[](const int& index)
{
if (index < 0 || index >= static_cast<int>(length_))
@@ -341,7 +481,7 @@ namespace MyStructures
return EmptyContact;
}
return contacts_[index];
- // TODO: insert return statement here
+
}
template<class C>
inline C ContactList<C>::operator[](const int& index) const
@@ -375,6 +515,7 @@ namespace MyStructures
}
return true;
}
+
template<class C>
inline bool ContactList<C>::Empty() const
{
diff --git a/Homework8/MyStructures/MenuHelper.hpp b/Homework8/MyStructures/MenuHelper.hpp
index bb5a70d..858f6e3 100644
--- a/Homework8/MyStructures/MenuHelper.hpp
+++ b/Homework8/MyStructures/MenuHelper.hpp
@@ -14,19 +14,15 @@ namespace MyStructures
int InputInt(const char* prompt);
- void PrintContact(ContactList<Contact>& contacts);
-
Contact NewContact();
- void PrintContact(ContactList<Contact>& contacts);
- bool OverWriteCharArray();
void MainMenu()
{
char Options = '\0';
-
-
+ size_t i = 0;
+ ContactList<Contact> AddressBook(3);
do
{
std::cout << "Welcome to the contacts menu\n"
@@ -38,13 +34,15 @@ namespace MyStructures
switch (Options)
{
case('1'):
-
+ AddressBook.Append(InputContact());
break;
case('2'):
- //PrintContact(contacts);
+ AddressBook.PrintList();
break;
case('3'):
- //contacts.DeleteContact("Which contact would you like to delete?", contacts);
+ cout << "Which contact would you like to delete?:";
+ cin >> i;
+ AddressBook.DeleteContact(i);
break;
case('4'):
std::cout << "Thank you, have a great day!" << std::endl;
@@ -60,7 +58,6 @@ namespace MyStructures
Contact InputContact()
{
-
Contact newContact;
newContact.SetFirstName(PromptCharInput("What is your first name?", 101));
@@ -71,25 +68,10 @@ namespace MyStructures
newContact.SetCity(PromptCharInput("What is your city?: ", 101));
newContact.SetZip(InputInt("What is your zip?"));
-
return newContact;
-
-
}
- void PrintContact(ContactList<Contact>& contacts)
- {
- contacts.PrintList();
- }
-
- inline bool OverWriteCharArray()
- {
- return false;
- }
-
-
-
Contact NewContact()
{
Contact newContact;