2012-11-21 106 views
1

我已經爲學校中的分配創建了一個散列分類,用於實現散列並處理衝突。當通過上傳25個項目(學生記錄)測試這個類時,我注意到我的散列類只是簡單地將每個項目插入用於存儲的數組(而不是使用散列代碼)。然而,在調試模式下,insertItemCollision()函數可以正確計算位置,但是按照連續順序插入項目。散列分類散列

是什麼導致了這種情況的發生以及如何解決?謝謝!

hash.h

//hash.h 
#include <string> 
struct Student 
{ 
    std::string name; 
    std::string id; 
}; 

class MyHash{ 
public: 
    MyHash(); 
    int hashCode(int, int); 
    void insertItemCollision(std::string, std::string); 
    std::string retrieveItem(std::string); 
    Student students[100]; 
}; 

hash.cpp

//hash.cpp 
#include <iostream> 
#include "Hash.h" 


MyHash::MyHash() 
{ 
} 

int MyHash::hashCode(int id, int max) 
{ 

    return (id % max); 
} 

void MyHash::insertItemCollision(std::string id, std::string name) 
{ 
    int idInt = atoi(id.c_str()); 
    int location; 


    location = hashCode(idInt, 100); 
    while (students[location].id != "") 
    location = (location + 1) % 100; 
    students[location].id = id; 
    students[location].name = name; 
} 


std::string MyHash::retrieveItem(std::string id) 
{ 
    int location; 
    int startLoc; 
    int idInt = atoi(id.c_str()); 
    bool moreToSearch = true; 
    bool found; 
    std::string item; 

    startLoc = hashCode(idInt, 100); 
    location = startLoc; 
    do 
    { 
     if (students[location].id == id || students[location].id == "") 
     moreToSearch = false; 
    else 
     location = (location + 1) % 100; 
    } while (location != startLoc && moreToSearch); 
    found = (students[location].id == id); 
    if (found) 
     item = students[location].name; 
    return item; 
} 

students.txt

//students.txt 
9892 Zack Lewis 
4592 Ken Rodriguez 
9819 Anderson Clark 
1519 Ben Robinson 
4597 Abigail Martinez 
8542 Madison Garcia 
6113 Mia Thompson 
8591 Chloe Martin 
9491 Daniel Harris 
1698 Aiden White 
5984 Alexander Walker 
6541 Ethan Jackson 
9549 Michael Thomas 
5949 Emily Anderson 
9861 Ava Taylor 
5412 Noah Moore 
6262 Olivia Wilson 
1954 Jayden Miller 
4954 William Davis 
9567 Emma Brown 
5195 Mason Jones 
9195 Isabella Williams 
5199 Sophia Johnson 
1294 Jacob Smith 

driver.cpp

//driver.cpp 
#include <iostream> 
#include<string> 
#include<fstream> 
#include "Hash.h" 
using namespace std; 


int read(string[]); 
void splitString(string, Student&); 
void init(string[], MyHash*, int); 
int showMenu(); 

int main() 
{ 
    int size; 
    int choice; 
    string input[100]; 
    MyHash* h = new MyHash(); 
    size = read(input); 
    init(input, h, size); 

    do 
    { 
     choice = showMenu(); 
     if (choice == 1) 
     { 
      string id; 
      cout << "Enter the id of the sutdent you would like to find: " << endl; 
      cin >> id; 
      std::string s = (*h).retrieveItem(id); 
      if (s != "") 
       cout << "The students name is: "<< s << endl; 
      else 
       cout << "No students matching that id was found!" <<endl; 
     } 

    }while (choice != 2); 

    system("pause"); 
    return 0; 
} 

int read(string st[]) 
{ 
    int size = 0; 
    ifstream infilestream; 
    infilestream.open("test.txt"); 


    for(int i = 0; infilestream.good(); i++) 
    { 
     getline(infilestream, st[i]); 
     cout<<st[i] <<endl; 
     size++; 
    } 
    infilestream.close(); 
    return size; 
} 

void splitString(string record, Student& s) 
{ 
    s.id = record.substr(0, 4); 
    s.name = record.substr(5, record.length()); 
} 

void init(string inputs[], MyHash* myHash, int size) 
{ 
    for(int i = 0;i < size; i++) 
    { 
     splitString(inputs[i],myHash->students[i]); 
     //cout << stus[i].name << " " << stus[i].id << endl; 
     myHash->insertItemCollision(myHash->students[i].id, myHash->students[i].name); 
    } 
} 

int showMenu() 
{ 
    int chs; 
    cout << "1. Find student by id." << endl; 
    cout << "2. Exit." << endl; 
    cin >> chs; 
    return chs; 
} 

修復: 這兩個功能進行了更新:

Student* splitString(string record) 
{ 
    Student* stu = new Student(); 
    stu->id = record.substr(0, 4); 
    stu->name = record.substr(5, record.length()); 
    return stu; 
} 

void init(string inputs[], MyHash* myHash, int size) 
{ 
    for(int i = 0;i < size; i++) 
    { 
     Student* s = new Student(); 
     s = splitString(inputs[i]);//,myHash->students[i]); 
     //cout << stus[i].name << " " << stus[i].id << endl; 
     //myHash->insertItemCollision(myHash->students[i].id, myHash->students[i].name); 
     myHash->insertItemCollision(s->id, s->name); 
    } 
} 
+0

您在插入時使用成員'students',而且在分割時使用。它們被覆蓋。 – imreal

+1

哈哈...只是改變了我的代碼,並得到它的工作。我會在一秒之內發佈它。謝謝! – Zzz

+0

你想發表你的評論作爲答案,我可以接受嗎? – Zzz

回答

1

的問題是,你正在使用的成員students當你插入(insertItemCollision),而且當你分割(splitString)。所以他們會被覆蓋。