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
};
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 revised code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_ITEMS 10
enum RelationType { LESS, GREATER, EQUAL };
typedef float ItemType;
class SortedList {
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
public:
RelationType ComparedTo(ItemType x) {
if (length > x.getLength())
return LESS;
else if (length == x.getLength())
return GREATER;
else
return EQUAL;
} SortedList::SortedList() {
length = 0;
currentPos = -1;
}
bool SortedList::IsFull() {
if (length < 9)
return false;
else
return true;
}
void SortedList::MakeEmpty() {
length = 0;
}
void SortedList::InsertItem(ItemType x) {
bool moreToSearch;
int location = 0;
moreToSearch = (location < length);
while (moreToSearch) {
switch (x.ComparedTo(values[location])) {
case LESS: //search in 1st half
moreToSearch = false;
break;
case GREATER:
location++;
moreToSearch = (location < length);
break;
}
}
for (int index = length; index > location; index--) {
values[index] = values[index - 1];
}
values[location] = x;
length++;
}
void SortedList::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 SortedList::RetrieveItem(ItemType & x, 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 (x.ComparedTo(values[midpoint])) {
case LESS: //search in 1st half
last = midpoint - 1;
moreToSearch = first <= last;
break;
case GREATER: //Search in 2nd half
first = midpoint + 1;
moreToSearch = first <= last;
break;
case EQUAL: //x has been found
found = true;
x = values[midpoint];
break;
}
}
}
int SortedList::LengthIs() {
return length;
}
void SortedList::ResetList() {
currentPos = -1;
}
void SortedList::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 am still getting these compiler errors:
1>c:\users\adam\documents\visual studio 2008\projects\cis200proj2\cis200proj2\cis200proj2.cpp(17) : 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(19) : 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(47) : 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(70) : 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(70) : fatal error C1903: unable to recover from previous error(s); stopping compilation
I am pretty sure that my RelationType ComparedTo(ItemType x) is set up the right way but it is still giving me errors.