해시 테이블을 생성하고 파일에 저장 한 프로그램을 작성했습니다. 나는 사용자가 키를 입력하고, 그 키에 대한 파일을 검색하고, 입력 된 키와 일치하는 레코드에 저장된 정보를 표시 할 수있는 두 번째 프로그램을 작성해야한다.파일에 저장된 해시 테이블 검색
내 프로그램에서 해시 테이블을 올바르게 만들고 올바른 파일에 저장했지만 파일을 검색하는 데 문제가 있습니다. 해시 테이블에있는 키를 입력하면 "이 키를 찾을 수 없습니다"라는 메시지가 나타납니다.
내 헤더 파일 :
//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);
};
방법에 대한 정의를 포함하는 내 .cpp 파일 :
//prog8.cpp
#include "prog8.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Hash::Hash() {
for(int i = 0; i < hashSize; i++) {
hashTable[i] = new record;
hashTable[i]->key = 8;
hashTable[i]->name = "blank";
hashTable[i]->code = 0;
hashTable[i]->cost = 0.0;
hashTable[i]->next = NULL;
}
}
void Hash::addRecord(int key, string name, int code, double cost) {
int index = hash(key);
if(hashTable[index]->key == 8) {
hashTable[index]->key = key;
hashTable[index]->name = name;
hashTable[index]->code = code;
hashTable[index]->cost = cost;
}
else {
record* ptr = hashTable[index];
record* newRecord = new record;
newRecord->key = key;
newRecord->name = name;
newRecord->code = code;
newRecord->cost = cost;
newRecord->next = NULL;
while(ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = newRecord;
}
}
int Hash::numInIndex(int index) {
int count = 0;
if(hashTable[index]->key == 0) {
return count;
}
else {
count++;
record* ptr = hashTable[index];
while(ptr->next != NULL) {
count++;
ptr = ptr->next;
}
}
return count;
}
int Hash::hash(int key) {
int index = key % 5;
return index;
}
void Hash::saveRecords() {
ofstream recordsFile;
recordsFile.open("records.dat");
int number;
for(int i = 0; i < hashSize; i++) {
number = numInIndex(i);
recordsFile << "-------------\n";
recordsFile << "Index = " << i << endl;
recordsFile << hashTable[i]->key << endl;
recordsFile << hashTable[i]->name << endl;
recordsFile << hashTable[i]->code << endl;
recordsFile << hashTable[i]->cost << endl;
recordsFile << "Number of items in index = " << number << endl;
recordsFile << "-------------\n";
}
}
void Hash::saveRecordsInIndex(int index) {
ofstream recordsFile;
recordsFile.open("records.dat");
record* ptr = hashTable[index];
if(ptr->key == 0) {
cout << "Index " << index << " is empty";
}
else {
cout << "Index " << index << " contains the following records:\n";
while(ptr != NULL) {
recordsFile << "--------------\n";
recordsFile << ptr->key << endl;
recordsFile << "--------------\n";
ptr = ptr->next;
//index++;
}
}
}
void Hash::findRecord(int key) {
ifstream recordsFile;
recordsFile.open("records.dat");
if(!recordsFile.is_open()) {
cerr << "Error opening file" << endl;
}
int index = hash(key);
bool wasFound = false;
record* ptr = hashTable[index];
while(ptr != NULL) {
if(ptr->key == key) {
wasFound = true;
key = ptr->key;
}
ptr = ptr->next;
}
if(wasFound == true) {
cout << key;
}
else {
cout << "There was no record matching the key " << key << " found."
<< endl;
}
recordsFile.close();
}
나의 첫번째 기본 파일 :
다음 내 코드입니다//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;
}
An 여기
//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;
}
내가지고있어 출력의 예입니다, 생성 된 records.dat 파일로 완성 : :
[[email protected] ~]$ g++ -o prog8 prog8.cpp prog8main.cpp
[[email protected] ~]$ prog8
[[email protected] ~]$ 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
-------------
[[email protected] ~]$ g++ -o prog8search prog8.cpp prog8search.cpp
[[email protected] ~]$ prog8search
Please enter a key 12345
There was no record matching the key 12345 found.
어떤 도움이 라 내 주요 파일은 출력 파일을 검색 할 수 대단히 감사하겠습니다.
저장소 선택에서 상당히 간결한 것으로 시작하십시오. 당신은 틀림없이 무의미한 양의 데이터를 당신에게 * 아무것도 쓰지 않지만 파일을 더 읽기 어렵게 만드는 .dat 파일에 저장합니다. – WhozCraig
좋아,하지만 지금은 이미 내가 가지고있는 것을 어떻게 검색 할 수 있는지에 대한 아이디어가 있습니까? – HiTechRedneck3
'findRecord'는 파일을 열지 만 아무것도 읽지 않으므로 테이블이 비어 있습니다. 아마도 "records.dat"를 기계가 읽을 수있는 형식으로 작성하고 싶을 것입니다. 또한 파일을 검색 할 때마다 파일을 읽는 것은 매우 비효율적입니다. 다른 기능에서 로딩 및 검색을 분리하십시오. – molbdnilo