-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.cpp
More file actions
260 lines (251 loc) · 6.21 KB
/
Copy pathManager.cpp
File metadata and controls
260 lines (251 loc) · 6.21 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
#include "Manager.h"
#include <iostream>
#include <fstream>
#include <cstdio>
#include "InputHelper.h"
using namespace std;
Manager::Manager()
{
}
Manager::~Manager()
{
}
void Manager::menu()
{
int choice;
while (true)
{
cout << "请输入需要执行的操作:" << endl;
cout << "1.添加教师" << endl;
cout << "2.添加学生" << endl;
cout << "3.显示教师" << endl;
cout << "4.显示学生" << endl;
cout << "5.删除教师" << endl;
cout << "6.删除学生" << endl;
cout << "7.退出" << endl;
readValid(choice, "无效选项,请重新输入:");
switch (choice)
{
case 1:
addTeacher();
break;
case 2:
addStudent();
break;
case 3:
showTeacher();
break;
case 4:
showStudent();
break;
case 5:
deleteTeacher();
break;
case 6:
deleteStudent();
break;
case 7:
return;
default:
cout << "无效的选择" << endl;
}
}
}
void Manager::addTeacher()
{
string name, id, password;
cout << "请输入教师姓名:" << endl;
readValid(name);
cout << "请输入教师编号:" << endl;
readValid(id);
// 查重
ifstream check("data/teacher.txt");
if (check.is_open()) {
string tname, tid, tpwd;
while (check >> tname >> tid >> tpwd) {
if (tid == id) {
cout << "该教师编号已存在,添加失败" << endl;
return;
}
}
check.close();
}
cout << "请输入教师密码:" << endl;
readValid(password);
ofstream out("data/teacher.txt", ios::app);
if (!out.is_open())
{
cout << "文件打开失败" << endl;
return;
}
out << name << " " << id << " " << password << endl;
out.close();
cout << "教师添加成功" << endl;
}
void Manager::addStudent()
{
string name, id, password;
cout << "请输入学生姓名:" << endl;
readValid(name);
cout << "请输入学生学号:" << endl;
readValid(id);
// 查重
ifstream check("data/students.txt");
if (check.is_open()) {
string sname, sid, spwd;
while (check >> sname >> sid >> spwd) {
if (sid == id) {
cout << "该学号已存在,添加失败" << endl;
return;
}
}
check.close();
}
cout << "请输入学生密码:" << endl;
readValid(password);
ofstream out("data/students.txt", ios::app);
if (!out.is_open())
{
cout << "文件打开失败" << endl;
return;
}
out << name << " " << id << " " << password << endl;
out.close();
cout << "学生添加成功" << endl;
}
void Manager::showTeacher()
{
ifstream in("data/teacher.txt");
if (!in.is_open())
{
cout << "文件打开失败" << endl;
return;
}
string name, id, password;
while (in >> name >> id >> password)
{
cout << "教师姓名:" << name << " 教师编号:" << id << " 教师密码:" << password << endl;
}
in.close();
}
void Manager::showStudent()
{
ifstream in("data/students.txt");
if (!in.is_open())
{
cout << "文件打开失败" << endl;
return;
}
string name, id, password;
while (in >> name >> id >> password)
{
cout << "学生姓名:" << name << " 学生学号:" << id << " 学生密码:" << password << endl;
}
in.close();
}
void Manager::deleteTeacher()
{
string id;
cout << "请输入要删除的教师编号:" << endl;
readValid(id);
ifstream in("data/teacher.txt");
if (!in.is_open())
{
cout << "文件打开失败" << endl;
return;
}
ofstream out("data/temp.txt");
if (!out.is_open())
{
cout << "文件打开失败" << endl;
return;
}
string name, tid, password;
bool found = false;
while (in >> name >> tid >> password)
{
if (tid == id)
{
found = true;
continue;
}
out << name << " " << tid << " " << password << endl;
}
in.close();
out.close();
if (found)
{
remove("data/teacher.txt");
rename("data/temp.txt", "data/teacher.txt");
cout << "教师删除成功" << endl;
}
else
{
remove("data/temp.txt");
cout << "未找到该教师编号" << endl;
}
}
void Manager::deleteStudent()
{
string id;
cout << "请输入要删除的学生学号:" << endl;
readValid(id);
ifstream in("data/students.txt");
if (!in.is_open())
{
cout << "文件打开失败" << endl;
return;
}
ofstream out("data/temp.txt");
if (!out.is_open())
{
cout << "文件打开失败" << endl;
return;
}
string name, sid, password;
bool found = false;
while (in >> name >> sid >> password)
{
if (sid == id)
{
found = true;
continue;
}
out << name << " " << sid << " " << password << endl;
}
in.close();
out.close();
if (found)
{
remove("data/students.txt");
rename("data/temp.txt", "data/students.txt");
// 同时删除该学生的成绩文件和课程文件(孤儿文件清理)
string gradePath = "data/" + id + "_Grade.txt";
string coursePath = "data/" + id + "_Courses.txt";
remove(gradePath.c_str());
remove(coursePath.c_str());
cout << "学生删除成功" << endl;
}
else
{
remove("data/temp.txt");
cout << "未找到该学生学号" << endl;
}
}
bool Manager::login(string name,string password)
{
ifstream in("data/user.txt");
if (!in.is_open())
{
cout << "文件打开失败" << endl;
return false;
}
string username, userpassword;
in >> username >> userpassword;
if (name == username && password == userpassword) {
cout << "登录成功" << endl;
return true;
}
cout << "账号或密码错误" << endl;
return false;
}