Implement a class SortedList as defined by the following skeleton:
#define MAX_ITEMS 10
typedef float ItemType;
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
SortedList( ); // default constructor: lenght=0, currentPos=-1
void MakeEmpty; // let length=0
void InsertItem(ItemType x); // insert x into the list
void DeleteItem(ItemType x); // delete x from the list
bool IsFull( ); // test if the list is full
int Lengthls( ); // return length
void RetrieveItem(ItemType &x, bool &found); // retrieve x from the list, the
// boolean result is stored in found
void ResetList( ); // currentPos=-1
void GetNextItem(ItemType &x); // get the next element from the list with
// respect to the currentPos
};
Requirement: You should use binary search in InsertItem( ), DeleteItem( ) and RetrieveItem( ).
In your main routine, the following tasks are required to complete:
Task 1: Create one instance of this class. You also need to read in data from one data file: float.txt, which contains the following numbers:
5.5
6.2
7.1
8.0
9.0
10.0
1.0
2.0
3.3
4.4
Data in float.txt contains floating numbers, which should be inserted into the object of SortedList. Note that you do not have any prior knowledge about data values in float.dat, but we assume that there are 10 elements in the data file.
Task 2: Use GetNextItem( ) to print out all the elements in the list in sorted sequence on computer screen.
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_ITEMS 10
typedef float ItemType;
class SortedList {
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
enum RelationType { LESS, GREATER, EQUAL };
public:
int getLength() {
return length;
} RelationType ComparedTo(ItemType x) {
if (length > x.getLength())
return LESS;
else if (length == x.getLength())
return GREATER;
else
return EQUAL;
}
SortedList() {
length = 0;
currentPos = -1;
}
void MakeEmpty() {
length = 0;
}
void InsertItem(ItemType x) {
bool moreToSearch;
int location = 0;
int midpoint;
int first = 0;
int last = length - 1;
midpoint = (first + last) / 2;
moreToSearch = (first <= last);
while (moreToSearch) {
switch (x.comparedTo(values[location])) {
case LESS: //search in 1st half
moreToSearch = (first <= last);
break;
case GREATER:
location++;
moreToSearch = (location < length);
break;
}
}
for (int index = length; length > location; index--) {
values[index] = values[index - 1];
}
values[location] = x;
length++;
}
void DeleteItem(ItemType x) {
int location = 0;
while (x.ComparedTo(values[location]) != EQUAL)
location++;
for (int index = location + 1; index < length; index++)
values[index - 1] = values[index];
length--;
}
void RetrieveItem(ItemType & x, bool & found) {
int midpoint;
int first = 0, last = length - 1;
bool moreToSearch = (first <= last);
found = false;
int index = 0;
while (moreToSearch && !found) {
midpoint = (first + last) / 2 switch (x.ComparedTo(values[index++])) {
case LESS: //search in 1st half
moreToSearch = (first <= last);
last = midpoint - 1;
break;
case GREATER: //Search in 2nd half
first = midpoint + 1;
moreToSearch = (first <= last);
break;
case EQUAL: //x has been found
found = true;
break;
}
}
}
int LengthIs() {
return length;
}
void ResetList() {
currentPos = -1;
}
bool IsFull() {
if (length < 9)
return false;
else
return true;
}
void GetNextItem(ItemType & x) {
currentPos++;
x = values[currentPos];
cout << x;
}
};
int main()
{
ifstream indata;
int i = 0;
int size = 0;
indata.open("float.txt");
float values[10];
while (!indata.eof()) // write or read data from indatac into values
{
indata >> values[i];
i++;
size++; // this will count how many values there are in the array
}
SortedList a;
ItemType x;
a.GetNextItem(x);
indata.close();
system("pause");
return 0;
}
I have been getting some compiler errors that I have been trying to fix it but have not been able to.
My compiler errors say
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\cis200proj2.cpp(21) : error C2228: left of '.getLength' must have class/struct/union
1> type is 'ItemType'
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\cis200proj2.cpp(23) : error C2228: left of '.getLength' must have class/struct/union
1> type is 'ItemType'
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\cis200proj2.cpp(51) : error C2228: left of '.comparedTo' must have class/struct/union
1> type is 'ItemType'
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\cis200proj2.cpp(74) : error C2228: left of '.ComparedTo' must have class/struct/union
1> type is 'ItemType'
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\cis200proj2.cpp(74) : fatal error C1903: unable to recover from previous error(s); stopping compilation