Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. //*******************************************************************************************************************
  8. struct node;
  9.  
  10. int fsm(char *tab, int size);
  11. void add_values_to_fsm(int **fsm, bool in_a_row);
  12. int rabin(int *wz, int size);
  13. int count_modulo(int *array, int size, int from);
  14.  
  15. //*******************************************************************************************************************
  16.  
  17. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. //struktura danych
  19. struct node
  20. {
  21. string imie;
  22. double pesel;
  23. node* next;
  24.  
  25. void dodaj(string im, double pes, node *&wpis);
  26. void wyswietl(string im, node *tmp);
  27. };
  28.  
  29. //***********************************************************************
  30. //funkcje skladowe struktury
  31. void node::dodaj(string im, double pes, node *&wpis)
  32. {
  33. node *tmp = new node;
  34. tmp->imie = im;
  35. tmp->pesel = pes;
  36. tmp->next = wpis;
  37. wpis = tmp;
  38. }
  39.  
  40. //***********************************************************************
  41.  
  42. void node::wyswietl(string im, node *tmp)
  43. {
  44. while (tmp->imie != im)
  45. tmp = tmp->next;
  46.  
  47. cout << "Imie: " << tmp->imie << endl
  48. << "Pesel: " << tmp->pesel << endl;
  49. }
  50.  
  51. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52.  
  53. //*******************************************************************************************************************
  54. //main
  55. int main()
  56. {
  57. srand(time(NULL));
  58. char *signs = NULL;
  59. string imie;
  60. int ilosc_osob, pozycja, hit_ratio;
  61. double pesel;
  62. cout << "Podaj liczbe osob ";
  63. cin >> ilosc_osob;
  64. node **Dane = new node*[ilosc_osob];
  65. for (int i = 0; i < ilosc_osob; i++)
  66. Dane[i] = NULL;
  67.  
  68. int wybor;
  69. do
  70. {
  71. cout << "1. Dodaj nowa osobe" << endl
  72. << "2. Odczytaj dane osoby" << endl
  73. << "3. Szukanie wzorca automat skonczony" << endl
  74. << "4. Rabin" << endl
  75. << "0. Wyjscie" << endl;
  76.  
  77. cin >> wybor;
  78. switch (wybor)
  79. {
  80. case 1:
  81. cout << "Podaj imie osoby ";
  82. cin >> imie;
  83. cout << "Podaj pesel osoby ";
  84. cin >> pesel;
  85. pozycja = 0;
  86. for (int i = 0; i < imie.length(); i++)
  87. pozycja += (int)imie[i];
  88.  
  89. pozycja %= ilosc_osob;
  90. Dane[pozycja]->dodaj(imie, pesel, Dane[pozycja]);
  91. break;
  92.  
  93. case 2:
  94. cout << "Podaj imie osoby ktora chcesz znalesc ";
  95. cin >> imie;
  96. pozycja = 0;
  97. for (int i = 0; i < imie.length(); i++)
  98. pozycja += (int)imie[i];
  99.  
  100. pozycja %= ilosc_osob;
  101.  
  102. Dane[pozycja]->wyswietl(imie, Dane[pozycja]);
  103. break;
  104.  
  105. case 3:
  106. cout << "Podaj rozmiar tablicy znakow: ";
  107. int size;
  108. cin >> size;
  109.  
  110. signs = new char[size];
  111.  
  112. for (int i = 0; i < size; i++)
  113. signs[i] = rand()%25+97;
  114.  
  115. hit_ratio = fsm(signs, size);
  116. cout << "Ilosc trafien wynosi: " << hit_ratio << endl;
  117. break;
  118.  
  119. case 4:
  120. cout << "Podaj dlugosc wzorca ktory chcesz wyszukac: ";
  121. int dlugosc;
  122. cin >> dlugosc;
  123. int *wz = new int[dlugosc];
  124. for (int i = 0; i < dlugosc; i++)
  125. {
  126. cout << "Podaj " << i << " cyfre wzorca: ";
  127. cin >> wz[i];
  128. }
  129.  
  130. int hits = rabin(wz, dlugosc);
  131. cout << "Ilosc trafien wynosi: " << hits << endl;
  132. break;
  133. }
  134.  
  135. } while (wybor != 0);
  136.  
  137. return 0;
  138. }
  139.  
  140. //*******************************************************************************************************************
  141. //FSM flying spaghetti monster finite state machine
  142. int fsm(char *tab, int size)
  143. {
  144. string pattern = "";
  145. cout << "Podaj wzorzec ktory chcesz wyszukac: ";
  146. cin >> pattern;
  147.  
  148. int **fsm = new int *[3];
  149. for (int i = 0; i < 3; i++)
  150. fsm[i] = new int[size + 1];
  151.  
  152. int j = 0;
  153.  
  154. for (int i = 0; i < size; i++)
  155. {
  156. if (tab[i] == pattern[j])
  157. add_values_to_fsm(fsm, true);
  158.  
  159.  
  160. }
  161.  
  162. return 0;
  163. }
  164.  
  165. //***********************************************************************
  166.  
  167. void add_values_to_fsm(int **fsm, bool in_a_row)
  168. {
  169.  
  170. }
  171.  
  172. //*******************************************************************************************************************
  173. //rabin-karp
  174. int rabin(int *wz, int size)
  175. {
  176. int hits = 0;
  177. bool check = false;
  178. int wz_mod_digit = 0;
  179. int rozmiar; //17
  180. cout << "Podaj rozmiar wzorca: ";
  181. cin >> rozmiar;
  182. int *text = new int[rozmiar];
  183. int *modulo_results = new int[rozmiar];
  184.  
  185. for (int i = 0; i < rozmiar; i++)
  186. modulo_results[i] = -1;
  187.  
  188. /*int text[17];
  189. text[0] = 2;
  190. text[1] = 3;
  191. text[2] = 5;
  192. text[3] = 9;
  193. text[4] = 0;
  194. text[5] = 2;
  195. text[6] = 3;
  196. text[7] = 1;
  197. text[8] = 4;
  198. text[9] = 1;
  199. text[10] = 5;
  200. text[11] = 2;
  201. text[12] = 6;
  202. text[13] = 7;
  203. text[14] = 3;
  204. text[15] = 9;
  205. text[16] = 9;*/
  206.  
  207. for (int i = 0; i < rozmiar; i++)
  208. text[i] = rand()%9;
  209.  
  210. wz_mod_digit = count_modulo(wz, size, 0);
  211.  
  212. for (int i = 0; i <= (rozmiar - size); i++)
  213. modulo_results[i] = count_modulo(text, size, i);
  214.  
  215. for (int i = 0; i < rozmiar; i++)
  216. {
  217. if (wz_mod_digit == modulo_results[i])
  218. {
  219. check = false;
  220.  
  221. for (int j = 0; j < size; j++, i++)
  222. {
  223. if (wz[j] == text[i])
  224. check = true;
  225.  
  226. else
  227. {
  228. check = false;
  229. break;
  230. }
  231. }
  232. }
  233.  
  234. if (check == true)
  235. {
  236. hits++;
  237. i--;
  238. }
  239. }
  240.  
  241. return hits;
  242. }
  243.  
  244. //***********************************************************************
  245.  
  246. int count_modulo(int *array, int size, int from)
  247. {
  248. int digit = 0;
  249.  
  250. for (int i = (size - 1 + from), potega = 0; i >= from; i--, potega++)
  251. {
  252. digit += array[i] * (pow(10, potega));
  253. }
  254.  
  255. digit %= 13;
  256.  
  257. return digit;
  258. }
  259.  
  260. //*******************************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement