
November 12th, 2005, 01:55 PM
|
|
Registered User
|
|
Join Date: Nov 2005
Posts: 1
Time spent in forums: 7 m 55 sec
Reputation Power: 0
|
|
|
Help With staticHTable Producing Wrong Output!
I have done these sets of code but for some reason its not give then correct ouput. I enter a word and its definition and then it gives me the hash index as well as displaying the word and its definition. this keeps looping so i can enter more words in which is correct. when i type exit and then the "enter search word" appears in type in that word i just entered and it says word not found which is incorrect. its supposed to find it and display the definition. I am testing this using HTableTest.cc. I run the programe by doing g++ staticHTable.cc HTableTest.cc and then ./a.out. Could I have some help in this:
Code:
\\staticHTable.cc
#include <iostream>
#include "staticHTable.h"
#include <string>
using namespace std;
staticHTable::staticHTable ()
{
int i;
for (i=0; i < TABLE_SIZE; i++)
{
sHTable[i].word = "";
sHTable[i].definition = "";
}
}
staticHTable::~staticHTable ()
{
}
int staticHTable::hash(const string& w)
{
int i, sum;
for (sum=0, i=0; ((w[i] != '\0') && (i < w.length())); i++)
sum += (int)w[i];
return sum % TABLE_SIZE;
}
int staticHTable::rehash(const string& w)
{
int sum,i;
for (sum=0, i=0; ((w[i] != '\0') && (i <w.length())); i++)
sum += (int)w[i];
return sum % 19;
}
void staticHTable::put(const string& w, const string& d)
{
int i, home, inc;
i = hash(w);
cout<< "Hash table index for word is "
<< i << " and word is :" << w << endl;
cout<< "Definition is :" << d << endl;
if ((sHTable[i].word != "") && (sHTable[i].word != w))
{
home = i;
do
{
inc = rehash(w);
i = i + inc;
if (i >= TABLE_SIZE)
{
i = i - TABLE_SIZE;
}
} while ((sHTable[i].word != "") && (sHTable[i].word != w)
&& (home != i));
if (sHTable[i].word == "")
{
sHTable[i].word = w;
sHTable[i].definition = d;
cout<< "inserted new key into hash table location "<<i
<< endl;
}
else if (sHTable[i].word == w)
{
sHTable[i].definition += "\n" + d;
}
else
cout<< "can't insert key into hash table - table is full\n";
}
else
sHTable[i].definition += "\n" + d;
}
const string staticHTable::get(const string& w)
{
int i, home, k;
i = hash(w);
if ((sHTable[i].word != "") && (sHTable[i].word != w))
{
k = rehash(w);
home = i;
do
{
i = i + k;
if (i >= TABLE_SIZE)
{
i = i - TABLE_SIZE;
}
} while ((sHTable[i].word != "") && (sHTable[i].word != w)
&& (home != i));
if (sHTable[i].word == w)
return sHTable[i].definition;
}
else if (sHTable[i].word == w)
{ return sHTable[i].definition;
}
return "";
}
Code:
\\ HTableTest.cc
#include <iostream>
#include "staticHTable.h"
using namespace std;
int main()
{
string word, def;
int i;
staticHTable sTable;
cout << "Enter word and definition (end by typing in exit): ";
cin >> word,def;
getline(cin, def);
cout << endl;
while (word != "exit")
{
sTable.put(word, def);
cout << "Enter word and definition (end by typing in exit): ";
cin >> word,def;
getline(cin, def);
cout << endl;
}
cout << "Enter search word: ";
cin >> word;
if ((word = sTable.get(word)) !="")
{
cout << "Found word and definition is: " << def << endl;
}
else
{
cout << endl << "word not in table\n";
}
return 0;
}
Code:
//staticHTable.h
#ifndef STATIC_TABLE
#define STATIC_TABLE
#include <string>
const int TABLE_SIZE = 23;
class staticHTable
{
public:
staticHTable(); // constructor ? create empty table
~staticHTable(); // destructor
int hash(const string& w);
int rehash(const string& w);
/* enter data into table.
if the word is already present in the list, update the existing
definition by concatenating the current one */
void put (const string& w,const string& d);
/* return definition associated with key 'w'
return null string if 'w' is not present in the table */
const string get(const string& w);
private:
struct
{
string word;
string definition;
} sHTable[TABLE_SIZE];
};
#endif
|