Name: Anonymous 2009-03-16 23:03
include<iostream>
#include<fstream>
using namespace std;
#define MAX_ITEMS 20
template<class 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
};
int numCharElement(ifstream &x);
int numFloatElement(ifstream &x);
int main()
{
ifstream inChar,inFloat ;
char charHolder;
float floatHolder;
int numOfChars,numOfFloats;
inChar.open("char.dat");
inFloat.open("float.dat");
SortedList <char> letters;
SortedList <float> numbers;
numOfChars = numCharElement(inChar);
numOfFloats = numFloatElement(inFloat);
inChar.seekg(0,std::ios::beg);
inChar.clear();
inFloat.seekg(0,std::ios::beg);
inFloat.clear();
for(int a=0;a<=numOfChars;a++)
{
inChar>>charHolder;
letters.InsertItem(charHolder);
}
for(int b=0;b<=numOfFloats;b++)
{
inFloat>>floatHolder;
numbers.InsertItem(floatHolder);
}
cout<<endl<<endl;
cout<<"There are "<<numOfChars<<" elements in the char.dat file."<<endl<<endl;
cout<<"There are "<<numOfFloats<<" elements in the float.dat file."<<endl;
cout<<"The elements in char.dat after being sorted are "<<endl;
for(int c=0;c<=numOfChars;c++)
{
letters.GetNextItem(charHolder);
cout<<charHolder<<endl;
}
cout<<endl<<"The elements in float.dat after being sorted are "<<endl;
for(int d=0;d<=numOfFloats;d++)
{
numbers.GetNextItem(floatHolder);
cout<<floatHolder<<endl;
}
return 0;
}
int numCharElement(ifstream &x)
{
char z;
int count = 0;
x>>z;
cout<<z<<endl; //delete
while(!x.eof())
{
x>>z;
cout<<z<<endl; //delete
count++;
}
return count;
}
int numFloatElement(ifstream &x)
{
float y;
int count = 0;
x>>y;
cout<<y<<endl; //delete
while(!x.eof())
{
x>>y;
cout<<y<<endl; //delete
count++;
}
return count;
}
template<class ItemType> SortedList<ItemType>::SortedList()
{
length = 0;
currentPos = -1;
}
template<class ItemType> void SortedList<ItemType>::MakeEmpty()
{
length = 0;
}
template<class ItemType> void SortedList<ItemType>::InsertItem(ItemType x)
{
int location = 0;
int end = length - 1;
bool moreToSearch;
moreToSearch = (location<length);
while(moreToSearch)
{
int midPoint = (location + end) / 2;
if(x>values[midPoint] && location<=end)
{
location = midPoint + 1;
}
else if(x<values[midPoint] && location<=end)
{
end = midPoint - 1;
}
else
moreToSearch = false;
}
for(int index=length;index<location;index--)
values[index] = values[index-1];
values[location]=x;
length++;
}
template<class ItemType> void SortedList<ItemType>::DeleteItem(ItemType x)
{
int location = 0;
int end = x.length - 1;
while(location<=end)
{
int midPoint = (start + end) / 2;
if(x>values[midPoint])
location = midPoint + 1;
else if(x<values[midPoint])
end = midPoint - 1;
else
{
for(index = location + 1;index < length; index++)
{
values[location-1] = values[location];
}
length--;
}
}
}
template<class ItemType> bool SortedList<ItemType>::IsFull( )
{
return (length == MAX_ITEMS);
}
template<class ItemType> int SortedList<ItemType>::Lengthls( )
{
return length;
}
template<class ItemType> void SortedList<ItemType>::RetrieveItem(ItemType &x, bool &found)
{
int location = 0;
int end = x.length - 1;
while(location<=end)
{
int midPoint = (location + end) / 2;
if(x>values[midPoint])
location = midPoint + 1;
else if(x<values[midPoint])
end = midPoint - 1;
else
{
found = true;
x = values[midPoint];
break;
}
}
}
template<class ItemType> void SortedList<ItemType>::ResetList( )
{
currentPos = -1;
}
template<class ItemType> void SortedList<ItemType>::GetNextItem(ItemType &x)
{
x = values[++currentPos];
}
And How do I fix it?
#include<fstream>
using namespace std;
#define MAX_ITEMS 20
template<class 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
};
int numCharElement(ifstream &x);
int numFloatElement(ifstream &x);
int main()
{
ifstream inChar,inFloat ;
char charHolder;
float floatHolder;
int numOfChars,numOfFloats;
inChar.open("char.dat");
inFloat.open("float.dat");
SortedList <char> letters;
SortedList <float> numbers;
numOfChars = numCharElement(inChar);
numOfFloats = numFloatElement(inFloat);
inChar.seekg(0,std::ios::beg);
inChar.clear();
inFloat.seekg(0,std::ios::beg);
inFloat.clear();
for(int a=0;a<=numOfChars;a++)
{
inChar>>charHolder;
letters.InsertItem(charHolder);
}
for(int b=0;b<=numOfFloats;b++)
{
inFloat>>floatHolder;
numbers.InsertItem(floatHolder);
}
cout<<endl<<endl;
cout<<"There are "<<numOfChars<<" elements in the char.dat file."<<endl<<endl;
cout<<"There are "<<numOfFloats<<" elements in the float.dat file."<<endl;
cout<<"The elements in char.dat after being sorted are "<<endl;
for(int c=0;c<=numOfChars;c++)
{
letters.GetNextItem(charHolder);
cout<<charHolder<<endl;
}
cout<<endl<<"The elements in float.dat after being sorted are "<<endl;
for(int d=0;d<=numOfFloats;d++)
{
numbers.GetNextItem(floatHolder);
cout<<floatHolder<<endl;
}
return 0;
}
int numCharElement(ifstream &x)
{
char z;
int count = 0;
x>>z;
cout<<z<<endl; //delete
while(!x.eof())
{
x>>z;
cout<<z<<endl; //delete
count++;
}
return count;
}
int numFloatElement(ifstream &x)
{
float y;
int count = 0;
x>>y;
cout<<y<<endl; //delete
while(!x.eof())
{
x>>y;
cout<<y<<endl; //delete
count++;
}
return count;
}
template<class ItemType> SortedList<ItemType>::SortedList()
{
length = 0;
currentPos = -1;
}
template<class ItemType> void SortedList<ItemType>::MakeEmpty()
{
length = 0;
}
template<class ItemType> void SortedList<ItemType>::InsertItem(ItemType x)
{
int location = 0;
int end = length - 1;
bool moreToSearch;
moreToSearch = (location<length);
while(moreToSearch)
{
int midPoint = (location + end) / 2;
if(x>values[midPoint] && location<=end)
{
location = midPoint + 1;
}
else if(x<values[midPoint] && location<=end)
{
end = midPoint - 1;
}
else
moreToSearch = false;
}
for(int index=length;index<location;index--)
values[index] = values[index-1];
values[location]=x;
length++;
}
template<class ItemType> void SortedList<ItemType>::DeleteItem(ItemType x)
{
int location = 0;
int end = x.length - 1;
while(location<=end)
{
int midPoint = (start + end) / 2;
if(x>values[midPoint])
location = midPoint + 1;
else if(x<values[midPoint])
end = midPoint - 1;
else
{
for(index = location + 1;index < length; index++)
{
values[location-1] = values[location];
}
length--;
}
}
}
template<class ItemType> bool SortedList<ItemType>::IsFull( )
{
return (length == MAX_ITEMS);
}
template<class ItemType> int SortedList<ItemType>::Lengthls( )
{
return length;
}
template<class ItemType> void SortedList<ItemType>::RetrieveItem(ItemType &x, bool &found)
{
int location = 0;
int end = x.length - 1;
while(location<=end)
{
int midPoint = (location + end) / 2;
if(x>values[midPoint])
location = midPoint + 1;
else if(x<values[midPoint])
end = midPoint - 1;
else
{
found = true;
x = values[midPoint];
break;
}
}
}
template<class ItemType> void SortedList<ItemType>::ResetList( )
{
currentPos = -1;
}
template<class ItemType> void SortedList<ItemType>::GetNextItem(ItemType &x)
{
x = values[++currentPos];
}
And How do I fix it?