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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
//prototypes
int wordTotal(string word);
void insertWord(string word, string table[]);
void isFull(string table[]);
int searchWord(string word, string table[]);
void menu(string table[], string word);
void displayArray(string table[]);
void remove(string table[], string word);
bool collisionCheck(string table[], int value);
void h(string table[], string word[]);
int main(){
string hashTable[19];
int asciiTotal = 0;
string word; // string for the "key"; user input
string table[19]; // array for incoming words from txt document
string hold[19];
ifstream inFile; // instream name
string fileName; // string to enter filename from user
int n = 0; // iterative integer
int index = 0;
int value = 0;
for (int i = 0; i < 19; i++){
table[i] = "";
}
do {// Loop to enter file name
inFile.clear();
cout << "Please enter file name." << endl;
cin >> fileName;
cout << endl << endl;
inFile.open("data.txt");
}// End of Do-While Loop
while (inFile.fail()); // If input of text file failes, repeat enter file
while (!inFile.eof()) // reads data until end of file
{
getline(inFile, table[n]); // reads in word data
n++; // incrementing through table
}
h(hashTable, table);
menu(hashTable, word); // User Menu
cout << endl << endl; // creating space between the users choice input and the menu options
inFile.close(); // closing inFile
}
// counts the total ascii values for each key
void menu(string table[], string word){
int choice = 0;
int index = 0;
while (choice != 5){
cout << "Please choose from the following options: " << endl;
cout << "1: To add a word " << endl;
cout << "2: To delete a word " << endl;
cout << "3: To search for a word " << endl;
cout << "4: To display contents of table " << endl;
cout << "5: To exit" << endl;
cin >> choice;
if (choice == 1){
cin.ignore(999, '\n');
insertWord(word, table);
}
else if (choice == 2){
cin.ignore(999, '\n');
remove(table, word);
}
else if (choice == 3){
cin.ignore(999, '\n');
index = searchWord(word, table);
if (index == -1)
{
cout << "Failure, word not found in list! " << endl;
displayArray(table);
}
else
cout << "The index of the word is: " << index << endl << endl;
}
else if (choice == 4){
displayArray(table);
}
}
cout << endl << endl;
}
int searchWord(string word, string table[]){
cout << "Enter word to be searched: " << endl;
getline(cin, word);
system("cls"); // clearing screen
for (int index = 0; index < 19; index++){ // searches through table size
if (word == table[index]) // checking to see if word entered is equal to anything inside the table
return index;
}
return -1;
} //done
void displayArray(string table[]){
for (int i = 0; i < 19; i++){
cout << "Location " << i << ": " << table[i] << endl;
}
cout << endl;
}
int wordTotal(string word){
int asciiTotal = 0;
for (int i = 0; i < word.length(); i++){ //loops through length of key
asciiTotal = asciiTotal + (int)word[i]; //adds ascii values through casting, asccii totals become the key values
}
cout << asciiTotal % 19 << endl;
return asciiTotal;
}
void insertWord(string word, string table[]){
int asciiTotal = 0;
cout << " Please enter word to be entered. " << endl;
getline(cin, word);
wordTotal(word); // calling word to turn input into ascii
isFull(table); //checking to make sure it is not full
for (int i = 0; i < 19; i++){
if (table[i] == "" || table[i] == "0"){ // loop to insert word
table[i] = word;
i = 19; // stopping iteration
}
}
}
void isFull(string table[]){
bool empty = true;
for (int i = 0; i < 19; i++){
empty = empty && table[i] != "";
}
if (empty)
cout << "Array not full. " << endl;
else cout << "Array is full. " << endl;
}
void remove(string table[], string word){
cout << " Enter your word to be removed: " << endl;
getline(cin, word);
for (int i = 0; i < 19; i++){
if (word == table[i])
table[i] = "";
}
}
bool collisionCheck(string table[], int value){
if (table[value] == "0")
return true;
else return false;
}
void h(string table[], string word[]){
bool used[19] = { false };
for (int i = 0; i<19; i++)
{
bool found = false;
int hash = wordTotal(word[i]) % 19;
int tempValue = 1;
do{
if (used[hash] == false)
{
found = true;
table[hash] = word[i];
used[hash] = true;
}
else
{
hash = (hash += tempValue) % 19;
tempValue += 2;
}
} while (found == false);
}
}
|