Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- const int n = 100;
- int hashFunc(string str)
- {
- int i = 0, temp1 = 0, temp2 = 1;
- for (int i = 0; i < str.length(); i++)
- {
- temp1 += pow(str[i] - 'a', temp2);
- temp2++;
- }
- i = temp1 % n;
- return i;
- }
- class MyHash
- {
- struct MyStruct
- {
- string str;
- };
- public: MyStruct obj[n];
- public: void addMyStruct(string str)
- {
- obj[hashFunc(str)].str = str;
- }
- public: void ShowMyStruct()
- {
- for (int i = 0; i < n; i++)
- if (obj[i].str != "")
- {
- cout << i << " " << obj[i].str << '\n';
- }
- }
- };
- int main(int argc, char* argv[])
- {
- MyHash a;
- string str;
- int n = 100;
- cout << "Enter String - ";
- getline(cin, str);
- a.addMyStruct(str);
- cout << "Enter String - ";
- getline(cin, str);
- a.addMyStruct(str);
- a.ShowMyStruct();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement