cresenia1988 0 Newbie Poster

Sorry, this might be abit of a rush and sudden. I have to finish this program in 3 hours time. I would appreciate if there's any kind soul willing to help me with the code. Thank you.

Output of the program should be like this.

Top popular 12 words

1 of 6

2 the 5

3 all 3

4 people 3

5 some 3

6 times 3

7 and 2

8 can 1

9 fool 1

10 not 1

11 them 1

12 you 1

Below is the text file I'm suppose to read.

===infile.txt===
you can fool some of the people
some of the times, and all of the people some of
the times and not all of the people all of them times.

===Word.h===

#include <iostream>
#include <fstream>

using namespace std;

class Word
{
    public:
        Word();
        Word (char *);

        int getSumASCII () const;
        void increment ();
        int getCount () const;
        char* getWord () const;
        int getKey () const;

        void setCount (int);
        void setWord (char*);
        void setKey (int);



    private:
        char *w;
        int count;

        int sumASCII () const;
        int n;
};

===Word.cpp===

#include "Word.h"

Word::Word()
{
    count = 0;
}

Word::Word (char *w)
{
    this -> w = new char [strlen(w) + 1];
    strcpy(this -> w, w);
    count = 1;
}

int Word::getSumASCII () const
{
    int sum = 0;
    char * p = &w[0];
    while(*p != '\0')
    {
        sum += *p;
        p++;
    }
    return sum;    
}

void Word::increment ()
{
    ++count;
}

int Word::getCount () const
{
    return count;
}

char* Word::getWord () const
{
    return w;
}

int Word::getKey () const
{
    return n;
}

void Word::setCount (int count)
{
    this -> count = count;
}

void Word::setWord (char *p)
{
        w = p;
}

void Word::setKey (int n)
{
    this -> n = n;
}

===HashTable.h===

#include "Word.h"

class HashTable
{
    public:
        HashTable();
        HashTable(int);

        void insertion (Word);
        void printTable () const;

    private:
        Word* wordArray;
        int size;
};

===HashTable.cpp===

#include "HashTable.h"

HashTable::HashTable()
{

}

HashTable::HashTable(int size)
{
    wordArray = new Word[size]; // insertion the size of the table

    char *ww = new char [20];

    for(int i = 0; i < size; i++)
    {

        wordArray[i].setWord ("\0");
        wordArray[i].setCount(0);
    }

    this-> size = size;
}        

void HashTable::insertion (Word w)
{
    int n = w.getSumASCII () % size;
    int i = n;

    bool found = false;
    bool collision = false;

    while (!found)
    {

        if (wordArray [i].getCount() == 0)
        {

            cout << w.getWord () << "\t";

            cout << "with % value = " << n << " inserted"
                << endl;

            wordArray [i] = w;
            wordArray[i].setKey (n);
            found = true;
        }
        else if (strcmp (wordArray[i].getWord (), w.getWord ()) == 0 && w.getCount() != 0)
        {
            wordArray [i].increment ();
            cout << w.getWord () << "\t";


            cout << "increase count" << endl;

            found = true;
        }
        else
        {
            collision = true;
            ++i;
            i = i % size;
        }
    }

    if (collision)
    {
        cout << w.getWord () << "\t";

        cout << "with % value = " << n 
            << " inserted with collisions"
            << endl; 
    }
}

void HashTable::printTable () const
{    
    double num, rate;

    cout <<endl
        << "Summary of Hash table, position occupied" 
        << endl << endl;

    cout << "Element" 
        << "\t" << "\t"
        << "Word" << "\t"
        << "Sum ASCII" << "\t"
        << "Mod 15" << "\t" << "\t"
        << "Count" << endl;

    for (int i = 0; i < size; i++)
    {
        if(wordArray[i].getCount () == 0)
        {

        }
        else
        {
        cout << "Table ["
             << i << "]\t"
             << wordArray [i].getWord () << "\t"
             << wordArray [i].getSumASCII ()
             << "\t" << "\t"
             << wordArray [i].getKey () << "\t" << "\t"
             << wordArray [i].getCount () << "\t"
             << endl; 
             ++num;
        }     
    }
    cout << endl;
    rate = (num /size) * 100;
    cout << "Occupancy rate: " << rate << "%";
}

===BST.h===

#include "HashTable.h"

class BST
{
    public:
        BST ();
        ~BST ();

        void insert (Word *); //insert hash table
        bool findNode (Word) const;
        void printBST () const;

    private:
        struct Node;
        typedef Node* NodePtr;

        struct Node
        {
            Word data;
            NodePtr left, right;
        };

        NodePtr root;

        int compareWP (Word, Word) const;

        void insert (NodePtr&, Word);
        bool findNode (NodePtr, Word) const;
        void inorderPrint (NodePtr) const;
};

===BST.cpp===

#include "BST.h"

BST::BST()
{
    root = NULL;
}

BST::~BST()
{
    destroy (root);
        //destroy not declared
}

void BST::insert (Word * w) //inserting hash table
{
    insert (root, w);
        //Error will occur - resulted no matching function for call to 'BST::insert (BST::Node*&, Word*&)'

}

bool BST::findNode (Word w) const
{
    return findNode (root, w);
}

void BST::printBST () const
{
    inorderPrint (root);
}

/*int BST::compareWP (Word w1, Word w2) const
{
    //I'm wondering must I really compare the words?
}*/

void BST::insert (NodePtr& root, Word w)
{
    if (root == NULL)
    {
        NodePtr temp = new Node;
        temp -> data = w;
        temp -> left = NULL;
        temp -> right = NULL;

        root = temp;
    }
    else if (compareWP (root -> data, w) > 0)
        insert (root -> left, w);
    else
        insert (root -> right, w);
}

bool BST::findNode (NodePtr root, Word w) const
{
    if (root == NULL)
        return false;
    else
    {
        int k = compareWP (root -> data, w);

        if (k == 0)
            return true;
        else if (k > 0)
            return findNode (root -> left, w);
        else
            return findNode (root -> right, w);
    }
}

void BST::inorderPrint (NodePtr) const
{

}

===Main.cpp===

#include "HashTable.h"

int main()
{
    int size;
    int num;
    char* filename = new char [20];
    fstream fin;

    cout << "Enter the file name for analysis: ";
    cin >> filename;
    cout << endl;

    fin.open(filename, ios::in);

    if(!fin)
    {
        cout << "File unable to open!" << endl;
        exit(1);
    }

    cout << "Enter the size of hash table: ";
    cin >> size;

    HashTable table (size);

    cout << "How many top popular words?: ";
    cin >> num;


    cout << "Analysis of insertion" 
        << endl << endl;

    char* p = new char [20];

    while(fin >> p)
    {
        //p = strtok (NULL, " .,?:;");
        Word w(p);
        table.insertion (w);      
    }

    table.printTable ();

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.