我正在阅读一个文本文件并组织它以便用C ++进行搜索。我们必须在文本文件中创建关键字数组和WebPages的链接列表。然后我们需要通过每个关键字将关键字链接到网页,这些关键字具有指向节点本身的链接列表。关键字数组需要按字母顺序排列,网页列表的顺序无关紧要。似乎每个关键字的列表都无法正常工作。
这是关键字类:
class KeywordList
{
private:
friend std::ostream& operator<<(std::ostream&, const KeywordList&);
public:
struct Keyword
{
std::string word;
std::list<WebPageList::WebPage*> list;
};
Keyword keywords [1000];
std::size_t size;
KeywordList()
{
size = 0;
}
~KeywordList() {}
void add(std::string value, WebPageList::WebPage* page)
{
bool keepGoing = true;
int i = 0;
while (keepGoing)
{
if (value == keywords[i].word)
{
keywords[i].list.push_front(page);
keepGoing = false;
} else if (i == size)
{
keywords[i].word = value;
keywords[i].list.push_front(page);
keepGoing = false;
++size;
} else if (value < keywords[i].word)
{
for (int j = size; j > i; --j)
{
keywords[j].word = keywords[j - 1].word;
keywords[j].list = keywords[j - 1].list;
}
keywords[i].word = value;
keywords[i].list.push_front(page);
keepGoing = false;
++size;
} else {
++i;
}
}
}
void search(std::string);
void printAllData()
{
for (int i = 0; i < size; ++i)
{
std::cout << keywords[i].word << " : " << keywords[i].list.size() << " : ";
WebPageList::WebPage * pagesList = keywords[i].list.front();
while (pagesList)
{
std::cout << pagesList->url << " ";
pagesList = pagesList->next;
}
std::cout << std::endl;
}
std::cout << std::endl;
}
这是网页链接列表:
class WebPageList
{
public:
struct WebPage
{
std::string url;
WebPage * next;
};
WebPage * head;
WebPageList()
{
head = NULL;
}
void add(std::string value)
{
WebPage * newPage;
newPage = new WebPage;
newPage->url = value;
if (!head)
{
head = newPage;
newPage->next = NULL;
}
else
{
newPage->next = head;
head = newPage;
}
}
void printAllPages()
{
WebPage * page = head;
while (page)
{
std::cout << page->url << std::endl;
page = page->next;
}
}
这是主要的:
ifstream dataFile;
dataFile.open(argv[1], std::ios::in);
KeywordList keywordList;
WebPageList mainPages;
int i = 0;
std::string line;
while (getline(dataFile, line))
{
std::string keyword, url;
std::istringstream lineStream(line);
lineStream >> url;
mainPages.add(url);
WebPageList::WebPage * passingPage = mainPages.head;
while (lineStream >> keyword)
{
keywordList.add(keyword, passingPage);
cout << endl;
keywordList.printAllData();
}
}
mainPages.printAllPages();
dataFile.close();
以下是我得到的输出:
structures : 1 : http://test1
structures : 1 : http://test1
zzz : 1 : http://something http://test1
structures : 1 : http://test1
zalgorithms : 2 : http://www.algorithms.com/ http://something http://test1
zzz : 1 : http://something http://test1
structures : 1 : http://test1
zalgorithms : 2 : http://www.algorithms.com/ http://something http://test1
zzz : 2 : http://somethingelse http://www.algorithms.com/ http://something http://test1
http://somethingelse
http://www.algorithms.com/
http://something
http://test1
它基于此输入文件:
http://test1 structures
http://something zzz
http://www.algorithms.com/ zalgorithms
http://somethingelse zzz
我的损失很大,所以任何帮助都会非常感激!