【发布时间】:2014-03-31 15:18:55
【问题描述】:
我正在开发一个词云 C++ 程序,该程序假设从文本文件中填充单词列表,将单词放入链接列表中,然后按频率对列表进行排序,并仅使用一个实例打印列表单词和单词旁边的频率。
我完成的是读取文件、填充链接列表并打印它。但是,我无法从 main 而不是 wordCloud 方法loadWordCloud() 打印列表。我的课程还不强,所以我确信这是我的问题。但基本上我想从 main 调用 printWordCloud() 方法,而不是从 loadWordCloud() 打印。当我在调用getList.loadWordCloud(myFile) 之后输入getList.printWordCloud() 时,我得到一个空列表。我知道为什么会这样,但我不知道如何从loadWordCloud() 中实际保存列表,以便稍后打印。我试图避免返回语句并保持方法无效。
这是我的课程:
class wordNode{
public:
string myWord;
int freq_count;
bool black_list;
wordNode *next;
wordNode(string aWord);
~wordNode(void){};
};
wordNode::wordNode(string aWord){
myWord = aWord;
next = NULL;
freq_count = 0;
}
class wordCloud{
public:
wordNode *head;
int size;
wordNode *nextWord;
wordCloud(void);
~wordCloud(void){};
void insertWord(string aWord) { insertWord(aWord, false); }
void insertWord(string aWord, bool blacklist);
/*void insertWordDistinct(string aWord);
void loadBlacklist(string fileName);*/
void loadWordCloud(string fileName);
//void printBlacklist(void); // Print the words in the blackList
void printWordCloud() { printWordCloud(1); }
void printWordCloud(int freq); // Print the words with freq or greater freq_count
/*void Initialize(void) { nextWord = head; }
void GetNextWord(string theWord, int *freq_count);
void freqSort(string sortOrder); // 'A' = ASC, 'D' = DESC
private:
void properlyHandleHeadofList(string sortOrder);
void properlyHandleRestofList(string sortOrder);
string upperCase(string text);
string lowerCase(string text);*/
};
/*void wordCloud::GetNextWord(string theWord, int *freq_count){
wordCloud aWord;
aWord.insertWord(theWord);
}*/
wordCloud::wordCloud(void){
head = NULL;
size = 0;
nextWord = NULL;
}
void wordCloud::insertWord(string aWord, bool blacklist){
wordNode *newWord = new wordNode(aWord);
if (head == NULL)
head = newWord;
else{
newWord->next = head;
head = newWord;
}
size++;
}
void wordCloud::printWordCloud(int freq){
wordNode *temp;
if (head == NULL)
cout << "No Word Cloud" << endl;
else{
temp = head;
while (temp != NULL){
cout << temp->myWord << endl;
temp = temp->next;
}
}
system("pause");
}
void wordCloud::loadWordCloud(string fileName){
ifstream file; //variable for fileName
string word;
wordCloud newList;
//newList.Initialize();
file.open(fileName); //open file
while (!file.eof()){
file >> word; //grab a word from the file one at a time
transform(word.begin(), word.end(), word.begin(),
::tolower); //automatically change words to lowercase
newList.insertWord(word);
size++; //increment size
//cout << word <<'\n'; //print word - for debugging
}
newList.printWordCloud(); //print word cloud - debugging - works
cout << size << '\n'; //print size - for debugging - works
file.close();
system("pause");
}
这是我的主要内容:
int main(){
string myFile = "words.txt";
wordCloud getList;
//For debugging
//list.insertWord("word");
//list.insertWord("more");
getList.loadWordCloud(myFile);
}
这个计划还远未完成。我还必须找出如何处理词频,并在一个词出现两次时删除一个节点,但现在,我只想知道我是如何从 main 打印的。任何帮助将不胜感激。
【问题讨论】:
-
在
loadWordCloud中,您加载到临时对象而不是当前对象。因此。摆脱newList。 -
这绝对有效。如此简单。然而,这让我失去了尺寸。该文件有 472 个字,当我摆脱 newList 时,现在我的大小计数是 944,所以它翻了一番。你能指出为什么它现在翻倍吗?
-
我已经写了这个作为答案,并根据您的评论进行了调整。
标签: c++ string class methods linked-list