[英]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.