C++ "Dictionary as Hash Table with Linked List" problems -
i started working hash tables , linked lists , given assignment involving them.
i create dictionary using text file "words.txt" hash table linked list spell check text file called "gettysburg_address.txt".
i given source code "hashtable class linked list" followed :
hashtable.cpp
#include <string> #include "listtools.h" #include "listtools.cpp" #include "hashtable.h" using linkedlistsavitch::node; using linkedlistsavitch::search; using linkedlistsavitch::headinsert; using namespace std; #define hash_weight 31 namespace hashtablesavitch { hashtable::hashtable() { (int = 0; < size; i++) { hasharray[i] = null; } } hashtable::~hashtable() { (int i=0; i<size; i++) { node<string> *next = hasharray[i]; while (next != null) { node<string> *discard = next; next = next->getlink( ); delete discard; } } } unsigned int hashtable::computehash(string s) const { unsigned int hash = 0; (unsigned int = 0; < s.length( ); i++) { hash = hash_weight * hash + s[i]; } return hash % size; } bool hashtable::containsstring(string target) const { int hash = this->computehash(target); node<string>* result = search(hasharray[hash], target); if (result == null) return false; else return true; } void hashtable::put(string s) { int hash = computehash(s); if (search(hasharray[hash], s) == null) { // add target if it's not in list headinsert(hasharray[hash], s); } } } // hashtablesavitch i suppose following :
1.display misspelled word (a word considered misspelled if not in dictionary)
2.count number of collisions each cell of hash table when loading dictionary "words.txt", , store/display data 20 per line
3.count number of words misspelled , correct - in each case, count number of operations performed , store/display these numbers - also, store/display average number of operations misspelled , correct word - note : "number of operations" refers number of nodes visited in linked list each word
this have far :
main.cpp
#include <iostream> #include <fstream> #include <cctype> #include <algorithm> #include <cstring> #include <string> #include "hashtable.h" using namespace std; using hashtablesavitch::hashtable; void uptolow(string & str); void removepunct(string & str); int main() { hashtable h; string currword; string word; int countmisspelled = 0; int countcorrect = 0; //get input words.rtf ifstream dictionary("words.txt"); //file checking if (dictionary.fail()) { cout << "file not exist" << endl; cout << "exit program" << endl; } //create dictionary hash table while(dictionary >> currword) { h.put(currword); } dictionary.close(); //get input gettysburg_address.txt ifstream input("gettysburg_address.txt"); //file checking if (input.fail()) { cout << "file not exist" << endl; cout << "exit program" << endl; } //spell check gettysburg_address.txt cout << "misspelled words : " << endl; cout << endl; //if word not in dictionary assume misspelled while(input >> word) { removepunct(word); uptolow(word); if(h.containsstring(word) == false) { countmisspelled++; // increment misspelled words count cout << word << " "; if(countmisspelled % 20 == 0) // display misspelled words 20 per line { cout << endl; } } else { countcorrect++; // increment correct words count } } input.close(); cout << endl; cout << endl; cout << "number of misspelled words : " << countmisspelled << endl; cout << "number of correct words : " << countcorrect << endl; return 0; } /*function convert uppercase letters lowercase*/ void uptolow(string & str) { (unsigned int = 0; < strlen(str.c_str()); i++) if (str[i] >= 0x41 && str[i] <= 0x5a) str[i] = str[i] + 0x20; } /*function remove punctuation string*/ void removepunct(string & str) { str.erase(remove_if(str.begin(), str.end(), static_cast<int(*)(int)>(&ispunct)),str.end()); } i've managed far spell check text file given , display words misspelled , counts "correct" , "misspelled". don't know should counting collisions , operations. instructor briefly went on hash tables , linked lists quite noob @ this.
for counting number of collisions, compare hash value each string in "words.txt" , if hash value same, increment counter number of collisions? not quite sure how go doing this.
i have no idea how go counting "number of operations performed". ideas this?
note again : "number of operations" refers number of nodes visited in linked list each word.
any tips/help on how should solve these problems appreciated.
Comments
Post a Comment