| | |
Printing a Hash Table
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 40
Reputation:
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.
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Hash Table Implementation (C++)
- 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
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets




