piątek, 14 kwietnia 2017

wchar_t, wcslen, algorithm Robin-Karp C++



bool TForm1::is_True(int x1,int y1,int x2,int y2)
{
if((x1<y1) && (x2<y2))
return true;
else
return false;
}
 wchar_t TForm1::wchr_to_int(wchar_t symbol)
 {
if(islower(symbol))
 return symbol-'a'+1;
else
 return symbol-'A'+27;
 }
int TForm1::how_many_Phrase(wchar_t *text1, wchar_t *text2)
{
int i,j,size_text1,size_text2;
int value;
size_text1=wcslen(text1);
size_text2=wcslen(text2);
i=j=0;
while(is_True(j,size_text1,i,size_text2))
{
if(text2[i]!=text1[j])
{
i-=j-1;
j=-1;
}
j++;
i++;
}
if(j==size_text1)
 value=(i-size_text1);
else
 value=-1;
return value;
}
int TForm1::size_rk(wchar_t *text1, wchar_t *text2)
{
int size_text1,size_text2;
size_text1=wcslen(text1);
size_text2=wcslen(text2);
int i;
unsigned long value_function1,value_function2,value_function3;
value_function1=value_function2=0;
value_function3=1;
for(i=0;i<size_text1;i++)
{
value_function1=(value_function1*32+wchr_to_int(text1[i]))% 70051;
value_function2=(value_function2*32+wchr_to_int(text2[i]))% 70051;
}
for(i=1;i<size_text1;i++)
value_function3=(value_function3*32)%70051;
for(i=0;value_function1!=value_function2;i++)
{
value_function2=(value_function2+32*70051-wchr_to_int(text2[i])*value_function3)%70051;
value_function2=(value_function2*32+wchr_to_int(text2[i+size_text1]))%70051;
if(i>size_text2-size_text1)
return -1;
}
    return i;

}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 wchar_t **m_text1=new wchar_t*[14];
 wchar_t **m_text2=new wchar_t*[14];

 int value[14];
 int i;
 for(i=0;i<14;i++)
 {
m_text1[i]=new wchar_t[64];
m_text2[i]=new wchar_t[64];
 }
 UnicodeString w_text1[14]={"telphon","tablebooktablepen",
 "potatoes","Cualarumpur","Anthropology","Sociocultural","international",
 "Characterisation","possession","nothing","capocannoniere","cicomjekocmsckocomcomsocm",
 "010201012011010343210101010101010","gold"};
 UnicodeString w_text2[14]={"el","bo","to","al","po","oc","na","ar","ss","xxx",
 "oca","com","10","gold"};
 for(i=0;i<14;i++)
 {
ListBox1->Items->Add(w_text1[i]);
ListBox2->Items->Add(w_text2[i]);
m_text1[i]=w_text1[i].w_str();
m_text2[i]=w_text2[i].w_str();
value[i]=how_many_Phrase(m_text2[i],m_text1[i]);
ListBox3->Items->Add(IntToStr(value[i]));
 }


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 wchar_t **m_text1=new wchar_t*[14];
 wchar_t **m_text2=new wchar_t*[14];

 int value[14];
 int i;
 for(i=0;i<14;i++)
 {
m_text1[i]=new wchar_t[64];
m_text2[i]=new wchar_t[64];
 }
 UnicodeString w_text1[14]={"telphon","penbooktablebook",
 "potatoes","Cualarumpur","Anthropology","Sociocultural","international",
 "Characterisation","possession","nothing","capocannoniere","cicomjekocmsckocomcomsocm",
 "010201012011010343210101010101010","gold"};
 UnicodeString w_text2[14]={"el","bo","to","al","po","oc","na","ar","ss","xxx",
 "oca","com","10","old"};
 for(i=0;i<14;i++)
 {
ListBox1->Items->Add(w_text1[i]);
ListBox2->Items->Add(w_text2[i]);
m_text1[i]=w_text1[i].w_str();
m_text2[i]=w_text2[i].w_str();
value[i]=size_rk(m_text2[i],m_text1[i]);
ListBox3->Items->Add(IntToStr(value[i]));
 }
}

Brak komentarzy:

Prześlij komentarz