Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #ifndef min
  7.     #define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
  8. #endif
  9.  
  10. int delta_table[1000][1000];
  11. char* slownik = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  12. char *cpy1;
  13. char *cpy2;
  14.  
  15. int delta(int q, char a){
  16.        int ai = a;
  17.        return delta_table[q][ai-32];
  18. }
  19.  
  20. int sufix(char* string, char* test){
  21.     int tr = 0;
  22.     int z = 0;
  23.     if(strlen(string)>=strlen(test)){
  24.           for(z=1;z<=strlen(test);z++){
  25.                 if(test[strlen(test)-z] == string[strlen(string)-z])
  26.                           tr = 1;
  27.                 else{
  28.                      tr = 0;
  29.                      break;
  30.                 }
  31.           }
  32.     }
  33.     else
  34.         return tr =0;
  35.     return tr;  
  36. }
  37.  
  38. void ComputeTransitionFunction(char wz[],char slownik[]){
  39.      printf("Wszedlem do CTF!\n");
  40.     int m = strlen(wz);
  41.     int q = 0;
  42.     int j = 0;
  43.    
  44.     cpy1 = (char*)malloc(sizeof(char)*1000);
  45.     cpy2 = (char*)malloc(sizeof(char)*1000);
  46.     char tmp[] = "";
  47.  
  48.     for(q=0;q<=m;q++){
  49.         for(j=0;j<strlen(slownik);j++){
  50.                 int k = min(m,q+1);
  51.                 printf("m = %d, k = %d, q = %d\n",m,k,q);
  52.                 printf("wz: %s\n",wz);
  53.                 if(q==0){
  54.                          //strncpy(cpy1,wz,k);
  55.                          //strncpy(cpy1,&slownik[j],1);
  56.                          while(sufix(strncpy(cpy2,&slownik[j],1),strncpy(cpy1,wz,k))!=1){
  57.                                printf("Wszedlem\n");
  58.                                printf("WHILE cpy1: %s\n",cpy1);
  59.                                printf("WHILE cpy2: %s\n",cpy2);
  60.                                memset(cpy1, '\0', strlen(cpy1)-1);
  61.                                memset(cpy2, '\0', strlen(cpy2)-1);
  62.                                k=k-1;
  63.                                if(k==0)
  64.                                   break;
  65.                                printf("k = %d\n",k);
  66.                                }                      
  67.                 }
  68.                 else{                  
  69.                          //strncpy(cpy1,wz,k);
  70.                          //strncat(strncpy(cpy2,wz,q),&slownik[j],1);
  71.                          while(sufix(strncat(strncpy(cpy2,wz,q),&slownik[j],1),strncpy(cpy1,wz,k))!=1){
  72.                                printf("Wszedlem\n");
  73.                                printf("WHILE cpy1: %s\n",cpy1);
  74.                                printf("WHILE cpy2: %s\n",cpy2);
  75.                                memset(cpy1, '\0', strlen(cpy1)-1);
  76.                                memset(cpy2, '\0', strlen(cpy2)-1);
  77.                                k=k-1;
  78.                                if(k==0)
  79.                                   break;
  80.                                printf("k = %d\n",k);
  81.                                }
  82.                 }
  83.                 printf("cpy1: %s\n",cpy1);
  84.                 printf("cpy2: %s\n",cpy2);
  85.                 //printf("slownik[j] = %s\n",&slownik[j]);
  86.                 //printf("strncat = %s\n",strncat(strncpy(cpy2,wz,q),&slownik[j],1));
  87.                 //while(sufix(strncat(strncpy(cpy2,wz,q),&slownik[j],1),strncpy(cpy1,wz,k))!=1){
  88.                
  89.                 printf("Przeszedlem\n");
  90.                 memset(cpy1, '\0', strlen(cpy1)-1);
  91.                 memset(cpy2, '\0', strlen(cpy2)-1);
  92.                 printf("Tutaj tez przeszedlem\n");
  93.                 int ai = slownik[j];
  94.                 delta_table[q][ai-32] = k;                
  95.         }
  96.     }
  97. }
  98.  
  99. void FiniteAutomatonMatcher(char in[], char wz[], int m){
  100.      int n = strlen(in);
  101.      int przesuniecie;
  102.      int q = 0;
  103.      int i = 0;
  104.      printf("Wszedlem do FAM!\n");
  105.      ComputeTransitionFunction(wz,slownik);
  106.      printf("Wrocilem do FAM\n");
  107.      for(i=1;i<=n;i++){
  108.          q = delta(q,in[i]);
  109.          if(q==m){
  110.             przesuniecie = i-m;
  111.             printf("Przesuniecie wzorca: %d\n",przesuniecie);
  112.          }                  
  113.      }
  114. }
  115.  
  116. int main(int argc, char *argv[])
  117. {
  118.     char input[1000];
  119.     char wzorzec[1000];
  120.     char wynik[1000];
  121.    
  122.     printf("Podaj ciag wejsciowy znakow: ");
  123.     scanf("%s",&input);
  124.     printf("Input = %s\n",input);
  125.     printf("\n");
  126.     printf("Podaj wzorzec wyszukowania: ");
  127.     scanf("%s",&wzorzec);
  128.     printf("Wzorzec = %s\n",wzorzec);
  129.     printf("\n");
  130.    
  131.     int m = strlen(wzorzec);
  132.     FiniteAutomatonMatcher(input,wzorzec,m);
  133.  
  134.     system("PAUSE");   
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement