DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Printing a Hash Table (http://www.daniweb.com/forums/thread15574.html)

coolmel55 Dec 14th, 2004 5:23 pm
Printing a Hash Table
 
I need to create a function that will allow me to print a hash table below is my code. There are 5 files all together: main.cpp, hash.h, hash.cpp, rec.h, rec.cpp.


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "Rec.h"
#include "Hash.h"

using namespace std;

int main()
{
    char filename[16];
    string myString;
    int lineNum = 0;
        CHash H1;
        CRec *rptr;

    //system("clear");
    cout << "Welcome to the hash table program!" << endl;
    cout << "********************************************" << endl;
    cout << "\nPlease enter a filename which we will build "
        << " hash table for: ";
    cin >> filename;

    fstream instream(filename);

    if(instream.fail())
    {
            cout << "Input file opening failed.\n";
            exit(1);
    }

    getline(instream, myString);
    istringstream istr(myString);

    while (!instream.eof())
    {
            lineNum++;
                        rptr = new CRec;
                        rptr ->set_data(lineNum, myString);
                        H1.add(rptr);
           

            getline(instream, myString);
            istringstream istr(myString);

            while (istr >> myString)
            {
                                lineNum++;
                                rptr = new CRec;
                                rptr ->set_data(lineNum, myString);
                                H1.add(rptr);                   
                        }

        }
 
        instream.close();

        return 0;
}

---------------new file Rec.h-------------------------------
#ifndef CREC_H
#define CREC_H
#include <string>

using namespace std;

class CHash;

class CRec
{
        friend class CHash;

public:
        CRec();
        ~CRec();
        void set_data(int, string);
        void get_data(int&, int&, string&);
        int makeKey(string s);


private:
        int key;
        int lineNum;
        string myString;
};
#endif

---------------new file Rec.cpp-------------------------------
#include "Rec.h"
#include <string>

using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRec::CRec()
{

}

CRec::~CRec()
{

}

void CRec::set_data(int l, string s)
{
        key = makeKey(s);
        lineNum = l;
        myString = s;
}

void CRec::get_data(int &k, int &l,  string &s)
{
        k = key;
        l = lineNum;
        s = myString;
}

int CRec::makeKey(string s)
{
        int sum=0;
        int num_in_word = s.length();

        for (int i = 0; i < num_in_word; i++)
                sum = sum + s[i];

        return(sum % num_in_word);
}

---------------new file Hash.h-------------------------------
#ifndef CHASH_H
#define CHASH_H

#include "Rec.h"

//const int size = 7;

class CHash
{
public:
        CHash(); //fixed size =7 for illustration
        ~CHash();
        int add(CRec *r); //hashes and stores
        CRec* get(int k); //hashes and retreives
        void output();
        void print();

private:
        CRec *tab[7]; //sttrage for records
        int used[7]; //indicates if used
        int size;
};
#endif

---------------new file Hash.cpp-------------------------------
#include "Hash.h"
#include "Rec.h"
#include <string>
#include <iostream>

using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHash::CHash()
{
        size=7;
        for(int x=0; x<size; x++)
                used[x]=0;
}

CHash::~CHash()
{

}

int CHash::add(CRec *r)
{
        int ret = 0;
        int ind = r->key%size;
        int count = 0;
        int done = 0;

        while(count < size && !done)
        {
                if(used[ind] == 0)
                {
                        tab[ind] = r;
                        used[ind] = 1;
                        done = 1; ret = 1;
                }
                else
                {
                        ind = (ind + 1) % size;
                        count++;
                }
        }
        return ret;
}

void CHash::print()
{

}

CRec *CHash::get(int k)
{
        CRec *ret;       
        int ind = k % size;
        int count = 0;
        int done = 0;
       
        while(count < size && !done)
        {
                if(tab[ind]->key == k)
                {
                        ret = tab[ind];
                        done = 1;
                }
                else
                {
                        ind = (ind + 1) % size;
                        count++;
                }
        }
        return ret;
}


All times are GMT -4. The time now is 5:34 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC