Guest User

Untitled

a guest
Jan 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 157;
  8.  
  9. string T[N]; //tablica
  10.  
  11. int hash(string word); //funkcja haszująca
  12. string init(); //generuje losowy ciąg n-znakowy
  13. void dodaj(string word);
  14. void drukuj(); //wydruk całej tablicy (pozycja + słowo)
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. srand(time(NULL));
  19. for(int i = 0; i < 20; i++)
  20. {
  21. string s = init();
  22. dodaj(s);
  23. }
  24.  
  25. drukuj();
  26. system("pause");
  27. }
  28.  
  29. int hash(string word)
  30. {
  31. int h = 0;
  32. for (int unsigned i = 0; i < word.length(); i++)
  33. {
  34. h = 2 * h + 1 - (word[i] & 1);
  35. }
  36. return h % N;
  37. }
  38.  
  39. string init() //generuje losowy ciąg n-znakowy
  40. {
  41. int n = rand()%9 + 1;
  42. int i;
  43. string t = "";
  44. for(i = 0; i < n; i++)
  45. {
  46. t += (char)(rand()%28 + 65);
  47. }
  48. t += '\0';
  49. return t;
  50. }
  51.  
  52. void dodaj(string word)
  53. {
  54. int index = hash(word);
  55.  
  56. bool good = false;
  57. while (!good)
  58. {
  59. if (T[index].empty())
  60. {
  61. good = true;
  62. T[index] = word;
  63. }
  64. else
  65. {
  66. index++;
  67. index = index % N;
  68. }
  69. }
  70. }
  71.  
  72. void drukuj()
  73. {
  74. for (int i = 0; i < N; i++)
  75. {
  76. if (!T[i].empty())
  77. {
  78. cout.width(4);
  79. cout << i;
  80. cout << ". " << T[i] << endl;
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment