// The following declarations and definitions go into file ItemType.h.
#include <fstream>
const int MAX_ITEMS = 5;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType
{
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print(std::ofstream&) const;
void Initialize(int number);
private:
int value;
};
**********************************************************************************
// The following definitions go into file [Link].
#include <fstream>
#include "ItemType.h"
ItemType::ItemType()
{
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < [Link])
return LESS;
else if (value > [Link])
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print(std::ofstream& out) const
// Pre: out has been opened.
// Post: value has been sent to the stream out.
{
out << value << " ";
}
****************************************************************************
// The following declarations and definitions go into file SortedType.h.
#include "ItemType.h"
// 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 SortedType
{
public :
SortedType () ;
bool IsFull() const;
int LengthIs() const;
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
void GetNextItem(ItemType& item);
private:
int length:
ItemType info[MAX_ITEMS];
int currentPos;
};
***********************************************************************
// The following definitions go into file [Link].
#include "UnsortedType.h"
SortedType::SortedType()
{
length = 0;
}
bool SortedType::IsFull() const
{
return (length == MAX_ITEMS);
}
int SortedType::LengthIs() const
{
return length;
}
void SortedType::RetrieveItem(ItemType& item, bool& found)
{
int midPoint;
int first = 0;
int last = length - 1;
bool moreToSearch = first <= last;
found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
switch ([Link](info[midPoint]))
{
case LESS : last = midPoint - 1;
moreToSearch = first <= last;
break;
case GREATER : first = midPoint + 1;
moreToSearch = first <= last;
break;
case EQUAL : found = true;
item = info[midPoint];
break;
}
}
}
void SortedType::InsertItem(ItemType item)
{
bool moreToSearch;
int location = 0;
moreToSearch = (location < length);
while (moreToSearch)
{
switch ([Link](info[location]))
{
case LESS : moreToSearch = false;
break;
case GREATER : location++;
moreToSearch = (location < length);
break;
}
}
for (int index = length; index > location; index--)
info[index] = info[index - 1];
info[location] = item;
length++;
}
void SortedType::DeleteItem(ItemType item)
{
int location = 0;
while ([Link](info[location]) != EQUAL)
location++;
for (int index = location + 1; index < length; index++)
info[index - 1] = info[index];
length--;
}
void SortedType::ResetList()
// Post: currentPos has been initialized.
{
currentPos = - 1;
}
// Pre: No transformer has been executed since last call
// Post: item is current item.
// Current position has been updated.
{
currentPos++;
item = info [currentPos] ;
}
***********************************************************************************
***********