-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnum_1.cpp
More file actions
243 lines (218 loc) · 5.17 KB
/
num_1.cpp
File metadata and controls
243 lines (218 loc) · 5.17 KB
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
#include <iostream>
#include <list>
#include <string>
using namespace std;
class student
{
public:
string name;
string sex;
string age;
string number;
string major;
friend istream& operator >>(istream& in, student& stu);
friend ostream& operator <<(ostream& out, const student& stu);
student(const string& stu_number, const string& stu_name, const string& stu_sex,
const string& stu_age, const string& stu_major)
{
name = stu_name;
sex = stu_sex;
age = stu_age;
number = stu_number;
major = stu_major;
};
student() {};
};
istream& operator >>(istream& in, student& stu)
{
in >> stu.number >> stu.name >> stu.sex >> stu.age >> stu.major;
return in;
}
ostream& operator <<(ostream& out, const student& stu)
{
//printf("%10s %10s %10s %10s %10s\n", "考号", "姓名", "性别", "年龄", "报考类别");
printf("%10s %10s %10s %10s %10s\n", stu.number.c_str(),stu.name.c_str(), stu.sex.c_str(), stu.age.c_str(), stu.major.c_str());
return out;
}
template<typename T>
class List_node
{
public:
T data;
List_node<T> *next;
string key;
List_node(List_node<T> *ptr = nullptr) { next = ptr; };
List_node(const T& item, const string& key_to, List_node<T> * ptr = nullptr)
{
data = item;
key = key_to;
next = ptr;
}
};
template<typename T>
class List : public List_node<T>
{
List_node<T> *ptr_first;
int size;
public:
template<typename T>
friend ostream& operator<< (ostream& out, List<T>& my_list);
List() :size(0)
{
ptr_first = new List_node<T>();
};
bool my_inserter(int position, const T& data,const string& key);
bool my_dalete(int position);
List_node<T>* my_search(const string& key);
int size_of()
{
return size;
};
};
template<typename T>
ostream& operator << (ostream& out, List<T>& my_list)
{
List_node<T>* ptr_start = my_list.ptr_first;
printf("%10s %10s %10s %10s %10s\n", "考号", "姓名", "性别", "年龄", "报考类别");
if (my_list.size_of())
{
ptr_start = ptr_start->next;
while (ptr_start)
{
out << ptr_start->data;
ptr_start = ptr_start->next;
}
}
return out;
}
template<typename T>
bool List<T>::my_dalete(int position)
{
List_node<T> *ptr_start = ptr_first;
if (position < 0 || position > size - 1)
return false;
for (int i = 0; i< position; i++)
{
ptr_start = ptr_start->next;
}
List_node<T> *ptr_delet = ptr_start->next;
ptr_start->next = ptr_delet->next;
delete ptr_delet;
size--;
return true;
}
template<typename T>
bool List<T>::my_inserter(int position, const T& data,const string& key_to)
{
List_node<T> *ptr_start = ptr_first;
if (position<0 || position>size)
return false;
for (int i = 0; i< position; i++)
{
ptr_start = ptr_start->next;
}
List_node<T> *ptr_new = new List_node<T>(data,key_to);
size++;
ptr_new->next = ptr_start->next;
ptr_start->next = ptr_new;
return true;
}
template<typename T>
List_node<T>* List<T>::my_search(const string& key)
{
List_node<T>* ptr_start = ptr_first;
while (ptr_start&&ptr_start->key!=key)
{
ptr_start = ptr_start->next;
}
return ptr_start;
}
int main()
{
int stu_number;
cout << "首先请建立考生信息系统!" << endl << "请输入考生人数: " << endl;
cin >> stu_number;
cout << "请依次输入考生的考号,姓名,性别,年龄及报考类别!" << endl;
List<student> list_stu;
for (int i = 0; i < stu_number; i++)
{
student tempt;
cin >> tempt;
// cout << tempt;
list_stu.my_inserter(list_stu.size_of(), tempt , tempt.number);
}
cout << list_stu;
int option = 0;
cout << "请选择您要进行的操作 (1为插入,2为删除,3为查找,4为修改,5为统计,0为取消操作"<< endl;
do
{
cout << "请选择您要进行的操作: " << endl;
cin >> option;
switch (option)
{
case 1:
{
int posi;
cout << "请输入你要插入的考生的位置: " << endl;
cin >> posi;
cout << "请依次输入要插入的考生的考号,姓名,性别,年龄及报考类别!" << endl;
student tempt;
cin >> tempt;
list_stu.my_inserter(posi - 1, tempt, tempt.number);
cout << list_stu;
}break;
case 2:
{
int posi;
cout << "请输入你要删除的考生的位置: " << endl;
cin >> posi;
list_stu.my_dalete(posi-1);
cout << list_stu;
}break;
case 3:
{
string posi;
cout << "请输入你要找的考生的考号: " << endl;
cin >> posi;
if (list_stu.my_search(posi))
{
cout << "你要找的考生信息是: " << endl;
cout << list_stu.my_search(posi)->data;
}
else
{
cout << "没有找到!" << endl;
}
}break;
case 4:
{
string posi;
cout << "请输入你要修改的考生的考号: " << endl;
cin >> posi;
if (list_stu.my_search(posi))
{
cout << "你要修改的考生信息是: " << endl;
cout << list_stu.my_search(posi)->data;
cout << "请依次输入要插入的考生的考号,姓名,性别,年龄及报考类别!" << endl;
student tempt;
cin >> tempt;
list_stu.my_search(posi)->data = tempt;
cout << list_stu;
}
else
{
cout << "没有找到!" << endl;
}
}break;
case 5:
{
cout << list_stu;
}break;
default:
break;
}
} while (option);
system("pause");
return 0;
}
// adding some comment here should not be a problem