Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- #define MLD 1000000000.0
- int main()
- {
- int wzorzecCounter = 0;
- int tekstCounter = 0;
- struct timespec tp0, tp1;
- //FILES
- int c, s, i;
- FILE *file;
- //WZORZEC
- file = fopen("wzorzec.txt", "r");
- if (file) {
- while ((c = getc(file)) != EOF)
- wzorzecCounter++;
- fclose(file);
- }
- //TEKST
- file = fopen("tekst.txt", "r");
- if (file) {
- while ((c = getc(file)) != EOF)
- tekstCounter++;
- fclose(file);
- }
- char P[wzorzecCounter]; //wzorzec
- char T[tekstCounter]; //tekst
- //WZORZEC
- file = fopen("wzorzec.txt", "r");
- i = 0;
- if (file) {
- while ((c = getc(file)) != EOF)
- {
- P[i] = c;
- i++;
- }
- fclose(file);
- }
- //TEKST
- file = fopen("tekst.txt", "r");
- i = 0;
- if (file) {
- while ((c = getc(file)) != EOF)
- {
- T[i] = c;
- i++;
- }
- fclose(file);
- }
- printf("%d, %d\n", tekstCounter, wzorzecCounter);
- // NAIWNY
- printf("NAIWNY: \n");
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
- for(s = 0; s <= tekstCounter-wzorzecCounter; s++) {
- i = 0;
- while(i<wzorzecCounter) {
- if(P[i]==T[s+i])
- i++;
- else break;
- if(i==wzorzecCounter)
- printf("Wzorzec wystepuje od pozycji %d\n", s+1);
- }
- }
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
- printf("CZAS WYKONYWANIA: %lf\n\n",tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement