Question: Write in C++: ( Ordered Sets ) Redo Programming Exercise 13 of Chapter 12 using template. Define the class unorderedSetType to manipulate sets. The elements
Write in C++:
(Ordered Sets) Redo Programming Exercise 13 of Chapter 12 using template. Define the class unorderedSetType to manipulate sets. The elements of an unorderedSetType object are distinct, but in no particular order. Design the class orderedSetType, derived from the class orderedArrayListType, to manipulate ordered sets. The elements of an orderedSetType object are distinct and in ascending order. Note that you need to redefine only the functions insert and replaceAt. If the item to be inserted is already in the list, the function insert outputs an appropriate message. Similarly, if the item to be replaced is already in the list, the function replaceAt outputs an appropriate message. Also, write a program to test your class.
Required File: (main.cpp)
Program Code / Logic for testing:
orderedSetType intList(25); double ary[] = {3.54,65,45.56,346.8,2356.7}; TEST(SET,0) { double x; for (int count = 0; count < 5; count++) { intList.insert(ary[count]); } intList.replaceAt(3,123); intList.retrieveAt(3,x); cout << x; ASSERT_EQ(123, x) << "'x' did not equal the expected replacement value of 123"; } Given Files:
//arrayListType.h
#ifndef H_arrayListType
#define H_arrayListType
#include
using namespace std;
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, int item) const;
virtual void insertAt(int location, int insertItem) = 0;
virtual void insertEnd(int insertItem) = 0;
void removeAt(int location);
void retrieveAt(int location, int& retItem) const;
virtual void replaceAt(int location, int repItem) = 0;
void clearList();
virtual int seqSearch(int searchItem) const = 0;
virtual void remove(int removeItem) = 0;
arrayListType(int size = 100);
arrayListType (const arrayListType& otherList);
virtual ~arrayListType();
protected:
int *list;
int length;
int maxSize;
};
bool arrayListType::isEmpty() const
{
return (length == 0);
}
bool arrayListType::isFull() const
{
return (length == maxSize);
} //end isFull
int arrayListType::listSize() const
{
return length;
} //end listSize
int arrayListType::maxListSize() const
{
return maxSize;
}
void arrayListType::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
} //end print
bool arrayListType::isItemAtEqual(int location, int item) const
{
if (location < 0 || location >= length)
{
cout << "The location of the item to be removed "
<< "is out of range." << endl;
return false;
}
else
return (list[location] == item);
} //end isItemAtEqual
void arrayListType::removeAt(int location)
{
if (location < 0 || location >= length)
cout << "The location of the item to be removed "
<< "is out of range." << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i+1];
length--;
}
} //end removeAt
void arrayListType::retrieveAt(int location, int& retItem) const
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "
<< "out of range" << endl;
else
retItem = list[location];
} //end retrieveAt
void arrayListType::clearList()
{
length = 0;
} //end clearList
arrayListType::arrayListType(int size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array of the size 100." << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new int[maxSize];
} //end constructor
arrayListType::~arrayListType()
{
delete [] list;
} //end destructor
arrayListType::arrayListType(const arrayListType& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new int[maxSize]; //create the array
for (int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
}//end copy constructor
#endif
//orderedArrayListType.h
#ifndef H_orderedArrayListType
#define H_orderedArrayListType
#include
#include "arrayListType.h"
using namespace std;
class orderedArrayListType: public arrayListType
{
public:
virtual void insertAt(int location, int insertItem);
virtual void insertEnd(int insertItem);
virtual void replaceAt(int location, int repItem);
virtual int seqSearch(int searchItem) const;
virtual void insert(int insertItem);
virtual void remove(int removeItem);
orderedArrayListType(int size = 100);
//Constructor
};
void orderedArrayListType::insert(int insertItem)
{
if (length == 0) //list is empty
list[length++] = insertItem; //insert insertItem
//and increment length
else if (length == maxSize)
cout << "Cannot insert in a full list." << endl;
else
{
//Find the location in the list where to insert
//insertItem.
int loc;
for (loc = 0; loc < length; loc++)
{
if (list[loc] >= insertItem)
{
break;
}
}
for (int i = length; i > loc; i--)
list[i] = list[i - 1]; //move the elements down
list[loc] = insertItem; //insert insertItem
length++; //increment the length
}
} //end insert
int orderedArrayListType::seqSearch(int searchItem) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
{
if (list[loc] >= searchItem)
{
found = true;
break;
}
}
if (found)
{
if (list[loc] == searchItem)
return loc;
else
return -1;
}
else
return -1;
} //end seqSearch
void orderedArrayListType::insertAt(int location, int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be " << "inserted is out of range." << endl ;
else if (length == maxSize) //list is full
cout << "Cannot insert in a full list." << endl;
else
{
cout << "This is a sorted list. Inserting at the proper place."<< endl;
insert(insertItem);
}
} //end insertAt
void orderedArrayListType::insertEnd(int insertItem)
{
if (length == maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
cout << "This is a sorted list. Inserting at the proper "<< "place." << endl;
insert(insertItem);
}
} //end insertEnd
void orderedArrayListType::replaceAt(int location, int repItem)
{
if (location < 0 || location >= length)
cout << "The location of the item to be replaced is out "
<< "of range." << endl;
else
{
removeAt(location);
insert(repItem);
}
} //end replaceAt
void orderedArrayListType::remove(int removeItem)
{
int loc;
if (length == 0)
cout << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list." << endl;
}
} //end remove
orderedArrayListType::orderedArrayListType(int size)
: arrayListType(size)
{
}
#endif
//orderedSetType.h
#ifndef H_orderedSetType
#define H_orderedSetType
#include
#include "orderedArrayListType.h"
using namespace std;
class orderedSetType: public orderedArrayListType
{
public:
void insert(int insertItem);
void replaceAt(int location, int repItem);
orderedSetType(int size = 100);
//Constructor
};
void orderedSetType::insert(int insertItem)
{
int loc;
bool found = false;
if (length == 0) //list is empty
list[length++] = insertItem; //insert insertItem
//and increment length
else if (length == maxSize)
cout << "Cannot insert in a full list." << endl;
else
{
for (loc = 0; loc < length; loc++)
{
if (list[loc] >= insertItem)
{
found = true;
break;
}
}
if (found)
found = (list[loc] == insertItem);
if (!found) //insertItem is not in list
{
for (int i = length; i > loc; i--)
list[i] = list[i - 1]; //move the
//elements down
list[loc] = insertItem; //insert insertItem
length++; //increment the length
}
else
cout << "The item to be inserted is already in the list. "
<< "No duplicates are allowed." << endl;
}
} //end insert
void orderedSetType::replaceAt(int location, int repItem)
{
if (location < 0 || location >= length)
cout << "The location of the item to be "
<< "replaced is out of range." << endl;
else
{
int loc = seqSearch(repItem);
if (loc == -1)
{
removeAt(location);
insert(repItem);
}
else
cout << "The item to be inserted is already in the list." << endl;
}
} //end replaceAt
orderedSetType::orderedSetType(int size)
: orderedArrayListType(size)
{
} //end constructor
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
