-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
338 lines (306 loc) · 8.58 KB
/
Copy pathMain.cpp
File metadata and controls
338 lines (306 loc) · 8.58 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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct StudentInfo
{
string name;
string number;
int score[3];
};
void Show();
void Exit();
void WriteInTheSystem();
void ShowScore();
void DeleteScore();
bool compareByScores(const StudentInfo& a, const StudentInfo& b);
void SortScore();
int main(){
while(1)
{
Show();
char numMain;
cin>>numMain;
switch (numMain)
{
case '1':
WriteInTheSystem();
break;
case '2':
DeleteScore();
break;
case '3':
ShowScore();
break;
case '4':
SortScore();
break;
case '5':
Exit();
break;
}
}
}
void Show()
{
cout<<" 欢迎来到学生管理系统! "<<endl;
cout<<" 请输入需要的操作 "<<endl;
cout<<endl;
cout<<"***-----------学生成绩管理系统--------------***"<<endl;
cout<<" (1) 成绩录入 (2) 成绩删除 "<<endl;
cout<<" (3) 成绩查询 (4) 成绩排序 "<<endl;
cout<<" (5) 退出系统 "<<endl;
cout<<"***-----------------------------------------***"<<endl;
}
void Exit()
{
system("cls");
cout<<"欢迎再次使用!"<<endl;
exit(0);
}
void WriteInTheSystem()
{
system("cls");
cout<<"***-----------学生成绩管理系统--------------***"<<endl;
cout<<" 成绩录入 "<<endl;
fstream f;
f.open("ScoreTable.txt",ios::in | ios::out |ios::app);
if (f)
{
cout<<"请输入要录入的学生数量:"<<endl;
int num;
cin>>num;
StudentInfo student[num];
cout<<"请按照学生姓名 - 学号 - 语文 - 数学 - 英语的顺序输入"<<endl;
for (int i = 0;i<num;i++)
{
cin>>student[i].name>>student[i].number;
for (int j = 0;j<3;j++)
{
cin>>student[i].score[j];
}
}
for (int i = 0;i<num;i++)
{
f<<student[i].name<<","<<student[i].number<<",";
for (int j = 0;j<2;j++)
{
f<<student[i].score[j]<<",";
}
f<<student[i].score[2]<<";";
}
cout<<"写入完成!"<<endl;
}
else cout<<"文件打开失败,请检查路径"<<endl;
system("pause");
system("cls");
}
void ShowScore()
{
system("cls");
cout << "***-----------学生成绩管理系统--------------***" << endl;
cout << " 成绩查询 " << endl;
cout << " 请选择查询方式 " << endl;
cout << " (1) 姓名查询 (2) 学号查询 " << endl;
cout << " (3) 查询所有信息 " << endl;
char lookup;
cin >> lookup;
fstream f("ScoreTable.txt", ios::in);
if (!f) {
cout << "文件打开失败,请检查路径" << endl;
return;
}
if (lookup == '1')
{
cout << "请输入你要查询的学生姓名:" << endl;
string TargetName;
cin >> TargetName;
string line;
bool found = false;
while (getline(f, line, ';')) {
stringstream ss(line);
string name, number, score1, score2, score3;
getline(ss, name, ',');
getline(ss, number, ',');
getline(ss, score1, ',');
getline(ss, score2, ',');
getline(ss, score3, ',');
if (name == TargetName) {
cout << "成功找到:" << endl;
cout << "姓名:" << setw(5) << name
<< " 学号:" << setw(5) << number
<< " 语文:" << setw(5) << score1
<< " 数学:" << setw(5) << score2
<< " 英语:" << setw(5) << score3
<< endl;
found = true;
break;
}
}
if (!found) {
cout << "没有找到 " << TargetName << endl;
}
system("pause");
system("cls");
}
else if (lookup == '2')
{
cout << "请输入你要查询的学生学号:" << endl;
string TargetNum;
cin >> TargetNum;
string line;
bool found = false;
while (getline(f, line, ';')) {
stringstream ss(line);
string name, number, score1, score2, score3;
getline(ss, name, ',');
getline(ss, number, ',');
getline(ss, score1, ',');
getline(ss, score2, ',');
getline(ss, score3, ',');
if (number == TargetNum) {
cout << "成功找到:" << TargetNum << endl;
cout << "姓名:" << setw(5) << name
<< " 学号:" << setw(5) << number
<< " 语文:" << setw(5) << score1
<< " 数学:" << setw(5) << score2
<< " 英语:" << setw(5) << score3
<< endl;
found = true;
break;
}
}
if (!found) {
cout << "没有找到学号为 " << TargetNum << " 的学生" << endl;
}
system("pause");
system("cls");
}
else if (lookup == '3')
{
cout << "全部学生成绩如下:" << endl;
string line;
int count = 0;
while (getline(f, line, ';')) {
stringstream ss(line);
string name, number, s1, s2, s3;
getline(ss, name, ',');
getline(ss, number, ',');
getline(ss, s1, ','); getline(ss, s2, ','); getline(ss, s3, ',');
cout << left << setw(3) << ++count << " 姓名:" << setw(10) << name
<< "学号:" << setw(10) << number
<< "语文:" << setw(6) << s1
<< "数学:" << setw(6) << s2
<< "英语:" << setw(6) << s3 << endl;
}
system("pause");
system("cls");
}
f.close();
}
void DeleteScore()
{
system("cls");
cout << "***-----------学生成绩管理系统--------------***" << endl;
cout << " 成绩删除 " << endl;
cout << "请选择删除方式:" << endl;
cout << " (1) 按姓名删除 (2) 按学号删除 " << endl;
char choice;
cin >> choice;
fstream f("ScoreTable.txt", ios::in);
if (!f) {
cout << "文件打开失败!" << endl;
return;
}
vector<string> records;
string line;
while (getline(f, line, ';')) {
records.push_back(line);
}
f.close();
string target;
if (choice == '1') {
cout << "请输入要删除的学生姓名:" << endl;
cin >> target;
} else if (choice == '2') {
cout << "请输入要删除的学生学号:" << endl;
cin >> target;
} else {
cout << "输入无效" << endl;
return;
}
bool deleted = false;
vector<string> updated;
for (const string& record : records) {
stringstream ss(record);
string name, number, s1, s2, s3;
getline(ss, name, ',');
getline(ss, number, ',');
getline(ss, s1, ','); getline(ss, s2, ','); getline(ss, s3, ',');
if ((choice == '1' && name == target) || (choice == '2' && number == target)) {
deleted = true; // 找到就跳过
} else {
updated.push_back(record);
}
}
ofstream out("ScoreTable.txt", ios::trunc); // 清空文件再写入
for (const string& r : updated) {
out << r << ";";
}
out.close();
if (deleted)
cout << "删除成功!" << endl;
else
cout << "未找到该学生信息。" << endl;
system("pause");
system("cls");
}
void SortScore() {
system("cls");
cout << "***-----------学生成绩管理系统--------------***" << endl;
cout << " 成绩排序 " << endl;
ifstream f("ScoreTable.txt");
if (!f) {
cout << "文件打开失败!" << endl;
return;
}
vector<StudentInfo> students;
string line;
while (getline(f, line, ';')) {
stringstream ss(line);
StudentInfo s;
string s1, s2, s3;
getline(ss, s.name, ',');
getline(ss, s.number, ',');
getline(ss, s1, ',');
getline(ss, s2, ',');
getline(ss, s3, ',');
s.score[0] = stoi(s1);
s.score[1] = stoi(s2);
s.score[2] = stoi(s3);
students.push_back(s);
}
f.close();
sort(students.begin(), students.end(), compareByScores);
cout << "按成绩排序结果如下:" << endl;
int i = 1;
for (const auto& s : students) {
cout << left << setw(3) << i++
<< "姓名:" << setw(10) << s.name
<< "学号:" << setw(10) << s.number
<< "语文:" << setw(6) << s.score[0]
<< "数学:" << setw(6) << s.score[1]
<< "英语:" << setw(6) << s.score[2] << endl;
}
system("pause");
system("cls");
}
bool compareByScores(const StudentInfo& a, const StudentInfo& b) {
if (a.score[0] != b.score[0]) return a.score[0] > b.score[0];
if (a.score[1] != b.score[1]) return a.score[1] > b.score[1];
return a.score[2] > b.score[2];
}