/* Due date for this lab: Saturday 11/5 */ /* Please email me your code using the following command */ /* mail -s "CISC2100 lab2" xzhang@fordham.edu < hash.cpp */ /* Extra credit part * Create a real hash table by making each table entry a * list of words, you can use STL list container: * * list table[TABLE_SIZE]; * * and store the words in the list. * See this tutorial: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#LIST */ #include #include using namespace std; /* In this program, we explore the idea of hashing function * that takes a string input, and returns an integer value * between 0...hashTableSize-1 * Application: * 1. hash table (a data structure) that has O(1) search performance * 2. to store a set of words (or other objects). */ //The size of the hashtable... const int TABLE_SIZE=200; /* Our hash function: * precondition: hashTableSize>=2 * postcondition: return value is between 0, hashTableSize-1 */ int MyHashFunction(string input, int hashTableSize) { //TODO by you, does this functio implements a function (in mathematical sense)? // What's the domain? What's the codomain? What's the range? // // right now return 0 return 0; } /* read a set of words from file and initilze the hashtable */ void InitTable (string fileName, int table [], int tableSize) { ifstream file; int hashcode; string word; //Init table entries to be 0 for (int i=0;i> word; if (!file.eof()) { // 2. Calcuate hashcode of the word using MyHashFunction hashcode = MyHashFunction (word, tableSize); table[hashcode]++; //one more word hashed to this code... } else endOfFile = true; } while (!endOfFile); //close the file file.close(); } int main() { int hashTable[TABLE_SIZE]; //hashTable[i] is the number of words that // are hashd to i string word; char cont; InitTable ("wordsInAnimalFarm.txt", hashTable, TABLE_SIZE); //Todo: write some code to answer the follow questions. //Is MyHashFunction a one-to-one function from the words in "wordsInAnimalFarm.txt" // to 0...TABLE_SIZE-1 // Is it onto? cout <<"Check whether a word appears in the book Animal Farm\n"; do { cout <<"Enter a word:"; cin >> word; int hashCode = MyHashFunction (word, TABLE_SIZE); if (hashTable[hashCode]==0) cout <<"not in the book!\n"; //no word in dictionary is hased to to this hashcode else cout <<"may in the book, but false positive is possible..."; cout <<"Try again (y/n)?"; cin >> cont; } while (cont=='y' || cont=='Y'); }