Advertisement
olekturbo

Untitled

May 16th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <math.h>
  6. #define MLD 1000000000.0
  7.  
  8. int main()
  9. {
  10. int wzorzecCounter = 0;
  11. int tekstCounter = 0;
  12. struct timespec tp0, tp1;
  13.  
  14. //FILES
  15. int c, s, i;
  16. FILE *file;
  17. //WZORZEC
  18. file = fopen("wzorzec.txt", "r");
  19. if (file) {
  20. while ((c = getc(file)) != EOF)
  21. wzorzecCounter++;
  22. fclose(file);
  23. }
  24. //TEKST
  25. file = fopen("tekst.txt", "r");
  26. if (file) {
  27. while ((c = getc(file)) != EOF)
  28. tekstCounter++;
  29. fclose(file);
  30. }
  31.  
  32. char P[wzorzecCounter]; //wzorzec
  33. char T[tekstCounter]; //tekst
  34.  
  35. //WZORZEC
  36. file = fopen("wzorzec.txt", "r");
  37. i = 0;
  38. if (file) {
  39. while ((c = getc(file)) != EOF)
  40. {
  41. P[i] = c;
  42. i++;
  43. }
  44. fclose(file);
  45. }
  46.  
  47. //TEKST
  48. file = fopen("tekst.txt", "r");
  49. i = 0;
  50. if (file) {
  51. while ((c = getc(file)) != EOF)
  52. {
  53. T[i] = c;
  54. i++;
  55. }
  56. fclose(file);
  57. }
  58. printf("%d, %d\n", tekstCounter, wzorzecCounter);
  59.  
  60.  
  61. // NAIWNY
  62. printf("NAIWNY: \n");
  63. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
  64. for(s = 0; s <= tekstCounter-wzorzecCounter; s++) {
  65. i = 0;
  66. while(i<wzorzecCounter) {
  67. if(P[i]==T[s+i])
  68. i++;
  69. else break;
  70. if(i==wzorzecCounter)
  71. printf("Wzorzec wystepuje od pozycji %d\n", s+1);
  72. }
  73. }
  74. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
  75. printf("CZAS WYKONYWANIA: %lf\n\n",tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement