-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHash.cpp
More file actions
177 lines (133 loc) · 2.95 KB
/
Copy pathPHash.cpp
File metadata and controls
177 lines (133 loc) · 2.95 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Name: Matthew Wilson
Course: CSCE 2110.002
Date: 11/15/21
This header file defines the member functions of the Plane hash table
*/
#include "PHash.h"
PHash::PHash(string file)
{
for(int i = 0; i < size; i++)
{
HashTable[i] = new node;
}
ifstream fin(file);
string line, word, Make, Model, LastMaint, LAstMaintA;
int ID = 0, i = 0;
getline(fin, line);
while(getline(fin, line))
{
for(unsigned int i = 0; i < line.length(); i++)
{
if(line[i] == ',')
{
line[i] = ' ';
}
}
stringstream ss(line);
ss >> ID >> Make >> Model >> LastMaint >> LAstMaintA;
i = hash(ID);
if(HashTable[i]->make == " ")
{
HashTable[i]->id = ID;
HashTable[i]->make = Make;
HashTable[i]->model = Model;
HashTable[i]->lastMaint = LastMaint;
HashTable[i]->lAstMaintA = LAstMaintA;
}
else//handling collision by linear probing
{
for(int i = 0; i < size; i++)
{
if(HashTable[i]->id == -999)
{
HashTable[i]->id = ID;
HashTable[i]->make = Make;
HashTable[i]->model = Model;
HashTable[i]->lastMaint = LastMaint;
HashTable[i]->lAstMaintA = LAstMaintA;
break;
}
}
}
}
fin.close();
}
int PHash::hash(int key)//mid-square hashing
{
long int hash = key * 999;
int index;
hash *= hash;
hash = hash / 10000;
hash = hash % 100000000;//gets middle digits
index = hash % size;
return index;
}
void PHash::display()
{
node* ptr;
cout << "ID\t" << "Maker\t" << "Model\t\t" << "Last Maintained Date\t" << "Last Maintained Airport" << endl;
for(int i = 0; i < size; i++)
{
ptr = HashTable[i];
if(ptr->make != " ")
{
cout << ptr->id << "\t";
cout << ptr->make << "\t" << ptr->model << "\t\t\t";
cout << ptr->lastMaint << "\t\t" << ptr->lAstMaintA;
// cout << "\t\t\tIndex: " << i;//testing
cout << endl;
}
}
cout << endl;
}
void PHash::insert(int ID, string mk, string md, string lm, string lma)
{
int index = hash(ID);
if(HashTable[index]->id != -999)
{
cout << "Error in plane table: need unique key from ID." << endl;
return;
}
HashTable[index]->id = ID;
HashTable[index]->make = mk;
HashTable[index]->model = md;
HashTable[index]->lastMaint = lm;
HashTable[index]->lAstMaintA = lma;
}
void PHash::update(int ID, string mk, string md, string lm, string lma)
{
int index = hash(ID);
if(HashTable[index]->make == " ")
{
cout << "Error in plane table: index is empty." << endl;
return;
}
HashTable[index]->id = ID;
HashTable[index]->make = mk;
HashTable[index]->model = md;
HashTable[index]->lastMaint = lm;
HashTable[index]->lAstMaintA = lma;
}
void PHash::select(string Date)
{
int j = -1;
int a[23];
for(int i = 0; i < size; i++)
{
if(HashTable[i]->lastMaint == Date)
{
j++;
a[j] = HashTable[i]->id;
}
}
if(j > -1)
{
cout << "Select: Plane id(s): ";
for(int i = 0; i <= j; i++)
{
cout << a[i] << ", ";
}
cout << "last maintained on " << Date << endl << endl;
}
}