2017-11-16 12 views
0

ハッシュテーブル関数を記述していますが、そのデータはテキストファイルからです。テキストファイルには、4桁の学生IDと名前にスペースが埋め込まれています。問題は、挿入関数がファイルから最初の行を取得しないことです。私はプログラムを実行し、テストするとファイルの最初の生徒が出てこない。生徒の名前を入力して残りの生徒の名前を確認し、名前を出力します。また、ファイルから取得したフルネームは出力されません。最初のものだけが出力されます。私がファイルからchris camposを調べると、4桁のIDを入力しますが、chrisだけがchris camposではなく出力されます。ハッシュテーブルの印刷と挿入機能

完全なコードを表示したい場合は投稿を編集しますが、投稿したコードにエラーがあるはずです。私はコードが正しいと思うが、私は他の人がエラーを見ることができるか、それを改善する方法を見たいと思う。

また、私は不思議です、私は衝突を処理するためにチェーンを使用しています。同じ4桁のIDを持つバケット内のすべての名前をプログラムに出力させるにはどうしたらいいですか? 2つの名前が同じIDを持つ場合、ユーザーがIDを入力すると、2つの名前をプログラムに出力させることができます。 ...ここで

void HashTable::Insert(std::string ID, std::string name) 
{ 
    int location = Hash(ID, tableSize); 

    if (listofStudents[location]->m_idNum == "empty") 
    { 
     listofStudents[location]->m_idNum = ID; 
     listofStudents[location]->m_Name = name; 
    } 
    else 
    { 

     Student* ptr = listofStudents[location]; 
     Student* newStudent = new Student; 
     newStudent->m_Name = name; 
     newStudent->m_idNum = ID; 
     newStudent->next = NULL; 

     while (ptr->next != NULL) 
     { 
      ptr = ptr->next; 
     } 
     ptr->next = newStudent; 
    } 

} 

は私の機能を取得..ですここで

HashTable hashtable; 
std::ifstream file("students.txt"); 
int option; 
std::string studentID; 
std::string studentName; 
std::string line; 

if (!file.is_open()) 
{ 
    std::cout << "Error in opening file\n"; 
} 
else 
{ 
    while (std::getline(file, line)) 
    { 
     file >> studentID >> studentName; 
     hashtable.Insert(studentID, studentName); 
    } 

    file.close(); 
} 

は私の挿入機能である

:ここ

2301 Robb Arredondo 
5401 Chris Campos 
6305 Yogi Bear 
9108 Yoshi Man 
0310 John Du 
1812 Maria Yu 
4318 Power Ranger 
7122 Bob Chan 
8225 Will Boo 
5324 Ghost Lee 
0134 Mary Su 
2150 Jane Mary 
1100 Gary Campos 
2305 Alan Kong 
3420 Bill Nye 
5608 Alex Garcia 
9112 Goku Nani 
6750 Paul Avalos 
1220 Jason Noni 
9005 Oscar Roger 
6550 Geo Qwerty 
1112 Mini Me 
2315 Garfield Beria 
4201 Just Saying 

は私のプログラムは、ファイルからデータを取得する方法であります

void HashTable::Retrieve(std::string ID) 
{ 
int location = Hash(ID, tableSize); 
Student* ptr; 

for (ptr = listofStudents[location]; ptr; ptr = ptr->next) 
{ 
    if (ptr->m_idNum == ID) 
    { 
     std::cout << "--------------------\n"; 
     std::cout << "Name of Student: " << ptr->m_Name << std::endl; 
     std::cout << "---------------------\n"; 
    } 
    else 
     std::cout << "No Student Found\n"; 
} 
} 

ここに私のハッシュ関数あなたがハッシュ技法がどのように働いているかを見たいと思えば、それは文字列をintに変換します。文字列を使用している理由は、割り当てに文字列を使用する必要があるためです。

int HashTable::Hash(const std::string& key, int tablesize) 
{ 
    int hashVal = 0; 
    for (int i = 0; i < key.length(); i++) 
    { 
     hashVal = 37 * hashVal + key[i]; 
    } 
    hashVal %= tablesize; 

    if (hashVal < 0) 
     hashVal += tablesize; 

    return hashVal; 
} 

答えて

0

犯人はここにある:

file >> studentID >> studentName; 

これは、あなたはそれがないと思う何をしません。私はstd::stringstreamlineをダンプして、そこからフィールドを抽出することをお勧め:あなたは単一の文字列に名前をダンプしようとしているが、実際には二つのフィールド(姓と名)がある

while (std::getline(file, line)) 
{ 
    std::stringstream ss(line); 
    std::string ID; 
    std::string firstName; 
    std::string lastName; 

    ss >> ID >> firstName >> lastName; 
    hashtable.Insert(studentID, firstName + " " + lastName); 
} 

注意、2つの変数が必要です。その後、それらを1つの文字列に組み合わせることができます。

P.S.ハッシュテーブルから、STLハッシュマップの実装(std::unordered_map)はあなたの友人です。

+0

を私はSTLを使用できることを願うが、私は、このシナリオではできません。エラーを指摘してくれてありがとう。私はfstreamがファイル内で行単位で読むことができると言われました。 – Fus10n

+0

まだファイルのファーストネームは取得されていません。最初のファイルを除いて、ファイル内の他のすべての名前を取得します。 – Fus10n

+0

'file >> studentID >> firstName >> lastName;'は必要ありません。完全に削除してください。現在の行のフィールドは 'ss 'から抽出されます。 – cantordust

0

ここにあなたの問題:

while (std::getline(file, line)) 
{ 
    file >> studentID >> studentName; // This only read the id and the first name 
    hashtable.Insert(studentID, studentName); 
} 

あなたは試してみてください:

file >> studentID >> firstname >> lastname; 
file.ignore(); // this will pass the EOL character 
+0

それは私のインサートの実装を台無しにしませんか?私はそれに対して2つのパラメータしか使用できないので、私の挿入関数はどのように姓に入るのでしょうか? – Fus10n

+0

peek()メソッドを使用できます。char x; while((file.peek()!= '\ n')&&(ファイル>> x))studentname + = x; –

関連する問題