1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <fstream>
#include <string>
#include "hashT.h"
#include "stateData.h"
using namespace std;
int hashFunc(string name, int size);
int main()
{
hashT<StateData> HashTable(50);
int size = 15;
ifstream infile;
infile.open("states.txt");
if(!infile)
{
cout << "Error reading states file.\n";
exit(1);
}
char ch;
int year, order, key;
string state, capitol;
bool found;
getline(infile, state);
while(infile)
{
getline(infile, capitol);
infile >> year;
infile >> order;
StateData temp;
temp.setStateInfo(state, capitol, year, order);
infile.get(ch);
key = hashFunc(state, size);
HashTable.insert(key, temp);
getline(infile, state);
}
HashTable.print();
// here is the code in question
cout<<"Enter item to be deleted: ";
getline(cin, state);
cout<<endl;
key = hashFunc(state, size);
// how do I determine dtemp in order to execute the remove function?
HashTable.remove(key, dtemp);
HashTable.print();
infile.close();
system("pause");
return 0;
}
int hashFunc(string name, int size)
{
int i, sum, len;
i= 0;
sum = 0;
len = name.length();
for (int k=0; k < 15 - len; k++)
name = name + ' ';
for (int k = 0; k < 5; k++)
{
sum = sum + static_cast<int>(name[i]) * 128 * 128
+ static_cast<int>(name[i+1]) * 128
+ static_cast<int>(name[i+2]);
i = i+3;
}
return sum % size;
}
|