Printing a Hash Table

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Sep 2004
Posts: 40
Reputation: coolmel55 is an unknown quantity at this point 
Solved Threads: 1
coolmel55 coolmel55 is offline Offline
Light Poster

Printing a Hash Table

 
0
  #1
Dec 14th, 2004
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.

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include "Rec.h"
  7. #include "Hash.h"
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. char filename[16];
  14. string myString;
  15. int lineNum = 0;
  16. CHash H1;
  17. CRec *rptr;
  18.  
  19. //system("clear");
  20. cout << "Welcome to the hash table program!" << endl;
  21. cout << "********************************************" << endl;
  22. cout << "\nPlease enter a filename which we will build "
  23. << " hash table for: ";
  24. cin >> filename;
  25.  
  26. fstream instream(filename);
  27.  
  28. if(instream.fail())
  29. {
  30. cout << "Input file opening failed.\n";
  31. exit(1);
  32. }
  33.  
  34. getline(instream, myString);
  35. istringstream istr(myString);
  36.  
  37. while (!instream.eof())
  38. {
  39. lineNum++;
  40. rptr = new CRec;
  41. rptr ->set_data(lineNum, myString);
  42. H1.add(rptr);
  43.  
  44.  
  45. getline(instream, myString);
  46. istringstream istr(myString);
  47.  
  48. while (istr >> myString)
  49. {
  50. lineNum++;
  51. rptr = new CRec;
  52. rptr ->set_data(lineNum, myString);
  53. H1.add(rptr);
  54. }
  55.  
  56. }
  57.  
  58. instream.close();
  59.  
  60. return 0;
  61. }
  62.  
  63. ---------------new file Rec.h-------------------------------
  64. #ifndef CREC_H
  65. #define CREC_H
  66. #include <string>
  67.  
  68. using namespace std;
  69.  
  70. class CHash;
  71.  
  72. class CRec
  73. {
  74. friend class CHash;
  75.  
  76. public:
  77. CRec();
  78. ~CRec();
  79. void set_data(int, string);
  80. void get_data(int&, int&, string&);
  81. int makeKey(string s);
  82.  
  83.  
  84. private:
  85. int key;
  86. int lineNum;
  87. string myString;
  88. };
  89. #endif
  90.  
  91. ---------------new file Rec.cpp-------------------------------
  92. #include "Rec.h"
  93. #include <string>
  94.  
  95. using namespace std;
  96. //////////////////////////////////////////////////////////////////////
  97. // Construction/Destruction
  98. //////////////////////////////////////////////////////////////////////
  99.  
  100. CRec::CRec()
  101. {
  102.  
  103. }
  104.  
  105. CRec::~CRec()
  106. {
  107.  
  108. }
  109.  
  110. void CRec::set_data(int l, string s)
  111. {
  112. key = makeKey(s);
  113. lineNum = l;
  114. myString = s;
  115. }
  116.  
  117. void CRec::get_data(int &k, int &l, string &s)
  118. {
  119. k = key;
  120. l = lineNum;
  121. s = myString;
  122. }
  123.  
  124. int CRec::makeKey(string s)
  125. {
  126. int sum=0;
  127. int num_in_word = s.length();
  128.  
  129. for (int i = 0; i < num_in_word; i++)
  130. sum = sum + s[i];
  131.  
  132. return(sum % num_in_word);
  133. }
  134.  
  135. ---------------new file Hash.h-------------------------------
  136. #ifndef CHASH_H
  137. #define CHASH_H
  138.  
  139. #include "Rec.h"
  140.  
  141. //const int size = 7;
  142.  
  143. class CHash
  144. {
  145. public:
  146. CHash(); //fixed size =7 for illustration
  147. ~CHash();
  148. int add(CRec *r); //hashes and stores
  149. CRec* get(int k); //hashes and retreives
  150. void output();
  151. void print();
  152.  
  153. private:
  154. CRec *tab[7]; //sttrage for records
  155. int used[7]; //indicates if used
  156. int size;
  157. };
  158. #endif
  159.  
  160. ---------------new file Hash.cpp-------------------------------
  161. #include "Hash.h"
  162. #include "Rec.h"
  163. #include <string>
  164. #include <iostream>
  165.  
  166. using namespace std;
  167.  
  168. //////////////////////////////////////////////////////////////////////
  169. // Construction/Destruction
  170. //////////////////////////////////////////////////////////////////////
  171.  
  172. CHash::CHash()
  173. {
  174. size=7;
  175. for(int x=0; x<size; x++)
  176. used[x]=0;
  177. }
  178.  
  179. CHash::~CHash()
  180. {
  181.  
  182. }
  183.  
  184. int CHash::add(CRec *r)
  185. {
  186. int ret = 0;
  187. int ind = r->key%size;
  188. int count = 0;
  189. int done = 0;
  190.  
  191. while(count < size && !done)
  192. {
  193. if(used[ind] == 0)
  194. {
  195. tab[ind] = r;
  196. used[ind] = 1;
  197. done = 1; ret = 1;
  198. }
  199. else
  200. {
  201. ind = (ind + 1) % size;
  202. count++;
  203. }
  204. }
  205. return ret;
  206. }
  207.  
  208. void CHash::print()
  209. {
  210.  
  211. }
  212.  
  213. CRec *CHash::get(int k)
  214. {
  215. CRec *ret;
  216. int ind = k % size;
  217. int count = 0;
  218. int done = 0;
  219.  
  220. while(count < size && !done)
  221. {
  222. if(tab[ind]->key == k)
  223. {
  224. ret = tab[ind];
  225. done = 1;
  226. }
  227. else
  228. {
  229. ind = (ind + 1) % size;
  230. count++;
  231. }
  232. }
  233. return ret;
  234. }
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC