Question
*****************************************I need help with this C++ progaming assignment please*********************************** Define a nested class called iterator Overload the prefix ++ operator Overload the != operator Overload
*****************************************I need help with this C++ progaming assignment please***********************************
Define a nested class called iterator
Overload the prefix ++ operator
Overload the != operator
Overload the * operator
The iterator class should also support begin(), end()
Modify the provided console driver to support interactive work with the data structure.
Here is some sample output:
Driver to test iterator for UnsortedType class
Please choose one of the following:
0 to quit
1 to insert
2 to delete
3 to print list
4 to get iterator to first element
5 to advance iterator
6 to compare iterator to end iterator
7 to dereference iterator
1
Enter integer to insert: 21
Please choose one of the following:
0 to quit
1 to insert
2 to delete
3 to print list
4 to get iterator to first element
5 to advance iterator
6 to compare iterator to end iterator
7 to dereference iterator
1
Enter integer to insert: -48
Please choose one of the following:
0 to quit
1 to insert
2 to delete
3 to print list
4 to get iterator to first element
5 to advance iterator
6 to compare iterator to end iterator
7 to dereference iterator
3
PrintList
21 -48
Please choose one of the following:
0 to quit
1 to insert
2 to delete
3 to print list
4 to get iterator to first element
5 to advance iterator
6 to compare iterator to end iterator
7 to dereference iterator
0
The driver is ending
Please follow the expected output text. This will allow me to grade your assignments faster so you get your grades earlier.
Style: Provide a brief comment to describe the program and each class and function you add, use meaningful names for the classes, functions, variables and constants you define. (For constants, you have to not only define named constants, but actually use those names.)
Main.cpp
/******************************************************************************
CS 301 Spring 2021 hw1
*******************************************************************************/
#include
using namespace std;
// Modified version of the PrintList in the text code // Main difference is to use ostream instead of ofstream // so we can use this with cout void PrintList(ostream& dataFile, UnsortedType& list) // Pre: list has been initialized. // dataFile is open for writing. // Post: Each component in list has been written to dataFile. // dataFile is still open. { int length; ItemType item; dataFile << "PrintList" << endl; list.ResetList(); length = list.GetLength(); if (length == 0) dataFile << "List is empty."; else /* for (int counter = 1; counter <= length; counter++) { item = list.GetNextItem(); item.Print(dataFile); } */ // Using the range-based for loop that got support with C++11 // Newer options for this style of for loop continue to be introduced for (auto item : list) { item.Print(dataFile); dataFile << ' '; } dataFile << endl; }
int main() { UnsortedType list; int cmd = -1; int val = 0; ItemType element; UnsortedType::iterator iter = list.begin(); const int QUIT = 0, INSERT = 1, DELETE = 2, PRINT = 3, START = 4, INC = 5, COMPARE_TO_END = 6, DEREF = 7; const string MENU = "0 to quit 1 to insert 2 to delete 3 to print list 4 to get iterator to first element 5 to advance iterator 6 to compare iterator to end iterator 7 to dereference iterator ";
cout << "Driver to test iterator for UnsortedType class" << endl << endl;
// for "true/false" output cout << boolalpha; do { cout << "Please choose one of the following: " << MENU; cin >> cmd; switch(cmd) { case INSERT: cout << "Enter integer to insert: "; cin >> val; list.PutItem(ItemType(val)); break; case DELETE: cout << "Enter integer to delete: "; cin >> val; list.DeleteItem(ItemType(val)); break; case PRINT: PrintList(cout, list); break; case START: iter = list.begin(); break; case INC: // INSERT HERE break; case COMPARE_TO_END: cout << "Equal to end iterator? " << (iter == list.end()) << endl; break; case DEREF: cout << "Iterator points to " ; // INSERT HERE cout << endl; break; case QUIT: cout << "The driver is ending" << endl; break; default: cerr << "Unrecognized command : " << cmd << endl; break; } } while (QUIT != cmd);
return 0; }
***********************************************************************************************************************************
ItemType.h
// The following declarations and definitions go into file
// ItemType.h.
#include
#include
const int MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType
{
public:
ItemType();
ItemType(int _value): value(_value) {} // added for convenience
RelationType ComparedTo(ItemType) const;
void Print(std::ostream&) const;
void Initialize(int number);
private:
int value;
};
// copied from file ItemType.cpp.
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(std::ostream& out) const
// pre: out has been opened.
// post: value has been sent to the stream out.
{
out << value;
}
*************************************************************************************************************
unsorted.h
#include "ItemType.h" // Source code for array-based list for Dale's C++ plus Data Structures, 6th ed. // https://www.jblearning.com/catalog/productdetails/9781284089189#productInfo // ch. 3 // Enhanced with a more appropriate iterator -- nested struct
// File ItemType.h must be provided by the user of this class. // ItemType.h must contain the following definitions: // MAX_ITEMS: the maximum number of items on the list // ItemType: the definition of the objects on the list // RelationType: {LESS, GREATER, EQUAL} // Member function ComparedTo(ItemType item) which returns // LESS, if self "comes before" item // GREATER, if self "comes after" item // EQUAL, if self and item are the same
class UnsortedType { private: int length; ItemType info[MAX_ITEMS]; int currentPos; public: UnsortedType(); // Constructor void MakeEmpty(); // Function: Returns the list to the empty state. // Post: List is empty. bool IsFull() const; // Function: Determines whether list is full. // Pre: List has been initialized. // Post: Function value = (list is full)
int GetLength() const; // Function: Determines the number of elements in list. // Pre: List has been initialized. // Post: Function value = number of elements in list
ItemType GetItem(ItemType, bool&); // Function: Retrieves list element whose key matches item's key (if // present). // Pre: List has been initialized. // Key member of item is initialized. // Post: If there is an element someItem whose key matches // item's key, then found = true and someItem is returned. // otherwise found = false and item is returned. // List is unchanged.
void PutItem(ItemType item); // Function: Adds item to list. // Pre: List has been initialized. // List is not full. // item is not in list. // Post: item is in list.
void DeleteItem(ItemType item); // Function: Deletes the element whose key matches item's key. // Pre: List has been initialized. // Key member of item is initialized. // One and only one element in list has a key matching item's key. // Post: No element in list has a key matching item's key.
void ResetList(); // Function: Initializes current position for an iteration through the list. // Pre: List has been initialized. // Post: Current position is prior to list.
ItemType GetNextItem(); // Function: Gets the next element in list. // Pre: List has been initialized and has not been changed since last call. // Current position is defined. // Element at current position is not last in list. // // Post: Current position is updated to next position. // item is a copy of element at current position.
// INSERT nested iterator class here };
// copied from unsorted.cpp
UnsortedType::UnsortedType() { length = 0; } bool UnsortedType::IsFull() const { return (length == MAX_ITEMS); } int UnsortedType::GetLength() const { return length; }
ItemType UnsortedType::GetItem(ItemType item, bool& found) // Pre: Key member(s) of item is initialized. // Post: If found, item's key matches an element's key in the // list and a copy of that element has been returned; // otherwise, item is returned. { bool moreToSearch; int location = 0; found = false;
moreToSearch = (location < length);
while (moreToSearch && !found) { switch (item.ComparedTo(info[location])) { case LESS : case GREATER : location++; moreToSearch = (location < length); break; case EQUAL : found = true; item = info[location]; break; } } return item; } void UnsortedType::MakeEmpty() // Post: list is empty. { length = 0; } void UnsortedType::PutItem(ItemType item) // Post: item is in the list. { info[length] = item; length++; } void UnsortedType::DeleteItem(ItemType item) // Pre: item's key has been initialized. // An element in the list has a key that matches item's. // Post: No element in the list has a key that matches item's. { int location = 0;
while (item.ComparedTo(info[location]) != EQUAL) location++;
info[location] = info[length - 1]; length--; } void UnsortedType::ResetList() // Post: currentPos has been initialized. { currentPos = -1; }
ItemType UnsortedType::GetNextItem() // Pre: ResetList was called to initialized iteration. // No transformer has been executed since last call. // currentPos is defined. // Post: item is current item. // Current position has been updated. { currentPos++; return info[currentPos]; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started