Advertisement
Pinkel

Haszowanie laborki

Jan 12th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cstdlib>
  4. #include <string.h>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. const int N = 157;
  9. string T[N];
  10. int licz = 0;
  11.  
  12.  
  13. int hash(char* slowo)
  14. {
  15. int sumaModulo = 0;
  16. for(int i=0; i<strlen(slowo) ;i++)
  17. {
  18. sumaModulo += pow(2,i) * slowo[i];
  19.  
  20. }
  21. return sumaModulo % N;
  22. } //funkcja haszująca
  23.  
  24.  
  25. char* init(){ //generuje losowy ciąg n-znakowy
  26. int n =rand()%9+1;
  27. int i;
  28. char *t = new char[n+1];
  29. for(i=0;i<n;i++)
  30. t[i] = (char)(rand()%28 + 65);
  31. t[i]='\0';
  32. return t;
  33. }
  34. void dodaj(char* slowo)
  35. {
  36.  
  37. int index = hash(slowo);
  38. if(T[index]== "")
  39. {
  40. T[index] = slowo;
  41. licz++;
  42. }
  43. else
  44. {
  45. for(int i = 1; i<N ; i++)
  46. {
  47. if(index + i < N)
  48. {
  49. if(T[index+i] == "")
  50. {
  51. T[index+i]=slowo;
  52. break;
  53. }
  54. }
  55. else
  56. {
  57. for(int x=0; x<index; x++)
  58. if(T[x]=="")
  59. {
  60. T[x]=slowo;
  61. break;
  62. }
  63. break;
  64. }
  65.  
  66. }
  67. }
  68.  
  69.  
  70. }
  71. void drukuj()
  72. {
  73. for(int i = 0; i< N ; i++)
  74. {
  75. cout <<"T["<<i<<"] = "<<T[i]<<endl;
  76. }
  77. cout << "Poprawne trafienia za pierwszym razem: "<<licz<<endl;
  78. cout << endl;
  79. } //wydruk całej tablicy (pozycja + słowo)
  80.  
  81.  
  82.  
  83.  
  84. int main(int argc, char *argv[])
  85. {
  86. srand(time(NULL));
  87. for(int i =0; i< N;i++)
  88. {
  89. char *s = init();
  90. dodaj(s);
  91. }
  92.  
  93.  
  94.  
  95. drukuj();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement