-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.cpp
636 lines (540 loc) · 12.9 KB
/
1.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#include<iostream>
#include<string>
using namespace std;
//table display settings
constexpr int IDLENGTH = 10;
constexpr int NAMELENGTH = 12;
constexpr int SEXLENGTH = 8;
constexpr int AGELENGTH = 10;
constexpr int CATEGORYLENGTH = 20;
//operation code
//convenient for reading
constexpr int SEARCH = 1;
constexpr int INSERT = 2;
constexpr int ADD = 3;
constexpr int REVISE = 4;
constexpr int DEL = 5;
constexpr int DISPLAY = 6;
constexpr int CLEAR = 7;
constexpr int EXIT = 0;
class Candidate
{
private:
//associated information
int id;
string name;
string sex;
short int age;
string category;
public:
//operator overload
friend ostream& operator<<(ostream& output, const Candidate candidate)
{
output << candidate.id;
int n = IDLENGTH - to_string(candidate.id).size();
for (int i = 0; i < n; i++)
output << " ";
output << candidate.name;
n = NAMELENGTH - candidate.name.size();
for (int i = 0; i < n; i++)
output << " ";
output << candidate.sex;
n = SEXLENGTH - candidate.sex.size();
for (int i = 0; i < n; i++)
output << " ";
output << candidate.age;
n = AGELENGTH - to_string(candidate.age).size();
for (int i = 0; i < n; i++)
output << " ";
output << candidate.category;
n = CATEGORYLENGTH - candidate.category.size();
for (int i = 0; i < n; i++)
output << " ";
output << endl;
return output;
}
//constructor
Candidate():id(0),name(""),sex(""),age(0),category(""){}
Candidate(int id, string name, string sex, short int age, string category) :id(id), name(name), sex(sex), age(age), category(category) {}
//set the data
void Setid(int n) { id = n; }
void Setname(string data) { name = data; }
void Setsex(string data) { sex = data; }
void Setage(short int data) { age = data; }
void Setcategory(string data) { category = data; }
//get the data
int Getid() { return id; }
string Getname() { return name; }
string Getsex() { return sex; }
short int Getage() { return age; }
string Getcategory() { return category; }
};
struct LinkNode
{
Candidate candidate;
LinkNode* next;
//constructor
LinkNode(int id, string name, string sex, short int age, string category):candidate(id,name,sex,age,category),next(nullptr){}
LinkNode() :candidate(), next(nullptr) {}
};
class Exam_Registration_System // the same as list
{
private:
LinkNode* Candidate_Table_Tail;
LinkNode* Candidate_Table_Head;
int length;
public:
//constructor
Exam_Registration_System() : length(0)
{
Candidate_Table_Tail = nullptr;
Candidate_Table_Head = nullptr;
}
//destructor
~Exam_Registration_System();
//get the length
int Getlength(){ return length; }
//function
void create();
LinkNode* search_by_id(int key);
void insert(int order, int id, string name, string sex, short int age, string category);
void add(int id, string name, string sex, short int age, string category); // add from the end
void revise_by_id(int key, int key_id, string key_name, string key_sex, short int key_age, string key_category);
void del_by_id(int key);
void display();
bool empty() { return length == 0; }
void clear();
};
Exam_Registration_System::~Exam_Registration_System()
{
cout << "退出中..\n"
<< "欢迎下次再见子\n";
LinkNode* cursor = Candidate_Table_Head;
while (cursor != nullptr) //delete nodes by iteration
{
LinkNode* temp = cursor;
cursor = cursor->next;
delete temp;
}
}
void Exam_Registration_System::create()
{
int candidate_num = 0, id = 0;
short age = 0;
string name, sex, category;
cout << "现在开始建立考生信息系统\n"
<< "请输入考生人数:";
while (true)
{
cin >> candidate_num;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
cout << "请依次输入考生的考号、姓名、性别(男/女)、年龄及报考类别\n";
for (int i = 0; i < candidate_num; i++)
{
while (true)
{
cin >> id >> name >> sex >> age >> category;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
add(id, name, sex, age, category); //using add function
}
cout << "ok啦!初步已经建立完毕了呢!\n";
return;
}
LinkNode* Exam_Registration_System::search_by_id(int key)
{
if (empty()) // judge if the table is empty
{
cout << "报名表现在为空!先来添加一点数据吧\n";
return nullptr;
}
LinkNode* cursor = Candidate_Table_Head;
while (cursor != nullptr && cursor->candidate.Getid() != key)
{
cursor = cursor->next;
}
if (cursor == nullptr)
{
cout << "阿欧 但是真的没有找到呢\n";
return nullptr;
}
else
{
cout << "查到啦~~\n";
return cursor;
}
}
void Exam_Registration_System::insert(int order,int id, string name, string sex, short int age, string category)
{
// judge whether the command meet the request
if (empty())
{
// cout << "报名表现在为空!先来添加一点数据吧\n";
add(id, name, sex, age, category);
return;
}
else if (order < 1 || order > length)
{
cerr << "啊偶 插入的位置貌似不对呢 要不再看看?\n";
return;
}
//operate
if (order == length) //equal to add function
{
add(id, name, sex, age, category);
return;
}
int i = 1; // 初始位置为1
LinkNode* cursor = Candidate_Table_Head;
LinkNode* new_data = new LinkNode(id, name, sex, age, category);
if (order != 1)
{
while (i != order - 1) // 移动到之前一个
{
cursor = cursor->next;
i++;
}
LinkNode* temp = cursor->next;
cursor->next = new_data;
new_data->next = temp;
}
else
{
new_data->next = Candidate_Table_Head;
Candidate_Table_Head = new_data;
}
length++;
cout << "插入成功!\n";
}
void Exam_Registration_System::add(int id, string name, string sex, short int age, string category)
{
LinkNode* new_data = new LinkNode(id, name, sex, age, category);
if (empty())
{
Candidate_Table_Head = Candidate_Table_Tail = new_data;
}
else
{
Candidate_Table_Tail->next = new_data;
Candidate_Table_Tail = Candidate_Table_Tail->next;
}
length++;
}
void Exam_Registration_System::revise_by_id(int key, int key_id, string key_name, string key_sex, short int key_age, string key_category)
{
LinkNode* target = search_by_id(key);
if (target == nullptr)
{
cerr << "但是但是 没有找到这个考生呢 再试一试?\n";
return;
}
target->candidate.Setid(key_id);
target->candidate.Setname(key_name);
target->candidate.Setsex(key_sex);
target->candidate.Setage(key_age);
target->candidate.Setcategory(key_category);
cout << "修改成功!"
<< "现在的情况是:\n"
<< target->candidate;
return;
}
void Exam_Registration_System::del_by_id(int key)
{
if (empty())
{
cout << "报名表现在为空!先来添加一点数据吧\n";
return;
}
LinkNode* cursor = Candidate_Table_Head;
if (cursor->candidate.Getid() == key)
{
Candidate_Table_Head = Candidate_Table_Head->next;
if (length == 1)
Candidate_Table_Tail = Candidate_Table_Head;
delete cursor;
length--;
return;
}
else
{
while (cursor->next != nullptr && cursor->next->candidate.Getid() != key)
{
cursor = cursor->next;
}
if (cursor->next == nullptr)
{
cerr << "没有找到要删除的目标子!" << endl;
return;
}
}
LinkNode* temp = cursor->next;
cursor->next = temp->next;
delete temp;
length--;
if (cursor->next == nullptr)
Candidate_Table_Tail = cursor;
return;
}
void Exam_Registration_System::display()
{
if (empty()) // judge if the table is empty
{
cerr << "报名表现在为空!先来添加一点数据吧\n";
return;
}
string ID = "考号";
string NAME = "姓名";
string SEX = "性别";
string AGE = "年龄";
string CATEGORY = "报考类别";
cout << ID;
for (int i = 0; i < IDLENGTH - ID.size(); i++)
{
cout << " ";
}
cout << NAME;
for (int i = 0; i < NAMELENGTH - NAME.size(); i++)
{
cout << " ";
}
cout << SEX;
for (int i = 0; i < SEXLENGTH - SEX.size(); i++)
{
cout << " ";
}
cout << AGE;
for (int i = 0; i < AGELENGTH - AGE.size(); i++)
{
cout << " ";
}
cout << CATEGORY;
for (int i = 0; i < CATEGORYLENGTH - CATEGORY.size(); i++)
{
cout << " ";
}
cout << endl;
LinkNode* cursor = Candidate_Table_Head;
for (int i = 0; i < length; i++)
{
cout << cursor->candidate;
cursor = cursor->next;
}
}
//void Exam_Registration_System::print_candidate(LinkNode* node)
//{
// if (node == nullptr)
// return;
// else
// {
// print_candidate(node->next);
// cout << node->candidate;
// }
//}
void Exam_Registration_System::clear()
{
LinkNode* cursor = Candidate_Table_Head;
Candidate_Table_Head = Candidate_Table_Tail = nullptr;
while (cursor!=nullptr)
{
LinkNode* temp = cursor;
cursor = cursor->next;
delete temp;
}
length = 0;
}
int GetOrder() // get insertion order
{
int order;
while (true)
{
cout << "请输入位置子(最开始是1):";
cin >> order;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
return order;
}
int GetID() // get id
{
int id = -1;
while (true)
{
cout << "请输入考号子:";
cin >> id;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
return id;
}
int main()
{
Exam_Registration_System exam_registration_system;
exam_registration_system.create();
bool flag = false;
int operation_code;
while (true)
{
cout << "请输入指令来执行相应命令哦~\n"
<< "1 查找 2 插入 3 添加 4 修改 5 删除 6 显示 7 清空 0 退出\n"
<< "请输入操作码:";
while (true)
{
cin >> operation_code;
if (cin.fail())
{
cerr << "额 好像输入错误呢 重新输入试试?\n"
<< "请输入操作码:";
cin.clear();
cin.ignore(1024, '\n');
}
else
break;
}
switch (operation_code)
{
case SEARCH:
{
cout << "请输入考生的考号:\n";
int key;
cin >> key;
LinkNode* result = exam_registration_system.search_by_id(key);
if (result != nullptr)
{
cout << result->candidate;
}
break;
}
case INSERT:
{
int id = 0, order = GetOrder();
short age = 0;
string name, sex, category;
cout << "请输入考生的考号、姓名、性别(男/女)、年龄及报考类别\n";
cout << "PS 如果报名表内无数据 则无论数字如何都默认添加新数据\n";
while (true)
{
cin >> id >> name >> sex >> age >> category;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
exam_registration_system.insert(order, id, name, sex, age, category);
break;
}
case ADD:
{
int id = 0;
short age = 0;
string name, sex, category;
cout << "请输入考生的考号、姓名、性别(男/女)、年龄及报考类别\n";
while (true)
{
cin >> id >> name >> sex >> age >> category;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
exam_registration_system.add(id, name, sex, age, category);
break;
}
case REVISE:
{
int key_id = GetID();
int id = 0;
short age = 0;
string name, sex, category;
cout << "请输入考生的考号、姓名、性别(男/女)、年龄及报考类别\n";
while (true)
{
cin >> id >> name >> sex >> age >> category;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "\n输入有误!请重新输入子\n";
continue;
}
else
break;
}
exam_registration_system.revise_by_id(key_id, id, name, sex, age, category);
break;
}
case DEL:
{
exam_registration_system.del_by_id(GetID());
break;
}
case DISPLAY:
{
exam_registration_system.display();
break;
}
case CLEAR:
{
exam_registration_system.clear();
cout << "已经清空了呜呜\n";
break;
}
case EXIT:
{
flag = true;
break;
}
default:
{
cout << "额..我没有听懂呢 再发一遍?\n";
break;
}
}
if (flag)
{
cout << "山水有相逢 我们来日再见!\n";
break;
}
}
return 0;
}