1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
const int Error = 1; // Global variable
//-----------------------------------------------------------------------------
int hashFunction(const string &key, int tableSize)
{
int hashVal = 0;
for(int i = 0; i < key.length(); i++)
hashVal = 37 * hashVal + key[i];
hashVal %= tableSize;
// Handle any negative values
// Won't happen with strings or ASCII values,
// could happen with other data types
if(hashVal < 0)
{
hashVal += tableSize;
}
return hashVal;
}
//-----------------------------------------------------------------------------
int main()
{
ifstream dictionary_File("dictionary.txt", ios::in); // Open dictionary
if(dictionary_File.is_open()) // Error check opening file
{
cout << "\nDictionary file open." << endl;
}
else
{
cout << "\nDictionary file did not open. Exiting." << endl;
return(Error);
}
//-----------------------------------------------------------------------------
ifstream check_File("check.me", ios::in); // Open check file
if(check_File.is_open()) // Error check opening file
{
cout << "\nCheck file open." << endl;
}
else
{
cout << "\nCheck file did not open. Exiting." << endl;
return(Error);
}
//-----------------------------------------------------------------------------
const int tableSize = 101111; // Prime number
string line;
string *hashTable = new string[tableSize]; // Allocate hashTable
//-----------------------------------------------------------------------------
for(int i=0; i<tableSize; i++) hashTable[i] = -1; // Set hashTable to NULL
//-----------------------------------------------------------------------------
while(getline(dictionary_File, line)) // Hash Dictionary
{
hashTable[hashFunction(line,tableSize)] = line; // Input into HashTable
}
cout << "\nDictionary in hashTable" << endl;
dictionary_File.close(); // Close filestream
//-----------------------------------------------------------------------------
//line.compare(hashTable[hashFunction(line,tableSize)]) != 0)
//line != hashTable[hashFunction(line, tableSize)]
string word;
//istringstream iss;
while(getline(check_File, line, ' ')) // Obtain words with space delimiter
{
//iss.clear();
//iss.str(line);
if(line != hashTable[hashFunction(line,tableSize)]) // Compare w dictionary
{
word = line; // Save word as original
for(int i=0; i < line.length(); i++) // Set all to lower case
line[i] = tolower(line[i]);
cout << "This is the new line after to lower: " << line << endl;
for(int i=0; i < line.length(); i++)
if(ispunct(line[i])) line[i] = ' ';
cout << "This is the new line after is punct: " << line << endl;
if(line != hashTable[hashFunction(line,tableSize)])
{
cout << "\nWord: '" << line << "' is misspelled." << endl;
}
else continue; // Spelled correctly
}
else continue; // Spelled correctly
}// end while
check_File.close(); // Close filestream
//-----------------------------------------------------------------------------
cin.get();
cin.ignore();
return (0); // Success
}// end main
//-----------------------------------------------------------------------------
|