Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdbool.h>
  4. #include<string.h>
  5.  
  6. int prawda = 0;
  7. int ileGwiazdek(char *); int ilePytajnikow(char *);
  8.  
  9. bool match(char* wzorzec, char* lancuch) {
  10.     int wzorzec_length=strlen(wzorzec);
  11.     int lancuch_length=strlen(lancuch);
  12.     int j=0; //pozycja lancucha
  13.  
  14.     for(int i=0; i<wzorzec_length; i++) {
  15.         if((j>=lancuch_length && ileGwiazdek(wzorzec)==0) || ilePytajnikow(wzorzec)>strlen(lancuch))
  16.             return false;
  17.         else if(ileGwiazdek(wzorzec)>0 && j>=lancuch_length)
  18.             return true;
  19.  
  20.         if(wzorzec[i]=='*') {
  21.             char tempwzorzec[100] = "";
  22.             for(int a=0; a<wzorzec_length; a++) {
  23.                 tempwzorzec[a] = wzorzec[i+1];
  24.                 i++;
  25.             }
  26.  
  27.             for(int k=j; k<lancuch_length; k++) { //sprawdzenie poprawnosci dla kazdego mozliwego ciagu lancucha
  28.                 int n=lancuch_length-k;
  29.                 char templancuch[100]="";
  30.  
  31.                 for(int a=0; a<n; a++) {
  32.                     templancuch[a]=lancuch[k+a];
  33.                 }
  34.                 if((match(tempwzorzec, templancuch) && ((ileGwiazdek(tempwzorzec)==0 && strlen(templancuch)==strlen(tempwzorzec)) ||
  35.                     ileGwiazdek(tempwzorzec)>0)) || strlen(tempwzorzec)==0)
  36.                     return true;
  37.             }
  38.             return false;
  39.         }
  40.         if(wzorzec[i]=='?' || wzorzec[i]==lancuch[j]) {
  41.             j++;
  42.             continue;
  43.         } else
  44.             return false;
  45.     }
  46.  
  47.     if(wzorzec_length!=lancuch_length)
  48.         return false;
  49.     return true;
  50. }
  51.  
  52. int ileGwiazdek(char* slowo) {
  53.     int slowo_length=strlen(slowo);
  54.     int wynik=0;
  55.     for(int j=0;j<slowo_length;j++)
  56.         if(slowo[j]=='*')
  57.             wynik++;
  58.     return wynik;
  59. }
  60.  
  61. int ilePytajnikow(char* slowo) {
  62.     int slowo_length=strlen(slowo);
  63.     int wynik=0;
  64.     for(int j=0;j<slowo_length;j++)
  65.         if(slowo[j]=='?')
  66.             wynik++;
  67.     return wynik;
  68. }
  69.  
  70. int main() {
  71.     char wzor[100]; char lanc[100];
  72.  
  73.     char *wzorkey; char *lanckey;
  74.     int wzorlen, lanclen;
  75.  
  76.     printf("Podaj wrzozec i lancuch: ");
  77.     scanf("%s", wzor);
  78.     scanf("%s", lanc);
  79.  
  80.     wzorlen = strlen(wzor) + 1;
  81.     lanclen = strlen(lanc) + 1;
  82.  
  83.     wzorkey = (char*)malloc(wzorlen * sizeof(char));
  84.     lanckey = (char*)malloc(lanclen * sizeof(char));
  85.     strcpy(wzorkey, wzor);
  86.     strcpy(lanckey, lanc);
  87.  
  88.     if(match(wzorkey,lanckey))
  89.         printf("TRUE\n");
  90.     else
  91.         printf("FALSE\n");
  92.  
  93.     free(wzorkey);
  94.     free(lanckey);
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement