Wiem, że poniższy kod wygląda okropnie. Stało się tak, bo zmieniałem go z 1000 razy przez pojawianie się za losowym podejściem poniższego błędu. W każdym razie, Ideone przepuszcza mi kod bez zarzutów i wypisuje zawsze co należy, a poniżej zamieszczam kod i screeny, z tego co mi się dzieje przy dwóch podejściach z identycznymi parametrami.
Jestem już na tyle zmęczony, że nie jestem w stanie ogarnąć dlaczego tak się dzieje. Początkowo myślałem, że to za sprawą mojego małego przekonfigurowania Eclipse, ale teraz po buildzie w CodeBlocks jest dokładnie to samo. Help -_-
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;
int textArray[] = {2,3,8,2,5,3,6,8,3,2,9,1,1,6,3,2,7,1,1,1,3,7,2,8,0,8,8};
void BoyerMoore(int* patternArray);
int main()
{
int *patternArray = new int[2];
cout << "Input pattern you want find in text:\n";
cin >> patternArray[0] >> patternArray[1] >> patternArray[2];
cout << endl << endl;
BoyerMoore(patternArray);
cout << endl << endl;
delete [] patternArray;
system("pause");
return 0;
}
//-------------------------------------------------------------------
void BoyerMoore(int* patternArray)
{
int textLength = sizeof(textArray)/sizeof(int),
patternLength = 3,
patternIndex = -1;
for(int i = 0; i < textLength; i++)
cout << setw(2) << textArray[i];
cout << endl << endl;
if(textLength < patternLength)
cout << "Pattern is longer than text.";
else
{
for(int i = patternLength - 1; i < textLength; i++)
{
if(patternIndex == -1)
if(patternArray[patternLength-1] == textArray[i])
for(int j = i, k = 2; k >= 0; j--, k--)
{
patternIndex = j;
if(patternArray[k] != textArray[j])
{
patternIndex = -1;
break;
}
}
}
}
if(patternIndex >= 0)
cout << "Pattern begin's in text[" << patternIndex << "]";
else
cout << "Pattern not found.";
}
- 56278ab9a6.png (33 KB) - ściągnięć: 173
- 75e592dae3.png (60 KB) - ściągnięć: 180
- delete (33 KB) - ściągnięć: 347
- delete (60 KB) - ściągnięć: 361