[英]Sorted list: must have class/struct/union
所以我已经在一个代码上工作了两个多星期,但进展不太顺利。 这是说明,下面是代码,以及错误:
任务1:创建此类的一个实例。 (排序列表;他还具有如何启动代码的其他说明,但是下面的代码已经由我完成,例如typedef ...)。您还需要从一个数据文件中读取数据:float.dat ,其中包含以下数字:
5.5
6.2
7.1
8
9
10.0
1.0
2.0
3.3
4.4
float.dat中的数据包含浮点数,应将其插入SortedList对象中。 请注意,您对float.dat中的数据值没有任何先验知识,但是我们假设数据文件中有10个元素。
任务2:使用GetNextItem()在计算机屏幕上按排序顺序打印出列表中的所有元素。
任务3:使用GetNextItem()将列表中的所有元素按排序顺序输出到数据文件output.dat上。
任务4:设计测试用例,以演示InsertItem(),DeleteItem()和RetrieveItem()是否按预期工作。
这是代码:
#include <iostream>
#include <fstream>
#include <string>
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:
SortedList() {length = 0; currentPos = -1;}
int getLength() {return length;}
RelationType ComparedTo(ItemType x)
{
if (length > x.getLength())
return LESS;
else if (length == x.getLength())
return GREATER;
else
return EQUAL;
}
void MakeEmpty() {length = 0;}
void InsertItem(ItemType x)
{
int first = 0, last = length --;
bool moreToSearch = (first <= last);
int location = 0;
int midpoint= (first + last) / 2;
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 ++; index < length; index++)
values[index --] = 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()
{
SortedList x;
ifstream inFile; ofstream output;
string line;
bool allAboutLists;
int i = 0;
int size = 0;
inFile.open("float.txt");
float values[10];
while (!inFile.eof()) // write or read data from inFile into values
{
inFile >> values[i];
i++;
size++; // this will count how many values there are in the array
x.InsertItem(values[i]);
++i;
}
x.ResetList();
cout << "The following is the list that's been made:" << endl << endl;
x.InsertItem(64);
//x.printlist();
cout << endl;
x.DeleteItem(64);
//x.printlist();
x.RetrieveItem(7.1, allAboutLists);
cout << endl;
cout << endl << "The length is: "; x.LengthIs(); cout << endl;
cout << "Is the list full?: " << boolalpha << x.IsFull() << endl;
cout << "The next item is: ";
for (int i = 0; i < 10; i++)
{
cout << x.GetNextItem << endl;
}
x.ResetList();
inFile.close();
output.open("output.txt");
for (int f = 0; f < 10; f++)
{
output << x.GetNextItem << endl;
}
system("pause");
return 0;
}
并且编译器一直在说:
我已经非常忙碌了,因为我已经研究了2周,这让我发疯了! 我已经完成了所看到的代码,而不是花很多钱,只需要确切地知道要更改什么,因为我一直在关注我一直在搜索和研究的所有内容,但是这样做没有好处。 因此我们将不胜枚举确切的细节或代码,特别是从我的手中获取并修复的代码。
谢谢!
您将x
作为ItemType
传递,这是一个float
。
float
没有这些方法...看起来像您想将其作为SortedList
传递
比较功能需要两个参数才能进行比较。 而不是CompareTo,您可能需要将其称为CompareToLocation。
RelationType CompareToLocation(ItemType x, size_t location){
if(x < values[location]) return LESS;
if(x == values[location]) return EQUAL;
return GREATER;}
一个示例用法是:
result = CompareToLocation(x, location);
// ...
您将CompareTo定义为SortedList的方法,但是,每次调用该函数时,都在实际上是浮动对象的ItemType对象上调用它。
从方法的定义中可以看到,您尝试再次在float对象上使用SortedList方法:
RelationType ComparedTo(ItemType x)
{
if (length > x.getLength())
return LESS;
else if (length == x.getLength())
return GREATER;
else
return EQUAL;
}
您的问题并不是真正的编译问题,而是概念上的问题,因为您似乎并不了解实际要编写的内容。
我建议将您的声明和实现分开,以便您一眼就能看到您的类如何工作。 您的类声明应如下所示:
class SortedList
{
private:
int length;
ItemType values[MAX_ITEMS];
int currentPos;
enum RelationType { LESS, GREATER, EQUAL };
public:
SortedList();
int getLength();
RelationType ComparedTo(ItemType x) ;
void MakeEmpty();
void InsertItem(ItemType x) ;
void DeleteItem(ItemType x);
void RetrieveItem(ItemType &x, bool & found);
int LengthIs();
void ResetList();
bool IsFull();
void GetNextItem(ItemType &x);
};
您应该专注于每种方法,明确每种方法试图实现的目标,以及实现该目标需要什么(参数)。
例如:
RelationType ComparedTo(ItemType x) ;
您的SortedList类具有此函数,该函数接收ItemType(浮点数)作为参数。
这是要达到什么目的? 您如何将整个有序列表与单个元素进行比较? 单个数字如何与一组数字相比更大或更小?
也许您真正想做的是将参数X与列表内的元素进行补偿? 如果是这样,您如何知道必须将列表中的哪个元素与参数X进行比较? 您应该添加另一个参数,告诉您要比较X的有序列表中的哪个元素。
我想这并不能真正解决您的问题,但是至少我希望这可以帮助您更好地了解问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.