2014-03-04 36 views
0

我已经创建了一个程序,它将读取文本文件,并将文字作为字符串放入链接列表中,以及文本文件中的频率计数。它只显示每个单词出现的总次数的一个出现次数。将一个链接列表与另一个黑名单与Word频率列表进行比较C++

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

我试过这样做了几种方法。以下是我的第三个版本。我想要做的是为每个节点添加一个布尔值,并且当一个节点等于黑名单中的一个单词时,布尔值将为true。但是,我没有得到它与以下代码正确打印。我搜索了,我似乎无法找到正确的语法来添加布尔值到链接列表中的节点。

编辑#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"); 
} 

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

+1

这是一个天真的实现,但为什么不只是做两个while循环,一个在另一个之内?你在外部黑名单上查看,在内部,你将你当前的黑名单单词与你单词列表中的所有单词进行比较?它显然可以优化,但至少它会给你一个工作的开始。 –

+0

@o_weisman是的,也许我正在调查这太多。这实际上是我现在想要做的。在解决移植代码问题上我有点慢,所以如果有任何成功,我会回复。 – charlwillia6

+0

@o_weisman - 我曾尝试过这一点,但似乎没有得到任何地方。我发布了上面的代码,但程序仍然冻结。 – charlwillia6

回答

0

终于!!

随着大量的旧时尚调试和cout声明,我终于得到了我想要的。我知道这可能对一些人来说很容易,但对链接列表不太熟悉,这对我来说是相当的过程。

之前我试图删除在wordList链接列表中的黑名单链表中看到的单词。我后来决定尝试向wordList中的节点添加一个布尔值true,然后调整我的打印功能以不打印值为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 *******************************************/  
} 

所以基本上这是一个比较函数,标记在另一个列表中找到的节点。适用于所有其他选项并进行测试。

0

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

在附注上,我将实现该比较函数作为比较运算符,并为其节点实现一个比较运算符(为了方便起见)。这样做会将代码分开一点,并在稍后帮助您理解问题所在。这也是一个更好的方法(更可读,OOP-Y等)。

+0

不完全确定如何做到这一点,或完全明白你在说什么。 – charlwillia6

+0

哪部分你不明白? Valgrind或运营商? –

+0

作为比较运算符的运算符和实现。我知道比较运算符是什么,但不知道如何“实施”解决方案作为比较运算符。你在说重载吗? – charlwillia6

相关问题