Ciao, il codice che sto per postare è un dizionario costruito con un hash table che in caso di fallimento della parola trovata, restituisce le parole più prossime (stile Google). Il codice fa quello che deve fare, ma dopo un certo numero di inserimenti di nomi, quandio vado a ricercare mi da il messaggio unhandled exception access violation 0xC0000005. Come compilatore uso il Visual C++ 6.0. Sinceramente il problema non so dove sia e spero che qualcuno mi possa rispondere. Grazie.
codice:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class HashTable
{
private:
vector<string> htArray;
int htSize;
public:
HashTable(int);
void MostraTabella();
int funzHash(const char *);
void inserisci(string);
bool trova(string);
void LCS(string, string, int);
void trovavocabolo(string);
};
int a_lcs[1000];
vector <string> a;
int i;
HashTable::HashTable(int size)
{
htSize=size;
htArray.resize(htSize);
for(int j = 0; j < htSize; j++)
htArray[j] = "";
}
void HashTable::MostraTabella()
{
cout << "Tabella: " << endl;
for(int j = 0; j < htSize; j++)
{
if(htArray[j] != "")
cout << htArray[j]<< endl;
}
cout<<endl;
}
int HashTable::funzHash(const char *v)
{
int h = 0;
int a = 127;
for(; *v != 0; v++)
h = (a*h + *v) % htSize;
return h;
}
void HashTable::inserisci(string elemento)
{
string key = elemento;
int hashVal = funzHash(key.c_str());
while(htArray[hashVal] != "")
{
hashVal++;
hashVal %= htSize;
}
htArray[hashVal] = elemento;
}
bool HashTable::trova(string key)
{
int hashVal = funzHash(key.c_str());
while(htArray[hashVal] != "")
{
if(htArray[hashVal] == key)
return true;
hashVal++;
hashVal %= htSize;
}
return false;
}
void HashTable::LCS(string s, string a, int k)
{
int* l;
l=new int[s.size()*a.size()];
for(int r=0;r<=s.size();r++)
for(int c=0;c<=a.size();c++)
l[r*a.size()+c]=0;
for( r=1;r<=s.size();r++)
for( int c=1;c<=a.size();c++) {
if(s[r-1]==a[c-1])
l[r*a.size()+c]=l[(r-1)*a.size()+(c-1)]+1;
else if(l[r*a.size()+(c-1)] > l[(r-1)*a.size()+c])
l[r*a.size()+c]=l[r*a.size()+(c-1)];
else
l[r*a.size()+c]=l[(r-1)*a.size()+c];
}
r=s.size(); int c=a.size();
a_lcs[k]=l[r*a.size()+c];
}
void HashTable::trovavocabolo(string s)
{
string p;
for(i=0; i<htSize; i++)
{
a=htArray;
}
for(int k=0; k<i; k++)
{
p=a[k];
LCS(s,p,k);
}
int temp,b;
string s_temp;
for( k=1; k <i; k++)
{
temp=a_lcs[k];
s_temp=a[k];
b = k-1;
while (b >= 0 && a_lcs[b] > temp)
{
a_lcs[b+1]= a_lcs[b];
a[b+1]=a[b];
b--;
}
a_lcs[b+1]=temp;
a[b+1]=s_temp;
}
}
int main()
{
string x;
string chiave,lcs;
int size;
char scelta = '0';
cout << "Inserisci la dimensione della hash table: ";
cin >> size;
cout<<endl;
HashTable myHashTable(size);
cout<<"Premi 1 per mostrare il dizionario"<<endl;
cout<<"Premi 2 per inserire un elemento"<<endl;
cout<<"Premi 3 per ricercare un elemento"<<endl;
cout<<"Premi 4 per uscire"<<endl;
while(scelta!='4')
{
cout <<"Inserisci la tua scelta: ";
char scelta;
cin >> scelta;
cout<<endl;
switch(scelta)
{
case '1':
myHashTable.MostraTabella();
break;
case '2':
cout << "Inserisci il valore chiave da aggiungere: ";
cin >> chiave;
cout<<endl;
myHashTable.inserisci(chiave);
break;
case '3':
cout << "Inserisci il valore chiave da trovare: ";
cin >> chiave;
if(myHashTable.trova(chiave) == false )
{
myHashTable.trovavocabolo(chiave);
cout << "Non trovato, cercavi forse " << a[i-1]<<", "<<a[i-2]<<" oppure "<<a[i-3] <<endl;
}
else
cout<<"Trovato"<<endl;
break;
case '4':
return 0;
default:
cout << "Scelta non valida"<<endl;
}
}
system("pause");
return 0;
}