Bittle icon

ChainingTable (Hash Table)

Bittle | PRO | 02/15/17 01:28:13 PM UTC | 0 ⭐ | 136 👁️ | Never ⏰ | []
C++ |

3.49 KB

|

None

|

0 👍

/

0 👎

#include <iostream>
#include <fstream>
#include "chainingTable.h"
#include "stringUtil.h"
// main.cpp
void writeToFile(string fileName, string str) {
    ofstream file(fileName);
    if (file.is_open()) {
        file << str;
        file.close();
    }
}
 
void correctRaven(chainingTable table) {
    stringUtil stringUtil;
    ifstream file("raven.txt");
    string str;
    string total = "";
 
    while (file >> str) {
        // read file word by word
        string word = stringUtil.scrubText(str);
 
        if (!table.contains(word)) {
            //cout << "This isn\'t English "<< word<< endl;
            total += word + "\n";
        }
    }
    writeToFile("output.txt", total);
}
 
int main() {
 
    chainingTable table;
 
    ifstream file("dictionary.txt");
    string str;
 
    while (std::getline(file, str)) {
        table.insert(str);
    }
 
    cout << table.averageSearchCost() << endl;
 
    correctRaven(table);
    return 0;
}
 
// ========= chainingTable.h ==========
#include "linkedList.h"
 
class chainingTable {
private:
    //hash table consists of a table of lists
    linkedList *table;
 
    //size of table
    int capacity;
 
    //number of items in hash table
    int numItems;
 
    int getCharInt(char c) {
        if (c >= 'a' && c <= 'z') {
            return c;
        } else {
            return c % 'a';
        }
    }
 
    int H(string str) {
        int prime1 = 60373;
        int prime2 = 80387;
        int prime3 = 90821;
        int h = 9973;
 
        for (int x = 0; x < str.length(); x++) {
            h = (h*prime1) * (str[x]*prime2);
            h%=prime3;
        }
 
        if (h < 0) {
            h *= -1;
        }
        h = h % prime3;
        return h % capacity;
    }
 
public:
 
    chainingTable() {
        capacity = 19471;    // 9973, and 194771 are also prime
        numItems = 0;
        table = new linkedList[capacity];
    }
 
    //use this to measure how good your
    //hash function is.
    double averageSearchCost() {
        double sumSquares = 0;
        for (int i = 0; i < capacity; i++) {
            sumSquares += (table[i].size() * table[i].size());
        }
        return sumSquares / numItems;
    }
 
    void insert(string x) {
        int index = H(x);
        table[index].addBack(x);
 
        numItems += 1;
    }
 
    bool contains(string x) {
        int index = H(x);
        if (table[index].contains(x)) {
            //table[index].display();
            return true;
        }
    }
};
 
// ========= stringUtil.h ==========
//
// Created by Oscar Torres on 2/14/17.
//
 
class stringUtil {
private:
    string toLower(string str) {
        string temp = "";
        for (int x = 0; x < str.length(); x++) {
            if (str[x] >= 'A' && str[x] <= 'Z') {
                char c = static_cast<char>(str[x] + 32);
                temp += c;
            } else {
                temp += str[x];
            }
        }
        return temp;
    }
 
    bool isLetter(char c) {
        return c >= 'a' && c <= 'z';
    }
 
    bool isPunct(char c) {
        return !isLetter(c);
    }
 
    string removePunct(string str) {
        string total = "";
        for (int x = 0; x < str.length(); x++) {
            if (!isPunct(str[x])) {
                total += str[x];
            }
        }
        return total;
    }
 
public:
 
    string scrubText(string str) {
        return removePunct(toLower(str));
    }
};

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎