Advertisement
MeekoSoup

CS-21 - p5

Mar 28th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. //p5.cpp
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <vector>
  6. #include "hashtable.h"
  7.  
  8. int main() {
  9.   int input=0;
  10.   HashTable hash(10);
  11.   std::cout << "Hash Tables\n";
  12.  
  13.   while (std::cin >> input) {
  14.     std::cout << input << " = " << hash.hash(input) << std::endl;
  15.     hash.insert(input, std::to_string(input));
  16.   }
  17.  
  18.   return 0;
  19. };
  20.  
  21. //hashtable.h
  22. #ifndef HASHTABLE__H
  23. #define HASHTABLE__H
  24. #include <vector>
  25. #include <string>
  26. #include "record.h"
  27. #include <cmath>
  28.  
  29. class HashTable {
  30.  public:
  31.   HashTable(int=100);
  32.   int hash(int); // hash value for key
  33.   int hash(Record &); // hash value for record
  34.   void insert(int,std::string); // insert copy of record
  35.   std::string to_string(); // returns string of current hash
  36.  private:
  37.   int m; // capacity of hash
  38.   std::vector<Record> A[]; // hash
  39. };
  40.  
  41. #endif
  42.  
  43. //hashtable.cpp
  44. #include <iostream>
  45. #include <vector>
  46. #include <cmath>
  47. #include <string>
  48. #include "record.h"
  49. #include "hashtable.h"
  50.  
  51. const double C=(sqrt(5)-1)/2; // hash constant
  52.  
  53. HashTable::HashTable(int size) {
  54.   m=size+1; // capacity of hash (zero isn't used, so +1 is added)
  55.   std::vector<Record> A[m];
  56. }
  57.  
  58. void HashTable::insert(int id, std::string data) {
  59.   A[hash(id)].push_back(Record(id,data));
  60. }
  61.  
  62. int HashTable::hash(int k) {
  63.   return (int)floor(m*(k*C-floor(k*C)));
  64. }
  65.  
  66. int HashTable::hash(Record &record) {
  67.   int k=record.id;
  68.   return (int)floor(m*(k*C-floor(k*C)));
  69. }
  70.  
  71. std::string HashTable::to_string() {
  72.   std::string str="";
  73.   for (int i=0; i<m; i++) {
  74.     str+="A["+std::to_string(i)+"]\n";
  75.     for (int j=0; j<A[i].size(); j++) {
  76.       str+="["+std::to_string(j)+"]={"+A[i][j].data;
  77.       if (j<A[i].size()-1)
  78.         str+=", ";
  79.     }
  80.     str+="}\n";
  81.   }
  82.   return str;
  83. }
  84.  
  85. //record.h
  86. #ifndef RECORD__H
  87. #define RECORD__H
  88.  
  89. class Record {
  90.  public:
  91.   int id;
  92.   std::string data;
  93.   Record();
  94.   Record(int,std::string);
  95.   Record* clone();
  96. };
  97.  
  98. #endif
  99.  
  100. //record.cpp
  101. #include <iostream>
  102. #include <string>
  103. #include "record.h"
  104.  
  105. Record::Record() {
  106.   id=0;
  107.   data="";
  108. }
  109.  
  110. Record::Record(int id, std::string data) {
  111.   this->id=id;
  112.   this->data=data;
  113. }
  114.  
  115. Record *Record::clone() {
  116.   Record *rec = new Record(id, data);
  117.   return rec;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement