分段错误拼字游戏

时间:2016-06-24 16:00:37

标签: c++

我正在研究一个scrabblegame,我读了一个txtfile并为里面的所有单词创建一个哈希表。 Word是一个包含向量的特定类。 我想自己创建一个hashmap并定义我的" Dictionary"是50000.我使用nullpointer来保留我的数组的所有索引。如果我想打印到我的哈希表,编译器告诉我一个分段错误。有没有人似乎错误?

the headerfile:
    class Dictionary {
    public:
        Dictionary();
        Dictionary(string filepath);
        friend std::ostream& operator<<(std::ostream& os, const Dictionary& obj);
        bool find(const Word& word);
        vector<Word> allPossibleWords(const vector<Character>& tiles);

        //    struct compare {
        //
        //        bool operator()(const Word& a, const Word& b) {
        //            return a.operator<(b);
        //        }
        //    } myCompare;

        vector<Word> m_allWords;
        vector<Word>::iterator itVecWords;
        static const int table_size = 500000;
        //    std::array <Word*, table_size> arrWords = {nullptr};
        Word* arrWords[table_size] = {nullptr};
        int hash(Word new_word);
        void addItem(Word word);
        void printHashTable();



    the cpp:
    Dictionary::Dictionary(string filepath) {

    ifstream datei(filepath.c_str());
    while (datei.good() && !datei.eof()) {
        string temp;
        string temp1;
        string::size_type pos;
        getline(datei, temp);
        pos = temp.find(" ");
        temp1 = temp.substr(0, pos);
        Word new_word(temp1);
        addItem(new_word);
    }
    datei.close();



}

std::ostream& operator<<(std::ostream& os, const Dictionary& obj) {
    for (int i = 0; i < obj.m_allWords.size(); i++) {
        os << obj.m_allWords[i] << endl;
    }
    return os;
}

bool Dictionary::find(const Word& word) const {
    if (std::binary_search(m_allWords.begin(), m_allWords.end(), word)) {
        return true;
    }
    return false;
}

vector<Word> Dictionary::allPossibleWords(const vector<Character>& tiles) const {

    vector<Word> ergebnis;


    string tmp;
    int cnt = 0;
    for (int i = 0; i < tiles.size(); i++) {
        tmp += tiles[i].GetC();
    }

    sort(tmp.begin(), tmp.end());


    for (int i = 1; i <= tiles.size(); i++) {
        do {
            string piece = tmp.substr(0, i);
            do {
                Word search = Word(piece);
                //Überschreibt immer der in Ergebnis existierte Wert
                if (find(search) && std::find(ergebnis.begin(), ergebnis.end(), search) == ergebnis.end()) {
                    ergebnis.push_back(search);
                }

            } while (next_permutation(piece.begin(), piece.end()));
        } while (next_permutation(tmp.begin(), tmp.end()));
    }

    return ergebnis;

}

int Dictionary::hash(Word new_word) {
    int index = 0;
    for (auto u : new_word.new_Character) {
        index += (int) u.GetC();
    }
    index = index * (int) new_word.new_Character.at(0).GetC();
    index = index * (int) new_word.new_Character.at(new_word.new_Character.size() - 1).GetC();
    return index % table_size;

}

void Dictionary::addItem(Word word) {

    int index = hash(word);
    if (arrWords[index] == nullptr) {
        arrWords[index] = new Word(word);
    } else {
        Word* ptr = arrWords[index];
        Word* neu = new Word(word);
        while (ptr->getNextWord() != nullptr) {
            ptr = ptr->getNextWord();
        }
        ptr->setNextWord(neu);
    }

}

void Dictionary::printHashTable() {

    Word* tmp;
    for (int i = 0; i < table_size; i++) {
        tmp = arrWords[i];
        if (tmp != nullptr) {
            tmp->printWord();
            cout << "Index : " << i;
        }

        tmp = tmp->getNextWord();
    }

}

class Word {
public:
    Word();
    Word(string m_wort);

    int length() const; 
    int points() const; 
    friend std::ostream& operator<<(std::ostream& os, const Word& obj);
    bool operator==(const Word& right) const; /
    bool operator!=(const Word& right) const; 
    bool contains(const Character& c) const; 
    Word substr(int start, int end) const; 
    bool operator<(const Word& right) const;

    vector <Character> new_Character;

    Word* Next = nullptr;
    void setNextWord(Word*);
    Word* getNextWord();
    void printWord();
    string getWordAsString();


CPP File:
    void Word::setNextWord(Word* w) {

        Next = w;
    }

    Word* Word::getNextWord() {

        return Next;

    }

    void Word::printWord() {
        string s = "";
        for (int i = 0; i < new_Character.size(); i++) {
            s += new_Character.at(i).GetC();
        }
        cout << s << endl;

    }

    string Word::getWordAsString() {
        string s;
        for (int i = 0; i < new_Character.size(); i++) {
            s += new_Character.at(i).GetC();
        }
        return s;
    }

0 个答案:

没有答案