Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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/0/search/2.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++_Search_Hash - Fatal编程技术网

C++ 搜索保存在文件中的哈希表

C++ 搜索保存在文件中的哈希表,c++,search,hash,C++,Search,Hash,我编写了一个程序,创建了一个哈希表并将其保存到一个文件中。我应该编写第二个程序,允许用户输入密钥,在文件中搜索该密钥,并显示与输入的密钥匹配的记录中存储的信息 我的程序正确地创建了哈希表并将其保存到正确的文件中,但是,我在搜索该文件时遇到了问题。当我输入一个我知道在哈希表中的键时,我会收到“thiskeywasnotfound”消息 这是我的密码: 我的头文件: //prog8.h #include <iostream> #include <fstream> #inclu

我编写了一个程序,创建了一个哈希表并将其保存到一个文件中。我应该编写第二个程序,允许用户输入密钥,在文件中搜索该密钥,并显示与输入的密钥匹配的记录中存储的信息

我的程序正确地创建了哈希表并将其保存到正确的文件中,但是,我在搜索该文件时遇到了问题。当我输入一个我知道在哈希表中的键时,我会收到“thiskeywasnotfound”消息

这是我的密码:

我的头文件:

//prog8.h
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Hash {
private:
    static const int hashSize = 8;
    struct record {
        int key;
        string name;
        int code;
        double cost;
        record* next;
    };
    record* hashTable[hashSize];
public:
    Hash();
    int hash(int key);
    void addRecord(int key, string name, int code, double cost);
    int numInIndex(int index);
    void saveRecords();
    void saveRecordsInIndex(int index);
    void findRecord(int key);
};
任何帮助都将不胜感激

ifstream recordsFile;
recordsFile.open("records.dat");
if(!recordsFile.is_open()) {
    cerr << "Error opening file" << endl;
}
根据定义,该表将为空,因为您仍然需要从records.dat文件中读取任何内容


记住:编程不是魔术。一开始可能看起来是这样的,但除非代码是由任何人编写的(不一定是您编写的)

我发现您的代码存在许多问题:

  • 你的散列索引大小是8?那你为什么要在5点之前搬家?这意味着单元格5、6和7将始终为空,因为它们将被重定向到0、1和2

  • 为什么在main()中每次读取记录后都要输出?为什么不在最后这样做呢?(如果你不坚持那种糟糕的支撑风格,你会更清楚你在做什么,尽管其他人在风格问题上会不同意我)

  • 您的代码当然会泄漏,是不正确的,一般来说可以做得更好

  • “findRecord”对输入文件有什么作用?我看不见它在读它

  • 为什么要检查key==0?那不是一把有效的钥匙吗


  • 首先,您的存储选择要简洁得多。您保存的数据量毫无意义。dat文件只会增加读取该文件的难度。好的,但现在您是否知道如何搜索我已有的内容?您的
    findRecord
    打开一个文件,但不从中读取任何内容,因此您的表是空的。您可能希望以更具机器可读性的格式编写“records.dat”。此外,每次要搜索文件时读取该文件的效率将非常低。在不同的函数中分别加载和搜索。此外,建议将“哈希表”实现为单链表的教授可能会被立即解雇,非常感谢。(prog08.h和cs331129@cs这些都是告密的迹象,让我为CS教育的现状而哭泣)。不知道我需要做些什么才能正确阅读?我试着这样做,基本上就像我用输入文件来创建表一样,但是当我这样运行时,会被要求输入一个键,然后一旦我这样做,程序就会退出而不显示任何内容。这是不进行输入验证的症状。这是一个开始:我没有明确地试图理解这是一个哈希表(我想你可能在某些地方使用键作为哈希,但这取决于你),我不得不使用作为指令的一部分给出的哈希函数,它是键mod 5的值。除此之外,是的,我知道我在这方面很差劲,这就是为什么我下学期要换专业的原因。我的大脑就是不这样工作,我可以整天阅读所有这些东西,但它就是不点击。因此,我对第二个和第三个问题没有真正的回应。像我这样在这个行业工作的专业人士可能比你的教授更了解如何编写好代码。你只是看起来很惊讶,5号、6号和7号单元格一直都是空的,而实际上它们一直都是空的。但是,与0的比较是什么:nt Hash::numInIndex(int index){int count=0;if(hashTable[index]->key==0){返回计数;哦,毫无疑问,我非常尊重你们所做的。不,我有点意识到当我看到它将被修改为5而不是通常情况下的表大小时,它不会使用最后的几个空格。我想他只是想确保我们会遇到我们必须处理的碰撞。而且t是一个输入错误,应该是8而不是0。我们应该用“虚拟记录”填充文件密钥是文件中的第一个数字,是8,这就是它的来源。哦,findRecord方法应该只查看输出file@CashCow是的。我曾试图挽回其中一些分数,但最终还是输了(见)
    //prog8main.cpp
    #include "prog8.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main() {
        ifstream file;
        file.open("prog8.dat");
        if(!file.is_open()) {
            cerr << "Error opening file" << endl;
        }
        int index;
        int key;
        string name;
        int code;
        double cost;
        Hash hashObj;
        if(key != 8) {
            while(file >> key && file >> name && file >> code && file >> cost) {
                hashObj.addRecord(key, name, code, cost);
                hashObj.saveRecords();
                //hashObj.saveRecordsInIndex(index);
            }
        }
        file.close();
        return 0;
    }
    
    //prog8search.cpp
    #include "prog8.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main() {
        int key;
        Hash hashObj;
        cout << "Please enter a key ";
        cin >> key;
        hashObj.Hash::findRecord(key);
    
        return 0;
    }
    
    [cs331129@cs ~]$ g++ -o prog8 prog8.cpp prog8main.cpp
    [cs331129@cs ~]$ prog8
    [cs331129@cs ~]$ cat records.dat
    -------------
    Index = 0
    12345
    Item06
    45
    14.2
    Number of items in index = 2
    -------------
    -------------
    Index = 1
    34186
    Item25
    18
    17.75
    Number of items in index = 2
    -------------
    -------------
    Index = 2
    12382
    Item09
    62
    41.37
    Number of items in index = 3
    -------------
    -------------
    Index = 3
    8
    blank
    0
    0
    Number of items in index = 1
    -------------
    -------------
    Index = 4
    12434
    Item04
    21
    17.3
    Number of items in index = 1
    -------------
    -------------
    Index = 5
    8
    blank
    0
    0
    Number of items in index = 1
    -------------
    -------------
    Index = 6
    8
    blank
    0
    0
    Number of items in index = 1
    -------------
    -------------
    Index = 7
    8
    blank
    0
    0
    Number of items in index = 1
    -------------
    [cs331129@cs ~]$ g++ -o prog8search prog8.cpp prog8search.cpp
    [cs331129@cs ~]$ prog8search
    Please enter a key 12345
    There was no record matching the key 12345 found.
    
    ifstream recordsFile;
    recordsFile.open("records.dat");
    if(!recordsFile.is_open()) {
        cerr << "Error opening file" << endl;
    }
    
    record* ptr = hashTable[index];