Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++&引用;计算哈希表中每个插槽的冲突数;_C++_List_Class_Hashtable - Fatal编程技术网

C++ C++&引用;计算哈希表中每个插槽的冲突数;

C++ C++&引用;计算哈希表中每个插槽的冲突数;,c++,list,class,hashtable,C++,List,Class,Hashtable,我假设创建一个字典作为一个带有链表的哈希表来检查文本文档的拼写。我在文件“words.txt”中读取创建字典。此外,当我加载字典“words.txt”时,我必须计算/显示哈希表中每个插槽的冲突数 我得到了带有链表的HashTable类的源代码,如下所示: hashtable.cpp(#包括“listtools.cpp”,因为它使用模板) #包括 #包括 #包括“listtools.h” #包括“listtools.cpp” #包括“hashtable.h” 使用LinkedListSavitch

我假设创建一个字典作为一个带有链表的哈希表来检查文本文档的拼写。我在文件“words.txt”中读取创建字典。此外,当我加载字典“words.txt”时,我必须计算/显示哈希表中每个插槽的冲突数

我得到了带有链表的HashTable类的源代码,如下所示:

hashtable.cpp(#包括“listtools.cpp”,因为它使用模板)

#包括
#包括
#包括“listtools.h”
#包括“listtools.cpp”
#包括“hashtable.h”
使用LinkedListSavitch::Node;
使用LinkedListSavitch::search;
使用LinkedListSavitch::headInsert;
使用名称空间std;
#定义HASH_权重31
名称空间HashTableSavitch
{
HashTable::HashTable()
{
对于(int i=0;icomputeHash(目标);
节点*结果=搜索(哈希数组[hash],目标);
如果(结果==NULL)
返回false;
其他的
返回true;
}
void哈希表::put(字符串s)
{
整数计数=0;
int hash=计算hash;
if(搜索(哈希数组[hash],s)==NULL)
{
//仅当目标不在列表中时才添加它
headInsert(哈希数组[hash],s);
}
其他的
{
冲突数组[hash]++;
}
void哈希表::printArray()
{
整数;
对于(int i=0;icout最简单的方法可能是在适当的位置声明数组

int collisionArray[SIZE]; 
HashTable::HashTable()中将其初始化为0


非常感谢。但是现在我在打印每个插槽的碰撞数时遇到了问题。我制作了一个空函数来打印每个插槽的碰撞数,但是所有碰撞结果都是0。有什么提示吗?抱歉,我认为复制不是碰撞。随着put的else块中的增量,只有当同一字符串出现两次。若要计算散列为相同值的离散字符串的数量,请将
collisionArray[hash]++;
行放在if块中。我编辑了代码以反映这一点。
collisionArray
应该在每个散列位置保留离散字符串的计数。谢谢。这解决了问题!
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <string>
#include "hashtable.h"
using namespace std;
using HashTableSavitch::HashTable;

void upToLow(string & str);
void removePunct(string & str);

int main()
{
    HashTable h;
    string currWord;
    string word;
    int countMisspelled = 0;
    int countCorrect = 0;

    //Get input from words.rtf
    ifstream dictionary("words.txt");

    //File checking
    if (dictionary.fail())
    {
        cout << "File does not exist" << endl;
        cout << "Exit program" << endl;
    }

    //Create the dictionary as a hash table
    while(dictionary >> currWord)
    {
        h.put(currWord);
    }
    dictionary.close();

    //display collisions
    h.printArray();

    //Get input from gettysburg_address.txt
    ifstream input("gettysburg_address.txt");

    //File checking
    if (input.fail())
    {
        cout << "File does not exist" << endl;
        cout << "Exit program" << endl;
    }

    //Spell check gettysburg_address.txt
    cout << "Misspelled words : " << endl;
    cout << endl;

    //If a word is not in the dictionary assume misspelled
    while(input >> word)
    {
        removePunct(word);
        upToLow(word);
        if(h.containsString(word) == false)
        {
            countMisspelled++; // Increment misspelled words count
            cout << word << " ";
            if(countMisspelled % 20 == 0) // Display misspelled words 20 per line
            {
                cout << endl;
            }
        }
        else
        {
            countCorrect++; // Increment correct words count
        }
    }
    input.close();

    cout << endl;
    cout << endl;

    cout << "Number of misspelled words : " << countMisspelled << endl;
    cout << "Number of correct words : " << countCorrect << endl;

    return 0;
}


/*Function to convert uppercase letters to lowercase*/
void upToLow(string & str)
{
    for (unsigned int i = 0; i < strlen(str.c_str()); i++)
         if (str[i] >= 0x41 && str[i] <= 0x5A)
              str[i] = str[i] + 0x20;
}


/*Function to remove punctuation from string*/
void removePunct(string & str)
{
    str.erase(remove_if(str.begin(), str.end(), static_cast<int(*)(int)>(&ispunct)),str.end());
}
int collisionArray[SIZE]; 
HashTable::HashTable()
{
 for (int i = 0; i < SIZE; i++)
 {
  hashArray[i] = NULL;
  collisionArray[i] = 0;
 }
}
void HashTable::put(string s)
{
    int count = 0;
    int hash = computeHash(s);
    if (search(hashArray[hash], s) == NULL)
    {
        // Only add the target if it's not in the list
        headInsert(hashArray[hash], s);
        collisionArray[hash]++;
    }
}