-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (143 loc) · 4.57 KB
/
main.cpp
File metadata and controls
157 lines (143 loc) · 4.57 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class listNode {
public:
string firstName;
string lastName;
listNode *next;
listNode(string fn, string ln){
firstName = fn;
lastName = ln;
}
void printNode(listNode *node, ofstream &outFile){
if (this->next == NULL)
outFile << "(" << this->firstName <<" "<< this->lastName <<" NULL)-->NULL";
if (this->next != NULL)
outFile << "(" << this->firstName <<" "<<this->lastName <<" , "
<< this->next->firstName << ")-->" ;
}
};
class hashTable {
public:
char op;
int bucketSize;
listNode* hashT[1000];
hashTable(int n){
bucketSize = n;
}
int Doit (string lastName){
unsigned int val = 1;
for (int i = 0; i<lastName.length(); i++){
val = val*32 + (int)lastName[i];
}
return val%bucketSize;
}
void createHashTable (){
for (int i = 0; i < bucketSize; i++){
listNode* dummy = new listNode("dummyfirst", "dummylast");
hashT[i] = dummy;
}
}
void informationProcessing (ifstream &infile, ofstream &outFile2){
string firstName;
string lastName;
int index;
while (infile >> op >> firstName >> lastName){
outFile2<<"Processing "<<op<<" "<<firstName<<" "<<lastName<<"..."<<endl;
index = Doit(lastName);
outFile2<<"Target Index: "<<index<<endl;
printList(index, outFile2);
if (op == '+'){
hashInsert (index, firstName, lastName, outFile2);
}
if (op == '-'){
hashDelete (index, firstName, lastName, outFile2);
}
if (op == '?'){
hashRetrieval (index, firstName, lastName, outFile2);
}
}
}
listNode* findSpot (int index, string firstName, string lastName){
listNode* spot = hashT[index];
while (spot->next != NULL
&& spot->next->lastName < lastName)
spot = spot->next;
while (spot->next != NULL
&& spot->next->lastName == lastName
&& spot->next->firstName < firstName)
spot = spot->next;
return spot;
}
void hashInsert(int index, string firstName, string lastName, ofstream &outFile2){
outFile2 << "***Performing hashInsert on "<<firstName<<" "<< lastName<< endl;
listNode* spot = findSpot(index, firstName, lastName);
if (spot->next != NULL && spot->next->lastName == lastName && spot->next->firstName == firstName)
outFile2<<"*** Warning, the record "<<firstName<< " " <<lastName<<" is in the database!"<<endl<<endl;
else{
listNode* newNode;
newNode = new listNode(firstName, lastName);
newNode->next = spot->next;
spot->next = newNode;
outFile2<<firstName<<" "<<lastName<<" has been INSERTED on "<<"HashTable["<<index<<"]: ";
printList(index, outFile2);
outFile2<<endl;
}
}
void hashDelete(int index, string firstName, string lastName, ofstream &outFile2){
outFile2 << "***Performing hashDelete on "<<firstName<<" "<< lastName<< endl;
listNode* spot = findSpot(index, firstName, lastName);
if (spot->next != NULL && spot->next->lastName == lastName && spot->next->firstName == firstName){
listNode* junk;
junk = spot->next;
spot->next = spot->next->next;
junk->next = NULL;
free(junk);
outFile2<<firstName<<" "<<lastName<<" has been DELETED on "<<"HashTable["<<index<<"]: ";
printList(index, outFile2);
outFile2<<endl;
}
else outFile2<<"*** Warning, the record "<<firstName<< " " <<lastName<<" is NOT in the database!"<<endl<<endl;
}
void hashRetrieval(int index, string firstName, string lastName, ofstream &outFile2){
outFile2 << "***Performing hashRetrieval on "<<firstName<<" "<< lastName<< endl;
listNode* spot = findSpot(index, firstName, lastName);
if (spot->next != NULL && spot->next->lastName == lastName && spot->next->firstName == firstName){
outFile2<<"Yes, the record "<<firstName<< " " <<lastName<<" is in the database!"<<endl<<endl;
}
else outFile2<<"No, the record "<<firstName<<" "<<lastName<<" is NOT in the database!"<<endl<<endl;
}
void printList(int index, ofstream &outFile){
listNode* tmp = hashT[index];
while (tmp != NULL){
tmp->printNode(tmp, outFile);
tmp = tmp->next;
}
outFile<<endl;
}
void printHashTable (ofstream &outFile){
for (int i; i<bucketSize; i++){
outFile<<"HashTable["<<i<<"]: ";
printList(i, outFile);
}
}
};
int main(int argc, char *argv[]){
ifstream input;
input.open(argv[1]);
ofstream output1, output2;
output1.open(argv[3]);
output2.open(argv[4]);
int i = atoi(argv[2]);
hashTable myHash(i);
myHash.createHashTable();
myHash.informationProcessing(input, output2);
myHash.printHashTable(output1);
input.close();
output1.close();
output2.close();
cout<<"DONE";
return 0;
}