-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimKiem.cpp
340 lines (304 loc) · 6.75 KB
/
TimKiem.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "SapXep.cpp"
#include "Tree.h"
#include <vector>
int SIZE_BANG[7] = {0, 6, 22, 40, 74, 92, 104};
struct Node
{
SinhVien data;
Node *next;
};
TreeNode *newTreeNode(SinhVien sv)
{
TreeNode *temp = new TreeNode;
temp->data = sv;
temp->left = temp->right = NULL;
return temp;
}
//===================================================
void timKiemTT(vector<SinhVien>, int, int);
void timKiemNP(vector<SinhVien>, int, int);
//==================================================
void khongTimThay();
void rawTextDemo(int x, int y, int size[], string data[]);
void rawTextColor(int x, int y, int size[], string data, int vt, string key, int type);
void rawSV_mau(int x, int y, int size[], SinhVien data, int vt, int loai, string key);
bool checkSinhVien(SinhVien data, int type, string s);
void printSearch(vector<SinhVien> ds2, int count, int type, string s);
void printSearch(Node *t, int count, int type, string s);
string low2(string); // chuyen string thanh chu thuong nhung k lam thay doi string do
//==================================================
Node *newNode(SinhVien sv);
TreeNode *newTreeNode(SinhVien sv);
void add(SinhVien sv, Node *t);
TreeNode *Insert(TreeNode *t, SinhVien data, int type);
//=================TIM KIEM TUAN TU==================
// ham tim kiem tuan tu theo type (done)
void timKiemTT(vector<SinhVien> ds, int n, int type)
{
clrscr(8);
TextColor(6);
gotoXY(30, 10);
cout << "Nhap tu khoa can tim: ";
string s;
int dem = 0;
if (type == 2)
{
fflush(stdin);
getline(cin, s);
}
else
{
cin >> s;
}
vector<SinhVien> ds2;
for (int i = 0; i < n; i++)
{
if (checkSinhVien(ds[i], type, s) == 1)
{
ds2.push_back(ds[i]);
++dem;
}
}
if (dem == 0)
{
khongTimThay();
}
else
{
printSearch(ds2, dem, type, s);
}
}
//==================TIM KIEM NHI PHAN=================
// ham tim kiem nhi phan theo type
void timKiemNP(vector<SinhVien> ds, int n, int type)
{
clrscr(8);
TextColor(6);
gotoXY(30, 10);
cout << "Nhap tu khoa can tim: ";
string keyword;
if (type == 2)
{
fflush(stdin);
getline(cin, keyword);
}
else
{
cin >> keyword;
}
// cout << keyword;
// Cần xử lý keyword tìm kiếm với ngày sinh
// Cách xử lý của tao là tạo một đối tượng mới chỉ
// có thuộc tính cần so sánh
// rồi so sánh để tìm đối tượng đó
TreeNode *svtemp;
switch (type)
{
case 0:
{
SinhVien a(keyword, "", "", "", 0);
svtemp = newTreeNode(a);
break;
}
case 1:
{
SinhVien a("", keyword, "", "", 0);
svtemp = newTreeNode(a);
break;
}
case 2:
{ // tim theo ten
SinhVien a("", "", keyword, "", 0);
svtemp = newTreeNode(a);
break;
}
case 3:
{ // tim theo ngay sinh (xu ly ngay sinh)
SinhVien a("", "", "", keyword, 0);
svtemp = newTreeNode(a);
break;
}
case 4:
{ // tim theo diemtb (can xu ly tiep)
SinhVien a(keyword, "", "", "", 0);
svtemp = newTreeNode(a);
break;
}
default:
break;
}
BSTtree T;
T.readTREE(ds);
// T.PreOrder(T.root);
SinhVien k;
k = T.SearchTree(T.root, svtemp, keyword, type);
// root : tree ban đầu , svtemp là đối tượng chứa thuộc tính cần so sánh, s : key word
cout << k;
}
//====================CAC HAM HO TRO TIM KIEM===============
// ham hien thi khi khong tim thay theo yeu cau
void khongTimThay()
{
clrscr(8);
gotoXY(35, 10);
TextColor(4);
cout << "Khong tim thay sinh vien theo yeu cau!";
}
// ham in chu o giua khung
void rawTextDemo(int x, int y, int size[], string data[])
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 15);
for (int i = 0; i < 6; i++)
{
int length = (size[i + 1] - size[i] + 1);
gotoXY(x + size[i] + length / 2 - data[i].length() / 2, y + 1);
cout << data[i];
}
}
// ham in mau chu tim kiem
void rawTextColor(int x, int y, int size[], string data, int vt, string key, int type)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 96);
low(key);
int length = (size[vt + 1] - size[vt] + 1);
int X = x + size[vt] + length / 2 - data.length() / 2;
int Y = y + 1;
size_t found = (low2(data)).find(key);
while (found != string::npos)
{
string KQ = data.substr(found, key.length());
gotoXY(X + found, Y);
cout << KQ;
found = (data).find(key, found + 1);
}
SetConsoleTextAttribute(hConsole, 15);
}
// in mau cho string tim thay trong sinh vien sd vector
void rawSV_mau(int x, int y, int size[], SinhVien data, int vt, int loai, string key)
{
low(key);
string bang[6];
bang[0] = coverIntToString(vt + 1, 3);
bang[1] = data.maLop;
bang[2] = data.maSV;
bang[3] = data.hoTen;
bang[4] = data.ngaySinh;
bang[5] = floatToString(data.diemTB);
rawTextDemo(x, y, size, bang);
rawTextColor(x, y, size, bang[loai + 1], loai + 1, key, loai);
// y += vt*2;
}
// kiem tra sinh vien co tim thay du lieu string s can tim hay khong
bool checkSinhVien(SinhVien data, int type, string s)
{
low(s);
switch (type)
{
case 0:
{
// ma lop
low(data.maLop);
size_t found = data.maLop.find(s);
if (found == string::npos)
return 0;
return 1;
break;
}
case 1:
{
// ma sinh vien
low(data.maSV);
size_t found = data.maSV.find(s);
if (found == string::npos)
return 0;
return 1;
break;
}
case 2:
{
// hoten
low(data.hoTen);
size_t found = data.hoTen.find(s);
if (found == string::npos)
return 0;
return 1;
break;
}
case 3:
{
// ngay sinh
size_t found = data.ngaySinh.find(s);
if (found == string::npos)
return 0;
return 1;
break;
}
case 4:
{
string x = floatToString(data.diemTB);
size_t found = x.find(s);
if (found == string::npos)
return 0;
return 1;
break;
}
}
}
// in ds tim kiem sd vector
void printSearch(vector<SinhVien> ds2, int count, int type, string s)
{
clrscr(8);
gotoXY(45, 10);
TextColor(6);
cout << "DANH SACH SINH VIEN TIM KIEM";
TextColor(7);
khungDS(8, 12, 2, count);
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
for (int i = 0; i < count; i++)
{
rawSV_mau(8, 14 + i * 2, SIZE_BANG, ds2[i], i, type, s);
}
}
// in ds tim kiem sd cau truc cay nhi phan
void printSearch(Node *t, int count, int type, string s)
{
if (t == NULL)
{
khongTimThay();
return;
}
clrscr(8);
gotoXY(45, 10);
TextColor(6);
cout << "DANH SACH SINH VIEN TIM KIEM";
TextColor(7);
khungDS(8, 12, 2, count);
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 14);
Node *temp = t;
if (temp != NULL)
{
for (int i = 0; i < count; i++)
{
rawSV_mau(8, 14 + i * 2, SIZE_BANG, temp->data, i, type, s);
temp = temp->next;
}
}
}
// ham chuyen cac chu cai thanh in thuong, tra ve string
string low2(string x)
{
for (int i = 0; i < x.length(); i++)
{
if (x[i] >= 'A' && x[i] <= 'Z')
x[i] = x[i] + 32;
}
return x;
}