2017-05-16 5 views
0

간단한 해시 테이블 프로젝트를 만들려고합니다. addPlayer 함수가 작동하지 않는 것을 발견했을 때 프로그램을 테스트하고있었습니다. addPlayer는 몇 가지 다른 함수를 사용하지만 이러한 함수도 확인 했으므로 오류를 찾을 수 없습니다. 이것에 대한 어떤 도움이라도 정말로 감사 할 것입니다, 제가 스스로 진단 할 수 있다면 그렇게 할 수있을 것입니다. 의 전체 프로그램은 다음과 같습니다 :해시 테이블 addFunction

헤더 파일 (Player.h)

#include <iostream> 
#include <string> 

    using namespace std; 

    #ifndef PLAYER_H 
    #define PLAYER_H 

    class Player 
    { 
    private: 
     static const int tableSize = 10; 

     struct player 
     { 
      string name; 
      string race; 
      player* next; 
     }; 

     player* HashTable[tableSize]; 

    public: 
     //Constructor 
     Player(); 
     //Hash function 
     int hash(string key); 
     //Adds players and takes in their attributes 
     void addPlayer(string name, string race); 
     //Counts number of players in index 
     int numPlayersInIndex(int index); 
     //Prints information from the items held in the hash table 
     void printTable(); 
    }; 
    #endif 

홈페이지 (하여 Main.cpp)

#include <cstdlib> 
#include <iostream> 
#include <string> 

//Custom headers 
#include "Player.h" 
//Pause console before exiting window 
#include <stdlib.h> 

#ifdef _WIN32 
#define WINPAUSE system("pause") 
#endif 

using namespace std; 

int main(int argc, char** argv) 
{ 
    //Test addPlayer 
    Player player1; 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("hapdsfdsfsdsdfsdfsdfpy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.printTable(); 

    //Call WINPAUSE to pause console window before exiting 
    WINPAUSE; 
    return 0; 
} 

클래스 (Player.cpp)

#include <cstdlib> 
#include <iostream> 
#include <string> 

#include "Player.h" 

using namespace std; 


//Player constructor sets default values for the attributes 
//of the player 
Player::Player() 
{ 
    for (int i = 0; i < tableSize; i++) 
    { 
     HashTable[i] = new player; 
     HashTable[i]->name = "empty"; 
     HashTable[i]->race = "empty"; 
     HashTable[i]->next = NULL; 
    } 
} 

void Player::addPlayer(string name, string race) 
{ 
    int index = hash(name); 
    //Check to see if the attributes have been set at index 
    if (HashTable[index]->name == "empty") 
    { 
     HashTable[index]->name == name; 
     HashTable[index]->race == race; 
    } 

    //If they've already been set, then make an addition to the list 
    else 
    { 
     player* ptr = HashTable[index]; 
     player* newPlayer = new player; 
     newPlayer->name = name; 
     newPlayer->race = race; 
     newPlayer->next = NULL; 
     //Traverse to the end of the list 
     while (ptr->next != NULL) 
     { 
      ptr = ptr->next; 
     } 
     //Links the last item in the list to the new player being added 
     //New player now sits at the end of the list 
     ptr->next = newPlayer; 
    } 
} 

//Function to count the number of players 
int Player::numPlayersInIndex(int index) 
{ 
    //Sentinal variable to keep track of positon in list 
    int count = 0; 

    //If the index is empty, or has default value, return 0 to mark 
    //as empty. 
    if (HashTable[index]->name == "empty") 
    { 
     return count; 
    } 

    //Otherwise, increment count and then traverse the list pointed to 
    //in the hash table. 
    else 
    { 
     count++; 
     player* ptr = HashTable[index]; 
     while (ptr->next != NULL) 
     { 
      count++; 
      ptr = ptr->next; 
     } 
    } 
    return count; 
} 

void Player::printTable() 
{ 
    //Holds number of elements in each bucket 
    int number; 
    for (int i = 0; i < tableSize; i++) 
    { 
     //Sets number to the number of players in the hash table 
     number = numPlayersInIndex(i); 
     cout << "-------------" << endl; 
     cout << "index = " << i << endl; 
     cout << HashTable[i]->name << endl; 
     cout << HashTable[i]->race << endl; 
     cout << "Number of characters: " << number << endl; 
     cout << "-------------" << endl; 
    } 
} 

//Recursive function to create simple and fairly unique hash pattern 
//to process objects 
int Player::hash(string key) 
{ 
    int hash = 0; 
    int index; 

    for (int i = 0; i < key.length(); i++) 
    { 
     hash = hash + (int)key[i]; 
    } 

    index = hash % tableSize; 

    return index; 
} 

답변

0
HashTable[index]->name == name; 
    HashTable[index]->race == race; 

변경 i t에서

HashTable[index]->name = name; 
    HashTable[index]->race = race; 
+0

아, 정말 고마워요! 난 그냥 테스트하고 그것은 여섯 번째 요소를 추가 할뿐입니다. 이견있는 사람? – user3352763

+0

그것은 6 번째 요소가 아니라 6 번째 인덱스입니다. – d9ngle

+0

해시 테이블에 10 개의 슬롯이 있고, 두 개의 키가 동일한 슬롯 (예 : 6 % 10 = 6, 16 % 10 = 6)에 해시 된 경우 링크 된 목록을 사용하여 저장합니다. – d9ngle