Hi,
i have built a hash table dictionary based on the linked list class, the List and Meaning classes are working correctly. So i haven't included those, apologies if there is a lot of code but it is difficult to explain what i am doing without showing all the necessary code.
This code looks up a word (input by the user at a command prompt)in the hash table dictionary and outputs a message saying "word found or word not found". But what i need it also to display is the actual word together with its meaning list if there is more than one meaning. I have tried several attempts (understatement!) to use a eg.cout << word << meaninglist << endl; but i don't actually know fully how to implement the code properly.
The main function also asks the user to type in the name of an input file first-which contains all the dictionary words and their subsequent meanings
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cctype>
#include <cstring>
#include "IndDicT.h"
#include "List.h"
#include "Meaning.h"
#include <cassert>
usingnamespace std;
IndDicT Dictionary;
int main()
{
ifstream inFile;
string inFileName;
cout << "Enter the name of the input file" << endl;
cin >> inFileName;
inFile.open(inFileName.c_str());
IndDicT dictionary;
string word;
string meaning;
assert(inFile);
while (!inFile.eof())
{
string word,meaning;
string hashKey;
inFile >> word;
getline(inFile, meaning);
dictionary.insert(word, meaning);
}
string wordSearch;
cout << "Type in a word to search: ";
cin >> wordSearch;
List* found;
//Testing lookUp function
found = dictionary.lookUp(wordSearch);
if (found)
{
cout << "Word found"<<word<< endl; //i want to include a display word and
//and meaning list to test the lookUp
// function more thoroughly?
}
else
{
cout << "Word not found" << endl;
}
return 0;
}
//This the IndDicT.cc
#include <list>
#include <string>
#include <iostream>
#include <cctype>
#include <iomanip>
#include "IndDicT.h"
#include "List.h"
#include "Meaning.h"
usingnamespace std;
IndDicT::IndDicT()
{
for( int i =0;i < MAX_TABLESZ;i++)
{
dictionary[i].word= " ";
dictionary[i].meaningList = new List();
}
}
IndDicT::~IndDicT()
{
}
int IndDicT::hashKey(string word)
{
int result= 0;
/*converts a string/char to an integer value
Uses the division method- divides a data item's key value
by the total size of the hash table
using the remainder of the division as the hash function return value
*/
for(int i= 0; i < word.length(); i++)
{
result = result + static_cast<int>(word[i]);
}
return( result % MAX_TABLESZ );
}
int IndDicT::rehashKey(string word)
{
int result= 0;
for(int i= 0; i < word.length(); i++)
{
result = result + static_cast<int>(word[i]);
}
return( result % REHASH_TABLESZ );
}
void IndDicT::insert(string word, string meaning)
{
int i = hashKey(word); // calculate the table index for word
int c = rehashKey(word); // calculates the table index for the rehash
int home = i;
cout <<""<< word <<":"<< endl;
cout <<""<< meaning << endl << endl;
if(dictionary[i].word !=" ") // word is not an empty string
{
home = i; // infinite loop check
do
{
i = i + c;
if (i >= MAX_TABLESZ)
{
i = i - MAX_TABLESZ;
}
} while ((dictionary[i].word !=" ") && (home != i));
}
if (dictionary[i].word ==" ")
{
dictionary[i].word= word;
dictionary[i].meaningList->insertAtRear(meaning);
}
}
List* IndDicT::lookUp(string word)
{
int i= hashKey(word);
int c= rehashKey(word);
int home = i;
if (dictionary[i].word !=word)
{
home =i;
c = rehashKey(word);
do
{
i = i + c;
if(i >= MAX_TABLESZ)
{
i = i - MAX_TABLESZ;
}
}while((dictionary[i].word != word) && (home !=i));
}
if (dictionary[i].word == word)
{
return dictionary[i].meaningList;
}
}
void IndDicT::traverse()
{
string word;
int i= 0;
list <string> List;
/*Loops through the hash table/dictionary and
Displays the word at location i of the hash table/dictionary
Displays the list of meanings from the linked list
*/
for (int i = 0; i < MAX_TABLESZ; i++)
{
cout << dictionary[i].word << endl;
dictionary[i].meaningList->printList();
}
}
i don't see your IndDicT class, so i don't really know how it works.
IndDicT::traverse() appears to print a list of meanings with dictionary[i].meaningList->printList(); so all you have to do is something like found->printList(); to print the definitions, right?
or do you mean you want to overload the << operator to print the list of definitions?
if you found the word, then your word should be in wordSearch? i don't know how to get at dictionary[i].word from the meaningList returned by InDicT::lookUp().