Aleks11

Isin, iswm

Jul 27th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int isin(char *text, char *subtext, int nPos = 0)
  9. {
  10.     int nTextLen = strlen(text),
  11.         nSubLen = strlen(subtext),
  12.         nStateFail = 0,
  13.         i = nPos - 1,
  14.         j = 0;
  15.  
  16.     while (++i < nTextLen)
  17.     {
  18.         if (i + nSubLen > nTextLen)
  19.             return -1;
  20.         if (text[i] == subtext[0])
  21.         {
  22.             j = 0;
  23.             while (++j < nSubLen)
  24.                 if (text[i + j] != subtext[j])
  25.                     break;
  26.             if (j == nSubLen)
  27.                 return i - 1;
  28.         }
  29.     }
  30.     return -1;
  31. }
  32.  
  33. int iswm(char *str, char *substr)
  34. {
  35.     int nLenStr = strlen(str),
  36.         nLenSubstr = strlen(substr),
  37.         nPosStr = 0,
  38.         nPosSubstr = 0;
  39.  
  40.     while (1)
  41.     {
  42.         // всякие условия выхода по завершению какой-то строки
  43.         if (nPosStr >= nLenStr) {
  44.             if (nPosSubstr < nLenSubstr)
  45.                 return 0;
  46.             else
  47.                 return 1;
  48.         }
  49.         if (nPosSubstr >= nLenSubstr)
  50.         {
  51.             if (substr[nLenSubstr - 1] == '*' || nPosStr >= nLenStr)
  52.                 return 1;
  53.             else
  54.                 return 0;
  55.         }
  56.        
  57.         // если ждём одиночный символ - пропускаем значит
  58.         if (substr[nPosSubstr] == '?')
  59.         {
  60.             nPosSubstr++;
  61.             nPosStr++;
  62.             continue;
  63.         }
  64.  
  65.         // переходим в поиск следующих символов
  66.         if (substr[nPosSubstr] == '*')
  67.         {
  68.             nPosSubstr++;
  69.             int nMustBeSymbols = 0;
  70.             while (nPosSubstr < nLenSubstr && (substr[nPosSubstr] == '*' || substr[nPosSubstr] == '?'))
  71.             {
  72.                 if (substr[nPosSubstr] == '?')
  73.                     nMustBeSymbols++;
  74.                 nPosSubstr++;
  75.             }
  76.  
  77.             if (nPosSubstr == nLenSubstr)
  78.                 return nLenStr - nPosStr >= nMustBeSymbols;
  79.  
  80.             int nEndPos = nPosSubstr;   // это - окончание символов любых
  81.             while (nEndPos < nLenSubstr && substr[nEndPos] != '?' && substr[nEndPos] != '*')
  82.                 nEndPos++;
  83.  
  84.             int nTmpLen = nEndPos - nPosSubstr;
  85.             char *tmpStr = new char[nTmpLen];
  86.             memcpy(tmpStr, substr + nPosSubstr, nTmpLen);
  87.             tmpStr[nTmpLen] = '\0';
  88.             // ищем вхождение наших символов из substr в str
  89.             int nFindPos = isin(str, tmpStr, nPosStr);
  90.  
  91.             // если не нашлось или мало символов, которые должны быть - выходим
  92.             if (nFindPos == -1 || nFindPos - nPosStr < nMustBeSymbols)
  93.                 return 0;
  94.  
  95.             // если всё хорошо - перемещаемся на новую позицию и идём дальше
  96.             nPosStr = nFindPos + nTmpLen;
  97.             nPosSubstr = nEndPos;
  98.  
  99.             printf("%d\n", nFindPos);
  100.             continue;
  101.         }
  102.  
  103.         if (str[nPosStr] != substr[nPosSubstr])
  104.             return 0;
  105.         nPosStr++;
  106.         nPosSubstr++;
  107.     }
  108.     return 1;
  109. }
  110.  
  111. int main()
  112. {
  113.     printf("%d\n", iswm("sta44444dfs3333fa1111fd2222FDD", "sta44444dfs3333fa*D?"));
  114.     _getch();
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment