-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW5.cpp
573 lines (498 loc) · 12.2 KB
/
HW5.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
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
/*
题目:基于链表的学生信息管理系统
要求:
(1)插入节点:输入学生信息后,生成节点并插入链表中;
(2)删除节点:根据给定的学生姓名或学号删除该学生节点;
(3)查找节点:根据给定的学生姓名或学号查找学生信息,并显示出来;
(4)查找并显示总成绩最高和最低的学生信息;
(5)统计链表中的学生人数;
(6)(时间允许时选做)对链表节点按总成绩从高到低排序。
注:
(1)每个学生信息包括:姓名、学号、性别、出生年月日和3门课的成绩;
(2)系统运行后,首先显示一个简易的菜单,基于菜单操作来完成上述功能;
(3)链表的功能需要编程实现。
*/
class student
{
friend class cList;
//private:
//friend class cList;
char name[20];
long int id;
char gender[7];
long int birthday;
double course1;
double course2;
double course3;
double sum;
student *next;
public:
student()
{
//cout << "You shall not pass1!!!\n";
//name = new char[20];
strcpy(name, "Gandalf");
id = 5;
//gender = new char[7];
strcpy(gender, "male");
birthday = 0;
course1 = 90;
course2 = 90;
course3 = 90;
sum = 270;
//cout << "You shall not pass2!!!\n";
}
student(char *nam, long int iden, char *gen, long int birth, double c1, double c2, double c3)
{
//name = new char[20];
strcpy(name, nam);
id = iden; //e.g. 201663034
//gender = new char[7];
strcpy(gender, gen);
birthday = birth; //e.g. 19980506, 19980127, 19980906
course1 = c1;
course2 = c2;
course3 = c3;
sum = c1 + c2 + c3;
}
~student()
{
//delete name;
//delete gender;
cout << "student destructor (ง •̀_•́)ง\n";
}
student * getNext()
{
return next;
}
void input()
{
cout << "name: ";
cin >> name;
cout << "\n";
cout << "id: ";
cin >> id;
cout << "\n";
cout << "gender: ";
cin >> gender;
cout << "\n";
cout << "birthday: ";
cin >> birthday;
cout << "\n";
cout << "course1: ";
cin >> course1;
cout << "\n";
cout << "course2: ";
cin >> course2;
cout << "\n";
cout << "course3: ";
cin >> course3;
cout << "\n";
sum = course1 + course2 + course3;
}
void print()
{
printf("Name: %s\n", name);
printf("ID: %ld\n", id);
printf("Gender: %s\n", gender);
printf("Birthday: %ld\n", birthday);
cout << "Course1 Grade: " << course1 << '\n';
cout << "Course2 Grade: " << course2 << '\n';
cout << "Course3 Grade: " << course3 << '\n';
cout << "Sum Grade: " << sum << '\n';
}
};
class cList
{
protected:
student *First;
student *Last;
public:
cList()
{
First = Last = new student;
}
~cList()
{
Clear();
//delete First;
cout << "cList destructor (●_●)\n";
}
cList &Insert(const student &x); //Insert a node into the header
cList &Append(const student &x); //append a node to the end
cList &Delete(); //Delete a node from the header
cList &Remove(); //Remove a node from the end
void searchByName(char namie[]);
void searchByID(long int idnum);
//student &highestScore();
//student &lowestScore();
void highestScore();
void lowestScore();
int total();
void rank();
void Clear();
};
cList &cList::Insert(const student &x)
{
student *ptr = First;
First = new student;
*First = x;
First -> next = ptr;
cout << "SUCCESSFULLY INSERTED!!\n";
return(*this);
}
cList &cList::Append(const student &x)
{
student *ptr = Last;
Last = new student;
*ptr = x;
ptr -> next = Last;
cout << "SUCCESSFULLY APPENDED!!\n";
return(*this);
}
cList &cList::Delete()
{
if (First != Last)
{
student *ptr = First -> next;
//delete First; ############################################11:57pm
First = ptr;
/*
*because all 'delete' happen in student objects
*you do not want to do it all over again
*/
}
cout << "SUCCESSFULLY DELETED!!\n";
return (*this);
}
cList &cList::Remove()
{
if (First == Last)
;
else if (First -> next == Last)
Delete();
else
{
student *now = First;
student *pre = First;
while (now -> next != Last)
{
pre = now;
now = now -> next;
}
//delete now;
/*
*because all 'delete' happen in student objects
*you do not want to do it all over again
*/
pre -> next = Last;
}
cout << "SUCCESSFULLY REMOVED!!\n";
return (*this);
}
void cList::searchByID(long int idnum)
{
cout << "start search by id:\n";
student *ptr = First;
while (ptr != Last)
{
if (ptr->id == idnum)
break;
ptr = ptr->next;
}
ptr -> print();
cout << "\n";
}
void cList::searchByName(char namie[])
{
cout << "start search by name:\n";
student *ptr = First;
while (ptr != Last)
{
if (strcmp(ptr -> name, namie) == 0)
{
ptr -> print();
cout << "\n";
}
ptr = ptr -> next;
}
/*
student *chosen = new student[count];
ptr = First;
int i = 0;
while (ptr != Last)
{
if (ptr -> name == namie)
ptr -> print();
ptr = ptr -> next;
}
*/
}
void cList::highestScore()
//student &cList::highestScore()
{
student *ptr = First;
student *crown;
double highest = First -> sum;
while (ptr != Last)
{
if (ptr -> sum > highest)
{
highest = ptr -> sum;
crown = ptr;
}
ptr = ptr -> next;
}
ptr = First;
while (ptr != Last)
{
if (ptr -> sum == highest) {
ptr -> print();
cout << '\n';
}
ptr = ptr -> next;
}
}
void cList::lowestScore()
//student &cList::lowestScore()
{
student *ptr = First;
student *dude;
double lowest = First -> sum;
while (ptr != Last)
{
if (ptr -> sum < lowest)
{
lowest = ptr -> sum;
dude = ptr;
}
ptr = ptr -> next;
}
ptr = First;
while (ptr != Last)
{
if (ptr -> sum ==lowest) {
ptr -> print();
cout << '\n';
}
ptr = ptr -> next;
}
//dude -> print();
//return (*dude);
}
int cList::total()
{
student *ptr = First;
int amount = 0;
while (ptr != Last)
{
amount++;
ptr = ptr -> next;
}
return amount;
}
void cList::Clear()
{
int count = total();
while (count != 0)
{
Delete();
count--;
}
}
student & creatANewStudent()
{
student *stu = new student;
stu -> input();
return (*stu);
}
void TheMenu(cList *ls)
{
cout << '\n';
cout << '\n';
cout << " (●'◡'●)ノ❤ (●'◡'●)ノ❤ (●'◡'●)ノ❤ (●'◡'●)ノ❤\n";
cout << " ☆☆☆ ☆☆☆ ☆☆☆ ☆☆☆\n";
cout << " ☆☆☆ ☆☆☆ ☆☆☆\n";
cout << " ☆☆☆ ☆☆☆\n";
cout << " ☆☆☆ \n";
/*
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ \n";
cout << " ☆ d(ŐдŐ๑)\n";
cout << '\n';
*/
cout << '\n';
cout << " STEP1: CREAT YOUR LIST\n";
cout << '\n';
cout << '\n';
cout << "You can type in following commands to create your unique student list:\n";
cout << '\n';
cout << '\n';
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << "1 --------- insert to the head \n";
cout << '\n';
cout << "2 --------- append to the end\n";
cout << '\n';
cout << "3 --------- delete from the head\n";
cout << '\n';
cout << "4 --------- remove from the end\n";
cout << '\n';
cout << "5 --------- next step\n";
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << '\n';
cout << '\n';
cout << '\n';
cout << '\n';
while (true)
{
int choice = 0;
cin >> choice;
if (choice == 1)
ls -> Insert(creatANewStudent());
else if (choice == 2)
ls -> Append(creatANewStudent());
else if (choice == 3)
if(ls -> total())
ls -> Delete();
else
cout << "there is no student in the list!!!\n";
else if (choice == 4)
if(ls -> total())
ls -> Remove();
else
cout << "there is no student in the list!!!\n";
else if (choice == 5)
break;
cout << '\n';
cout << '\n';
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << "1 --------- insert to the head \n";
cout << '\n';
cout << "2 --------- append to the end\n";
cout << '\n';
cout << "3 --------- delete from the head\n";
cout << '\n';
cout << "4 --------- remove from the end\n";
cout << '\n';
cout << "5 --------- next step\n";
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << '\n';
cout << '\n';
}
cout << '\n';
cout << '\n';
cout << " Yeah! Your list is created!\n";
cout << '\n';
cout << " (●'◡'●)ノ❤ (●'◡'●)ノ❤ (●'◡'●)ノ❤ (●'◡'●)ノ❤\n";
cout << '\n';
cout << '\n';
cout << " STEP2:CHECK YOUR LIST INFORMATION\n";
cout << '\n';
cout << '\n';
cout << "You can type in following commands to check your student list:\n";
cout << '\n';
cout << '\n';
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << "6 --------- search by name\n";
cout << '\n';
cout << "7 --------- search by id number\n";
cout << '\n';
cout << "8 --------- highest total grade student(s)\n";
cout << '\n';
cout << "9 --------- lowest total grade student(s)\n";
cout << '\n';
cout << "10 --------- total number of students\n";
cout << '\n';
cout << "11 --------- sort the list\n";
/*with the highest-grade student on the head,
the next highest-grade student right after ... and the lowest-grade student on the end.
Note that when multiple students have the same sum grade,
the algorithm will sort them in the order of id number.\n";
*/
cout << '\n';
cout << "12 --------- end this program\n";
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << '\n';
cout << '\n';
cout << '\n';
cout << '\n';
while (true)
{
int choice = 0;
cin >> choice;
if (choice == 6)
{
char mName[20];
cout << "name: ";
cin >> mName;
cout << "\n";
ls -> searchByName(mName);//char namie[]
}
else if (choice == 7)
{
long int mID;
cout << "id: ";
cin >> mID;
cout << "\n";
ls -> searchByID(mID);//long int idnum
}
else if (choice == 8)
ls -> highestScore();
else if (choice == 9)
ls -> lowestScore();
else if (choice == 10)
cout << '\n' << "there are " << ls -> total() << " student(s) in total!\n";
else if (choice == 11)
cout<< '\n' << "sorry, this function is not available now.\n";//ls -> rank();
else if (choice == 12)
break;
cout << '\n';
cout << '\n';
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << "6 --------- search by name\n";
cout << '\n';
cout << "7 --------- search by id number\n";
cout << '\n';
cout << "8 --------- highest total grade student(s)\n"; cout << '\n';
cout << "9 --------- lowest total grade student(s)\n";
cout << '\n';
cout << "10 --------- total number of students\n";
cout << '\n';
cout << "11 --------- sort the list\n";
cout << '\n';
cout << "12 --------- end this program\n";
cout << "# # # # # # # # # # # # # # # # # # \n";
cout << '\n';
cout << '\n';
}
cout << '\n';
cout << '\n';
cout << "THE END. THANKS FOR USING!\n";
cout << '\n';
cout << "(*^ω^*) \n";
cout << '\n';
cout << '\n';
}
int main()
{
cList *csjpnz = new cList;
TheMenu(csjpnz);
return 0;
}