// Driver file
#include <iostream>
#include "ItemType.h"
#include "Unsorted.h"
using namespace std;
void Driver1()
{
cout << "Driver1:" << endl;
cout << "========" << endl;
Unsorted UL;
ItemType IT;
ItemType IT2;
IT.Initialize(5);
bool TestF = UL.IsFull();
cout << TestF << endl; //should print 0 :false
UL.InsertItem(IT);
UL.InsertItem(IT2);
cout << UL.LengthIs() << endl << endl << endl;//should print 2
}
void Driver2()
{
cout << "Driver2:" << endl;
cout << "========" << endl;
Unsorted UL;
// Create an ItemType called MyDay and initialize it with your day of birth and put it in the list UL.
ItemType MyDay;
MyDay.Initialize(10);
UL.InsertItem(MyDay);
// Create an ItemType called MyMonth and initialize it with your month of birth and put it in the list UL.
ItemType MyMonth;
MyMonth.Initialize(7);
UL.InsertItem(MyMonth);
// Create an ItemType called MyYear and initialize it with your year of birth and put it in the list UL.
ItemType MyYear;
MyYear.Initialize(1995);
UL.InsertItem(MyYear);
// Create an ItemType called MyT and initialize it with the last four digits of your TNumber and store it in UL.
ItemType MyT;
MyT.Initialize(4017);
UL.InsertItem(MyT);
// Call the member function LengthIs() and display the length of the list after all these insertions.
cout << "Length: " << UL.LengthIs() << endl;
// display the elements in list
cout << "List: " << endl;
UL.ResetList(); // reset list
ItemType item;
// loop over the list, displaying elements
for (int i = 0; i < UL.LengthIs(); i++)
{
UL.GetNextItem(item); // get next item in item
item.Print(); // display item
}
bool found;
// call the member function RetrieveItem() to retrieve your month of birth.
UL.RetrieveItem(MyMonth, found);
// display found or not found based on found value
if (found)
cout << "Found" << endl;
else
cout << "Not found" << endl;
// call the member function DeleteItem() to delete your Tnumber digits.
UL.DeleteItem(MyT);
// Display the elements of the list after this deletion.
cout << "List: " << endl;
UL.ResetList(); // reset the list
// loop over the elements of the list, displaying elements
for (int i = 0; i < UL.LengthIs(); i++)
{
UL.GetNextItem(item);
item.Print();
}
}
int main()
{
Driver1();
Driver2();
return 0;
}
#pragma once
#include "ItemType.h"
class Unsorted
{ // declares a class data type
public: // 8 public member functions
Unsorted();
bool IsFull() const;
int LengthIs() const; // returns length of list
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
void GetNextItem(ItemType& item);
private: // 3 private data members
int length;
ItemType info[MAX_ITEM];
int currentPos;
};
#include "ItemType.h"
#include "Unsorted.h"
Unsorted::Unsorted()
// Pre: None.
// Post: List is empty.
{
length = 0;
}
void Unsorted::InsertItem(ItemType item)
// Pre: List has been initialized. List is not full.
// item is not in list.
// Post: item is in the list.
{
info[length] = item;
length++;
}
int Unsorted::LengthIs() const
// Pre: List has been inititalized.
// Post: Function value == ( number of elements in
// list ).
{
return length;
}
void Unsorted::RetrieveItem(ItemType& item, bool& found)
// Pre: Key member 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 stored in item;
// otherwise, item is unchanged.
{
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;
}
}
}
bool Unsorted::IsFull() const
// Pre: List has been initialized.
// Post: Function value == ( list is full ).
{
return (length == MAX_ITEM);
}
void Unsorted::DeleteItem(ItemType item)
// Pre: item’s key has been inititalized.
// 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++;
// move last element into position where item was located
info[location] = info[length - 1];
length--;
}
void Unsorted::ResetList()
// Pre: List has been inititalized.
// Post: Current position is prior to first element in list.
{
currentPos = -1;
}
void Unsorted::GetNextItem(ItemType& item)
// Pre: List has been initialized. 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.
{
currentPos++;
item = info[currentPos];
}
#pragma once
const int MAX_ITEM = 5;
enum RelationType { LESS, EQUAL, GREATER };
class ItemType // declares class data type
{
public: // 3 public member functions
RelationType ComparedTo(ItemType) const;
void Print() const;
void Initialize(int number);
ItemType();
~ItemType();
private: // 1 private data member
int value; // could be any different type
};
#include "ItemType.h"
#include <iostream>
using namespace std;
ItemType::ItemType()
{
value = 1;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Print() const
{
cout << value << endl;
}
void ItemType::Initialize(int number)
{
value = number;
}
ItemType::~ItemType()
{
}