Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int N = 157;
- string T[N]; //tablica
- int hash(string word); //funkcja haszująca
- string init(); //generuje losowy ciąg n-znakowy
- void dodaj(string word);
- void drukuj(); //wydruk całej tablicy (pozycja + słowo)
- int main(int argc, char *argv[])
- {
- srand(time(NULL));
- for(int i = 0; i < 20; i++)
- {
- string s = init();
- dodaj(s);
- }
- drukuj();
- system("pause");
- }
- int hash(string word)
- {
- int h = 0;
- for (int unsigned i = 0; i < word.length(); i++)
- {
- h = 2 * h + 1 - (word[i] & 1);
- }
- return h % N;
- }
- string init() //generuje losowy ciąg n-znakowy
- {
- int n = rand()%9 + 1;
- int i;
- string t = "";
- for(i = 0; i < n; i++)
- {
- t += (char)(rand()%28 + 65);
- }
- t += '\0';
- return t;
- }
- void dodaj(string word)
- {
- int index = hash(word);
- bool good = false;
- while (!good)
- {
- if (T[index].empty())
- {
- good = true;
- T[index] = word;
- }
- else
- {
- index++;
- index = index % N;
- }
- }
- }
- void drukuj()
- {
- for (int i = 0; i < N; i++)
- {
- if (!T[i].empty())
- {
- cout.width(4);
- cout << i;
- cout << ". " << T[i] << endl;
- }
- }
- }
Add Comment
Please, Sign In to add comment