分配是为链表创建排序功能。能够编码,但我得到了大量未知的语法错误。我怀疑它可能是Visual Studio(教授要求它),但我想看看你们的想法。大多数错误都存在于头文件(SortedList.h)中,但是我的教授提供了这段代码,所以它没有意义,它不起作用。
EDIT:
Errors include:
-Missing type specifier - int assumed Note: C++ does not support default int
(sortedlist.h Line:4)
-Syntax error missing ; before identifier ItemType (sortedlist.h Line:4)
-'string' ambiguous symbol (sortedlistprogram.cpp Line:12)
-Syntax error: Identifier 'itemType' (Sortedlist.h Line 14)
-Unexpected token(s) proceeding ';' (sortedlist.h Line 34)
//SortedList.h (header file)
#include <string>
const int MAX_ITEMS = 20;
typedef string ItemType;
class SortedList
{
public:
SortedList();
// Constructor
// Post: Empty list is created.
// Action responsibilities
void Insert(ItemType item);
// Pre: The list is not full;
// Post: item is in the list; list is stored in
// increasing order.
void PrintList();
// Post: If the list is not empty, the elements are
// printed on the screen in increasing order;
// otherwise "The list is empty" is
// printed on the screen.
// Knowledge responsibilities
int GetLength();
// Post: return value is the number of items in the list.
bool IsEmpty();
// Post: returns true if list is empty; false otherwise.
bool IsFull();
// Post: returns true if there is no more room in the
// list; false otherwise.
private:
int length;
ItemType values[MAX_ITEMS];
};
//SortedList.CPP
#include "sortedList.h"
#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
SortedList::SortedList()
{
length = 0;
}
bool SortedList::IsEmpty()
//Function to check if empty
{
if (values->empty())
return true;
return false;
}
bool SortedList::IsFull()
//Function to check if full
{
if (length == MAX_ITEMS)
return true;
return false;
}
void SortedList::Insert(ItemType item)
//Insert function for SortedList
{
if (!IsFull())
{
values[length] = item;
++length;
sort(values, values + length);
}
}
int SortedList::GetLength()
//Function to return length
{
return length;
}
void SortedList::PrintList()
//Function to print list
{
if (IsEmpty())
{
cout << "List is empty" << endl;
}
else
{
for (int index = 0; index < length; ++index)
{
cout << values[index] << endl;
}
cout << endl << "There are " << GetLength() << " items in the list" <<
endl;
}
}
//Driver.CPP
#include "sortedList.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string value;
SortedList list;
list.PrintList();
ifstream file("word.in");
//calls word.in file
if (file.good())
//Checks if file is good
{
while (getline(file, value))
{
list.Insert(value);
}
file.close();
}
list.PrintList();
//Prints list
return 0;
}