Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

c++ - mh.display() doesn't display anything and I don't know where my error is

I'm working on this project, and the display function is not displaying anything for some reason the arrays are empty. Can any one tell me why? When i cout << word, it is not blank, but when assigning word = mh.array[count].word it is throwing error string.length() to memory. No given error but that. Any help? (edited for adding my input) this is my input:

**

<REUTERS ... >
<DATE>26-FEB-1987 15:01:01.79</DATE><TOPICS><D>cocoa</D></TOPICS>
<PLACES><D>el-salvador</D><D>usa</D><D>uruguay</D></PLACES><PEOPLE></PEOPLE>
<ORGS></ORGS><EXCHANGES></EXCHANGES><COMPANIES></COMPANIES><UNKNOWN> ... </UNKNOWN><TEXT> ...
<TITLE>BAHIA COCOA REVIEW</TITLE>
<DATELINE> SALVADOR, Feb 26 - </DATELINE>
<BODY> 
Showers continued throughout the week in the Bahia cocoa zone, alleviating the drought since
...
...
Brazilian Cocoa Trade Commission after carnival which ends midday on February 27.
Reuter
&#3;
</BODY></TEXT>
</REUTERS>

The code is to extract between tags and and print out top 10 most frequent words. Unable to use vector as per instructions. (input has be cut for character minimization)

#include <iostream>
#include <string>
#include <algorithm> 
#include <fstream>
#include <cctype>
using namespace std; 
const int maxchars = 26; 
struct TrieNode
{
    bool isEnd; 
    unsigned int HeapIndex; 
    TrieNode* child[maxchars]; 
    unsigned int frequency; 
    TrieNode()
    {
        HeapIndex = -1; 
        isEnd = false; 
        frequency = 0; 
        for (int i = 0; i < 26; i++)
        {
            child[i] = NULL; 
        }
    }
};

struct HeapNode
{
    string word; 
    unsigned int frequency; 
    TrieNode* TN; 
    HeapNode()
    {
        frequency = 0; 
        TN = NULL; 
        word = ""; 
    }
};

class Trie
{
private: 
    TrieNode* root; 
public: 
    Trie(); 
    
    TrieNode* insert(string word); 
    TrieNode* search(string word); 
};

Trie::Trie()
{
    root = new TrieNode(); 
}

TrieNode* Trie::insert(string word)
{
    TrieNode* temp = root; 
    for (unsigned int i = 0; i < word.length(); i++)
    {
        if (temp->child[word[i] - 'a'] == 0)
        {
            
                TrieNode* node = new TrieNode();
                temp->child[word[i] - 'a'] = node;
            
            temp = temp->child[word[i] - 'a'];
        }
    }
    temp->frequency = 1; 
    temp->isEnd = 1; 
    return temp; 
}

TrieNode* Trie::search(string word)
{
    TrieNode* temp = root; 
    for (unsigned int i = 0; i < word.length(); i++)
    {
        if (temp->child[word[i] - 'a'] == NULL)
        {
            temp = temp->child[word[i] - 'a'];
            return NULL; 
            
        }
    }
    if (temp->isEnd)
    {
        return temp; 
        delete temp; 
    }
    else
    {
        return NULL; 
    }
}

class MinHeap
{
private: 
    int capacity, count; 
    HeapNode* array; 
public: 
    
    MinHeap(int capacity); 
    void Display(); 
    void minHeapify(int index); 
    void Build(int index); 
    void insert(TrieNode* TN, string word); 
    void TopKFrequentWord(string fileName, int k);
    
};




MinHeap::MinHeap(int capacity = 0)
{
    this->capacity = capacity;
    count = 0; 
    array = new HeapNode[capacity]; 
    capacity++;
}


void MinHeap::insert(TrieNode* TN, string word)
{
    if (TN->HeapIndex != -1)
    {
        array[TN->HeapIndex].word = word;
        array[TN->HeapIndex].frequency++;
        minHeapify(TN->HeapIndex);
    }
    else if (count < capacity)
    {
        array[count].word = word;
        array[count].frequency = 1;
        array[count].TN = TN;
        TN->HeapIndex = count;
        count++;
        Build(count);
    }
    else if (TN->frequency > array[0].frequency)
    {
        
        array[0].TN->HeapIndex = TN->HeapIndex;
        array[0].word = word;
        array[0].frequency = TN->frequency;
        array[0].TN = TN;

        minHeapify(0);
    }
}

void MinHeap::minHeapify(int index)
{
    int left = 2 * index + 1; 
    int right = 2 * index + 2; 
    int minIndex = index; 
    if (left < count && array[left].frequency < array[minIndex].frequency)
    {
        minIndex = left; 
    }
    else if (right < count && array[right].frequency < array[minIndex].frequency)
    {
        minIndex = right; 
    }
    else if (minIndex != index)
    {
        array[index].TN->HeapIndex = minIndex; 
        array[minIndex].TN->HeapIndex = index;
        swap(array[index], array[minIndex]); 
        minHeapify(minIndex); 
    }
}

void MinHeap::Build(int index)
{
    int n = count - 1; 
    for (int i = (n - 1) / 2; i >= 0; i--)
    {
        minHeapify(i); 
    }
}



void MinHeap::Display()
{
    for (int i = 0; i < capacity; i++)
    {
        
        cout << array[i].word << "  " << array[i].frequency << endl; //Here the arrays are

    }
}


void MinHeap::TopKFrequentWord(string fileName, int k)
{
    MinHeap mh;
    Trie T;
    string word;
    string line;

    
    for (int i = 0; i < 22; i++)
    {
        if (i >= 10)
        {
            fileName = "C:\Users\Kareem's Laptop\Desktop\Reuters-21578\reut2-0" + to_string(i) + ".sgm";

        }

        else if (i <= 9)
        {
            fileName = "C:\Users\Kareem's Laptop\Desktop\Reuters-21578\reut2-00" + to_string(i) + ".sgm";


        }
        ifstream inFile(fileName);
        if (!inFile)
        {
            cout << fileName << " did not open." << endl;
            exit(1);
        }

        bool found = true;

        while (inFile >> line)
        {

            size_t pos = line.find("<BODY>");

            if (pos != string::npos)
            {
                if (found)
                {
                    word = line.substr(pos + 6);
                
                    found = true;
                     
                    TrieNode* TN = T.search(word);

                    if (!TN)
                    {
                        TN = T.insert(word);
                    }
                    else
                    {
                        TN->frequency++;
                    }
                    mh.insert(TN, word);
                    
                
                }
            }

            
        }

        inFile.close();
    }
    mh.Display();

}
    
    
    





int main()
{
    MinHeap foo; 
    string fileName;
    
    foo.TopKFrequentWord(fileName, 10);
}
question from:https://stackoverflow.com/questions/65848313/mh-display-doesnt-display-anything-and-i-dont-know-where-my-error-is

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...