-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListCTKH.cpp
384 lines (375 loc) · 10.1 KB
/
ListCTKH.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include"ListCTKH.h"
template<class T>
ListCTKH<T>::ListCTKH()
{
this->data=nullptr;
this->soLuongCTKH=0;
this->tenTacGia="";
}
template<class T>
ListCTKH<T>::ListCTKH(string _tenTacGia)
{
this->soLuongCTKH=0;
this->tenTacGia=_tenTacGia;
this->data= nullptr;
}
template<class T>
ListCTKH<T>::~ListCTKH()
{
delete[] this->data;
}
template<class T>
const CongTrinhKhoaHoc ListCTKH<T>::operator[](const int index)
{
if(index <0 || index >= this->soLuongCTKH)
{
throw "invalid index!";
}
return this->data+index;
}
template<class T>
void ListCTKH<T>::operator=(const ListCTKH& l)
{
this->soLuongCTKH =l.soLuongCTKH;
this->tenTacGia = l.tenTacGia;
this->data= new T*[this->soLuongCTKH];
for(int i=0;i<l.soLuongCTKH;++i)
{
this->data[i] = l.data[i];
}
}
template<class T>
void ListCTKH<T>::Add(T *ct)
{
if(this->soLuongCTKH ==0)
{
this->data = new T*[this->soLuongCTKH+1];
this->data[this->soLuongCTKH] = ct;
}
else
{
T **temp =new T*[this->soLuongCTKH];
for(int i =0; i<this->soLuongCTKH;++i)
{
// temp[i] = this->data[i];
*(temp +i) = *(this->data+i);
}
this->data= new T*[this->soLuongCTKH+1];
for(int i=0;i<this->soLuongCTKH;++i)
{
*(this->data+ i) = *(temp +i);
}
*(this->data + this->soLuongCTKH)= ct;
delete[] temp;
}
this->soLuongCTKH++;
}
template<class T>
void ListCTKH<T>::Input()
{
int loai=0;
T *ct;
while(loai<1 || loai > 2)
{
while(cout<<"Loai cong trinh: (1): bai bao (2): sach: " && !(cin>> loai))
{
cin.clear();
cin.ignore();
cout<<"Dau vao sai moi nhap lai!";
}
}
if(loai == 1)
{
ct = new BaiBao();
ct->Nhap();
this->Add(ct);
}
if(loai ==2)
{
ct = new Sach();
ct->Nhap();
this->Add(ct);
}
}
template<class T>
void ListCTKH<T>::Remove()
{
int index;
while(cout<<"Nhap vi tri can xoa: " && !(cin>> index))
{
cin.clear();
cin.ignore();
cout<<"Dau vao khong hop le moi nhap lai!"<<endl;
}
if(index >= 0 && index < this->soLuongCTKH)
{
T **temp= new T*[this->soLuongCTKH];
for(int i=0;i<this->soLuongCTKH;++i)
{
temp[i]=this->data[i];
}
this->data= new T*[this->soLuongCTKH-1];
for(int i=0;i<this->soLuongCTKH;++i)
{
this->data[i] = temp[i];
}
for(int i=index; i<this->soLuongCTKH;++i)
{
this->data[i] = temp[i+1];
}
cout<<"Da xoa cong trinh khoa hoc co ma: "<<index<<endl;
delete[] temp;
this->soLuongCTKH--;
}
else
{
cout<<"Dau vao khong hop le!"<<endl;
}
}
template<class T>
void ListCTKH<T>::ShowList()
{
for(int i=0; i<this->soLuongCTKH;++i)
{
this->data[i]->Show();
cout<<"------------------------------------------"<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"------------------------------------------"<<endl;
}
}
template<class T>
int ListCTKH<T>::Find()
{
string _ma;
cout<<"Nhap ma cong trinh can tim: ";
fflush(stdin);
getline(cin, _ma);
for(int i=0;i<this->soLuongCTKH;++i)
{
if(this->data[i]->getMaCongTrinh() == _ma)
{
cout<<"\nVi tri cua cong trinh: "<<i;
cout<<"\n";
return i;
}
}
cout<<"Khong tim thay doi tuong tren moi kiem tra lai!"<<endl;
return this->Find();
}
template<class T>
void ListCTKH<T>::Update()
{
int index = this->Find();
this->data[index]->Show();
cout<<"\nNhap truong can thay doi: \n"<<endl;
if(BaiBao* bb = dynamic_cast<BaiBao*>(data[index]))
{
bool dung= false;
int chon=-1;
while (dung ==false)
{
cout<<endl;
cout<<"** 1.Ma bai bao. **\n";
cout<<"** 2.Ten bai bao. **\n";
cout<<"** 3.So luong tac gia. **\n";
cout<<"** 4.Nam xuat ban. **\n";
cout<<"** 5.Khu vuc xuat ban. **\n";
cout<<"** 6.Tat ca. **\n";
cout<<"** 0.Dung update **\n";
while(cout<<"nhap lua chon: " && !(cin>>chon))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
cout<<"\n";
switch (chon)
{
case 1:
{
string mact;
cout<<"Nhap ma cong trinh";
cin.ignore();
getline(cin,mact);
this->data[index]->setMaCongTrinh(mact);
this->data[index]->Show();
cout<<endl;
break;
}
case 2:
{
string tenct;
cout<<"Nhap ten cong trinh";
cin.ignore();
getline(cin, tenct);
static_cast<BaiBao*>(this->data[index])->setTenCongTrinh(tenct);
this->data[index]->Show();
cout<<endl;
break;
}
case 3:
{
int sotg;
while(cout<<"nhap so tac gia: " && !(cin>> sotg))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
static_cast<BaiBao*>(this->data[index])->setSoTacGia(sotg);
this->data[index]->Show();
cout<<endl;
break;
}
case 4:
{
int namxb=1900;
while(namxb < 1990)
{
while(cout<<"\nnhap nam xuat ban: "&& !(cin>>namxb))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
}
this->data[index]->setNamXuatBan(namxb);
this->data[index]->Show();
cout<<endl;
break;
}
case 5:
{
bool kvxb=-1;
while(kvxb<0 || kvxb >1)
{
while(cout<<"Nhap khu vuc xuat ban: (0) trong nuoc (1) quoc te: " && !(cin>>kvxb))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
}
this->data[index]->setKhuVucXuatBan(kvxb);
this->data[index]->Show();
cout<<endl;
break;
}
case 6:
{
this->data[index]->Nhap();
this->data[index]->Show();
break;
}
case 0:
{
cout<<"Hoan thanh update!"<<endl;
dung =true;
break;
}
default:
{
cout<<"Nhap sai moi nhap lai";
break;
}
}
}
}
else
{
bool dung2 = false;
int chon2=-1;
while (dung2 == false)
{
cout<<endl;
cout<<"** 1.Ma sach. **\n";
cout<<"** 2.Loai sach. **\n";
cout<<"** 3.Nam xuat ban. **\n";
cout<<"** 4.Khu vuc xuat ban. **\n";
cout<<"** 5.Tat ca. **\n";
cout<<"** 0.Dung update **\n";
while(cout<<"nhap lua chon: " && !(cin>>chon2))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
switch (chon2)
{
case 1:
{
string mact;
cout<<"Nhap ma cong trinh";
cin.ignore();
getline(cin,mact);
this->data[index]->setMaCongTrinh(mact);
this->data[index]->Show();
cout<<endl;
break;
}
case 2:
{
string loaisach;
cout<<"Nhap loai sach";
cin.ignore();
getline(cin, loaisach);
static_cast<Sach*>(this->data[index])->setLoaiSach(loaisach);
this->data[index]->Show();
cout<<endl;
break;
}
case 3:
{
int namxb=1900;
while(namxb < 1990)
{
while(cout<<"\nnhap nam xuat ban: "&& !(cin>>namxb))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
}
this->data[index]->setNamXuatBan(namxb);
this->data[index]->Show();
cout<<endl;
break;
}
case 4:
{
bool kvxb=-1;
while(kvxb<0 || kvxb >1)
{
while(cout<<"Nhap khu vuc xuat ban: (0) trong nuoc (1) quoc te: " && !(cin>>kvxb))
{
cin.clear();
cin.ignore();
cout<<"dau vao khong hop le!";
}
}
this->data[index]->setKhuVucXuatBan(kvxb);
this->data[index]->Show();
cout<<endl;
break;
}
case 5:
{
this->data[index]->Nhap();
this->data[index]->Show();
break;
}
case 0:
{
cout<<"Hoan thanh update!"<<endl;
dung2=true;
break;
}
default:
{
cout<<"Nhap sai moi nhap lai";
break;
}
}
}
}
}