Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. const int n = 100;
  7.  
  8. int hashFunc(string str)
  9. {
  10.     int i = 0, temp1 = 0, temp2 = 1;
  11.  
  12.     for (int i = 0; i < str.length(); i++)
  13.     {
  14.         temp1 += pow(str[i] - 'a', temp2);
  15.         temp2++;
  16.     }
  17.     i = temp1 % n;
  18.     return i;
  19. }
  20.  
  21. class MyHash
  22. {
  23.     struct MyStruct
  24.     {
  25.         string str;
  26.     };
  27.     public: MyStruct obj[n];
  28.  
  29.     public: void addMyStruct(string str)
  30.     {
  31.         obj[hashFunc(str)].str = str;
  32.     }
  33.  
  34.     public: void ShowMyStruct()
  35.     {
  36.        
  37.         for (int i = 0; i < n; i++)
  38.             if (obj[i].str != "")
  39.             {
  40.                 cout << i << " " << obj[i].str << '\n';
  41.             }
  42.            
  43.     }
  44. };
  45.  
  46. int main(int argc, char* argv[])
  47. {
  48.     MyHash a;
  49.     string str;
  50.     int n = 100;
  51.  
  52.     cout << "Enter String - ";
  53.     getline(cin, str);
  54.     a.addMyStruct(str);
  55.  
  56.     cout << "Enter String - ";
  57.     getline(cin, str);
  58.     a.addMyStruct(str);
  59.  
  60.     a.ShowMyStruct();
  61.  
  62.     system("pause");
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement