Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "stdafx.h"
- #include "iostream"
- #include <list>
- #include <iterator>
- #include "time.h"
- #include <string>
- using namespace std;
- class ListString //класс список указателей
- {
- private:list<string*>strList; //кол-во строк
- list<string*>::iterator i;
- public:
- ListString() {};
- ListString(int n, int rangeChar)
- {
- string str;
- int len;
- for (int i = 0; i < n; i++)
- {
- len = 1 + rand() % 10;
- for (int j = 0; j < len; j++)
- {
- str += (char)('a' + rand() % rangeChar);
- }
- strList.push_back(new string(str)); //добавляем в конец
- str = "";
- }
- cout << "List generation completed!" << endl;
- }
- void ofStream()
- {
- string* str;
- i = strList.begin();
- while (i != strList.end())
- {
- str = *i;
- cout << *str << endl;
- i++;
- }
- }
- string getValue(int step)
- {
- string* str;
- i = strList.begin();
- advance(i, step); //приращение итератора по смещению
- str = *i;
- return *str;
- }
- int insert(string str, int counter)
- {
- int check = 0;
- string* str1;
- i = strList.begin();
- while (i != strList.end())
- {
- str1 = *i;
- i++;
- counter++;
- if (str == *str1)
- {
- check = 1;
- break;
- }
- }
- if (check != 1)
- {
- strList.push_back(new string(str)); //добавляем в конец
- //return 0;
- }
- return counter;
- }
- };
- class Hashtable
- {
- private: ListString* table;
- public:
- void tablegenerator(ListString myList, int n, int q)
- {
- int hsum, counter = 0;
- table = new ListString[q];
- for (int i = 0; i < n; i++)
- {
- string str = myList.getValue(i);
- hsum = 0;
- for (int j = 0; j < str.length(); j++)
- {
- hsum = (hsum * 31 + str[j]) % q; //полином по схеме Горнера
- }
- counter += table[hsum].insert(str, counter);
- }
- cout << endl << counter << endl;
- for (int i = 0; i < q; i++)
- {
- table[i].ofStream();
- }
- }
- };
- int main()
- {
- srand(time(NULL));
- int n, rangeChar, q;
- cout << "Please enter a range of characters (1-26): " << endl;
- cin >> rangeChar;
- cout << "Please enter the number of rows (<=100000): " << endl;
- cin >> n;
- cout << "Please enter the size of the hash table" <<
- "(a prime number that must be greater than the number of rows, for example, 10007, 50651 or 100003): ";
- cin >> q;
- ListString myList(n, rangeChar);
- myList.ofStream();
- Hashtable mytable;
- mytable.tablegenerator(myList, n, q);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement