-
Notifications
You must be signed in to change notification settings - Fork 0
/
newInfoManage.cc
127 lines (110 loc) · 2.78 KB
/
newInfoManage.cc
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
#include <array>
#include <iomanip>
#include <iostream>
using namespace std;
struct Student {
int id{};
string name{};
array<double, 3> score{};
double average{};
friend ostream &operator<<(ostream &os, const Student &student) {
os << student.id << " " << student.name << " " << fixed << setprecision(2)
<< student.score[0] << " " << student.score[1] << " " << student.score[2]
<< " " << student.average;
return os;
}
};
struct Node {
Student student{};
Node *next{};
};
class List {
public:
using Link = Node *;
List() : first{new Node} {}
List(const List &src) : first{new Node} { copyFrom(src); }
List &operator=(const List &rhs) {
if (this != &rhs) {
clearStudent();
copyFrom(rhs);
}
return *this;
}
~List() {
clearStudent();
delete first;
}
bool empty() const { return (first->next == nullptr); }
void decreOrderInsert(const Student &student) {
Link current_front = first;
// first
if (current_front->next == nullptr) {
Link newNode = new Node;
newNode->student = student;
newNode->next = nullptr;
first->next = newNode;
return;
}
// middle
while ((current_front->next) != nullptr) {
if (student.average >= (current_front->next->student).average) {
Link current = current_front->next;
Link newNode = new Node;
newNode->student = student;
current_front->next = newNode;
newNode->next = current;
return;
}
current_front = current_front->next;
}
// last
Link newNode = new Node;
newNode->student = student;
newNode->next = nullptr;
current_front->next = newNode;
}
friend ostream &operator<<(ostream &os, const List &list) {
Link temp = list.first;
int i = 1;
while ((temp = temp->next) != nullptr) {
os << temp->student << " " << i++ << '\n';
}
return os;
}
protected:
void clearStudent() {
while (first->next != nullptr) {
Link temp = first->next;
first->next = temp->next;
delete temp;
}
}
void copyFrom(const List &src) {
Link srcTemp = src.first;
Link desTemp = first;
while ((srcTemp = srcTemp->next) != nullptr) {
desTemp->next = new Node;
desTemp = desTemp->next;
desTemp->student = srcTemp->student;
}
desTemp->next = nullptr;
}
private:
Link first{};
};
int main() {
int numStudents{};
cin >> numStudents;
List students{};
for (int i = 0; i < numStudents; i++) {
Student student{};
cin >> student.id >> student.name >> student.score[0] >> student.score[1] >>
student.score[2];
for (const auto &score : student.score) {
student.average += score;
}
student.average /= 3;
students.decreOrderInsert(student);
}
cout << students;
}