0

我创建了一个程序,它将读取文本文件并将单词作为字符串放入链接列表,以及它们在整个文本文件中的频率计数。它只打印每个单词的出现次数及其出现的总次数。

我的程序还加载了一个黑名单,它应该将黑名单链表与词云(或词频)链表进行比较,然后从词频列表中删除列入黑名单的词。

我已经尝试过几种方法。以下是我的第三个版本。我想要做的是为每个节点添加一个布尔值,当一个节点等于黑名单中的一个单词时,布尔值将为真。但是,我无法使用以下代码正确打印它。我已经搜索过了,似乎找不到正确的语法来将布尔值添加到链表中的节点。

编辑#3:

void wordCloud::compareWith(wordCloud& wordList, wordCloud& badList){
wordNode *wordListTemp, *blacklistTemp, *temp = NULL;
unsigned int counter = 0;

for (blacklistTemp = badList.head; blacklistTemp; blacklistTemp = blacklistTemp->next){
    cout << blacklistTemp->myWord << "\n";
    for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){

        if (wordListTemp->myWord != blacklistTemp->myWord){

            wordListTemp->blacklist = false;
            if (wordListTemp->blacklist = false){
                cout << wordListTemp->myWord << " <"
                    << wordListTemp->freq_count << ">\n";
            }
        }
        else if (wordListTemp->myWord == blacklistTemp->myWord){
            cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";
            wordListTemp->blacklist = true;
            if (wordListTemp->blacklist = true)
                cout << wordListTemp->myWord << "\n";
        } 
    }
    //counter++;
    cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";
}

system("pause");
}

这并不完整,但据我所知。问题是它只打印 true if,并且不会打印任何 false if。即使我切换了值,它仍然只会打印真正的 if。所以我假设我正在处理这个错误。“标记”节点为真和“标记”节点为假的正确方法是什么?所有 cout 都用于调试目的。稍后我将删除或评论这些内容。

4

2 回答 2

0

首先,您始终可以逐步调试以查看代码的哪一部分冻结了您的comp。检测内存泄漏的更好方法是使用Valgrind

附带说明一下,我会将该比较函数实现为比较运算符,并为它们的节点也实现一个比较运算符(为方便起见)。这样做会稍微划分代码,并有助于稍后了解您的问题所在。这也是一种更好的方法(更具可读性,OOP-y 等)。

于 2014-03-04T08:47:16.423 回答
0

最后!!

通过大量老式调试和 cout 语句,我终于得到了我想要的。我知道这对某些人来说可能很容易,但由于对链表不太熟悉,这对我来说是一个相当大的过程。

在我试图从链表中删除黑名单链表中看到的单词之前wordList。后来我决定尝试将布尔值 true 添加到 中的节点wordList,然后将我的打印函数调整为不打印值为 true 的节点。我还必须在insertWord()和我的freqSort()函数中调整一些东西,但真正包含的只是在创建新节点时添加一个指向布尔值的指针。

我的成员函数是void wordCloud::compareWith(wordCloud& wordList, wordCloud& badList),并且是我的 wordCloud 类的一部分。这是以下定义:

void wordCloud::compareWith(const wordCloud& wordList, const wordCloud& badList){
wordNode *wordListTemp, *blacklistTemp;
unsigned int counter = 0;

//loop that advances wordListTemp
for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){
    blacklistTemp = badList.head;

    //loop advances blacklistTemp - compares links in wordList to badList(blacklist)
    //and sets the node to true if myWord equals any word in the blacklist
    while (blacklistTemp){          
        if (wordListTemp->myWord == blacklistTemp->myWord){
            wordListTemp->blacklist = true; 
            counter++;
        }
        blacklistTemp = blacklistTemp->next;
    }

    //for debugging
    //cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";     
}

/*********************  All for debugging  ***************************************
cout << "True:\n\n";
wordListTemp = wordList.head;       //reset wordListTemp to head    

while (wordListTemp){               //print blacklisted words from wordList
    if (wordListTemp->blacklist == true){
        cout << wordListTemp->myWord << " <"
            << wordListTemp->freq_count << ">\n";
    }
    wordListTemp = wordListTemp->next;
}
//prints total words blacklisted
cout << "There are " << counter << " blacklisted words.";                   

cout << "\n\nFalse:\n\n";
wordListTemp = wordList.head;       //reset wordListTemp to head    
counter = 0;

while (wordListTemp){               //print non-blacklisted words from wordList
    if (wordListTemp->blacklist == false){
        cout << wordListTemp->myWord << " <"
            << wordListTemp->freq_count << ">\n";
        counter++;
    }
    wordListTemp = wordListTemp->next;
}
//prints total words not blacklisted
cout << "There are " << counter << " words that are not blacklisted.\n";

system("pause");    
********************  End debugging *******************************************/    
}

所以基本上这是一个比较函数,它标记在另一个列表中找到的节点。运行良好并与所有其他选项一起测试。

于 2014-03-05T05:22:11.583 回答