•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,844 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,773 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4560 | Replies: 0
![]() |
•
•
Join Date: Sep 2004
Posts: 40
Reputation:
Rep Power: 5
Solved Threads: 1
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;
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Reading txt file into Hash Table (C++)
- Hash Table template implementation help (C++)
- C++hash table lookin' 4 help~~ (C++)
- Pass XML file contents to a hash table. (Java)
- compile error-chained hash table c++ (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Help
- Next Thread: need help for programming

Linear Mode