【发布时间】:2014-03-03 08:31:19
【问题描述】:
我正在编写一个程序,它从文本文件中读取单词并将所有这些单词放入链接列表中。该文件没有标点符号,只有单词。我还想将链表与预加载的黑名单进行比较,黑名单也是链表。
我已经完成的是我可以从文件中加载链接列表,打印链接列表,检查大小,计算一个单词在文件中出现的频率,而不是打印低于指定的单词频率,并且我还能够将所有单词格式化为小写以便更好地处理。
我遇到的问题是让代码正确,以便它只打印一次出现多个频率的单词。因此,如果单词“the”出现 20 次,我不希望它在下次出现时打印“the ”然后打印“the ”,清除“the ”我只是希望它打印一次“”
我正在发布我的加载文件功能、打印功能和插入字功能,这些都是class wordCloud() 的一部分。
下面是代码:
void wordCloud::insertWord(string aWord){
wordNode *newWord = new wordNode(aWord);
//old code
if (head == NULL)
head = newWord;
else{
newWord->next = head;
head = newWord;
}
//revised code
//newWord->next = head;
//head = newWord;
size++;
}
void wordCloud::insertWordDistinct(string word){
for (wordNode *temp = head; temp != NULL; temp = temp->next){
if (word == temp->myWord){
temp->freq_count++;
//cout << temp->freq_count; //for debugging
}
}
insertWord(word);
}
void wordCloud::printWordCloud(int freq){
wordNode *temp, *previous;
int listSize = 0;
if (head == NULL) //determines if there are any words in the list
cout << "No Word Cloud" << endl;
else{
temp = head;
while (temp->next != NULL){ //prints each word until the list is NULL
if (temp->freq_count >= freq){
cout << temp->myWord << " <" << temp->freq_count << ">" << endl;
temp = temp->next;
listSize++;
}
else{
previous = temp;
temp = temp->next;
previous = NULL;
free(previous);
}
}
}
cout << "\nThere are " << size << " words in the file.\n"; //print file size - for debugging - works
cout << "\nThere are " << listSize << " words in the list\n\n"; //print list size - for debugging - works
system("pause");
}
void wordCloud::printBlacklist(){
wordNode *temp;
if (head == NULL) //determines if there is a list
cout << "No Words in the blacklist" << endl;
else{
temp = head;
while (temp != NULL){ //prints each word until the list is NULL
cout << temp->myWord << endl;
temp = temp->next;
}
}
cout << "\nThere are " << size << " words in the file.\n\n"; //print size - for debugging - works
system("pause");
}
void wordCloud::loadWordCloud(string fileName){
ifstream file; //variable for fileName
string word; //string to hold each word
file.open(fileName); //open file
if (!file) { //error handling
cout << "Error: Can't open the file. File may not exist.\n";
exit(1);
}
while (!file.eof()){
file >> word; //grab a word from the file one at a time
insertWordDistinct(changeToLowerCase(word));
//insertWord(word); //for debugging
//cout << word <<'\n'; //print word - for debugging
}
//printWordCloud(); //print word cloud - for debugging - works
file.close(); //always make sure to close file after read
}
void wordCloud::loadBlacklist(string fileName){
ifstream file; //variable for fileName
string bannedWord; //string to hold each word
file.open(fileName); //open file
if (!file) { //error handling if file does not load
cout << "Error: Can't open the file. File may not exist.\n";
exit(1);
}
while (!file.eof()){
file >> bannedWord; //grab a word from the file one at a time
if (bannedWord.empty()){ //error handling if file is empty
cout << "File is empty!!\n";
exit(1);
}
insertWord(changeToLowerCase(bannedWord));
//cout << bannedWord << '\n'; //print blacklist words - for debugging
}
//printBlacklist(); //print blacklist - for debugging - works
file.close(); //always make sure to close file after read
}
我注意到,如果我将previous = NULL 放在free() 之前,我的程序不会崩溃,也不会出现任何 dll 内存处理错误。事实上,我可以完全删除free(),它似乎工作得很好。我只是不知道这是否是正确的方法。在我看来,如果我只是将一个节点指向 NULLfree() 或delete() 来终止节点感到不安。如果我错了,请纠正我,或者请直接指出我的权利。
差不多,这有什么问题:
wordNode *previous, *temp = head;
while (temp != NULL){
if (word == temp->myWord){
temp->freq_count++;
previous = temp;
temp = temp->next;
delete(previous);
}
}
我可能做错了,但基本上我只需要找到插入列表中的每个单词的频率,然后删除包含该单词的多个节点,直到只剩下频率计数最高的节点打印。我正在尝试在insertWordDistinct(string word) 中执行此操作以完成此操作。只是不知道该怎么做。
【问题讨论】:
-
这是一个错误的描述!
-
“我开始使用 free() 是因为
delete()也给我带来了内存处理问题” - 根本无法描述该标志的 red 程度。如果你没有malloc(),你就不需要free()(你也不需要malloc(),所以都不应该在这段代码中)。您现在不妨说一下这是否适用于学术界,因为这将有助于避免随之而来的“您使用错误的容器”cmets 的暴风雪。 -
230 LOC 不是 SSCCE。请编辑您的问题,以便更容易回答。
-
关于单词排序和频率计算,如果您对标准库有充分的信心和信誉,I strongly advise you use it。
-
@Johnsyweb - 我发布了我所有的代码,因为老实说,我不知道我的所有错误到底在哪里导致我的程序因内存错误而崩溃。我尝试调试,但最终在我的代码中的不同点处中断,这些点对我来说甚至不知道问题可能是什么。
标签: c++ sorting linked-list word-frequency word-cloud