-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileController.h
360 lines (315 loc) · 12.1 KB
/
FileController.h
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
//
// Created by 下水道的小老鼠 on 2023/6/3.
//
#ifndef UNTITLED_FILECONTROLLER_H
#define UNTITLED_FILECONTROLLER_H
#include "CEmployee.h"
#include <optional>
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
class FileController {
public:
/**
* 读取文件中的数据
*
* @param filename 文件名
* @return
*/
static vector<CEmployee> readEmployeesFromFile(const string &filename) {
vector<CEmployee> employees;
//打开文件
ifstream infile(filename);
if (!infile.is_open()) {
cout << "Error opening file: " << filename << endl;
return employees;
}
//读取文件中的每一行
string line;
while (getline(infile, line)) {
istringstream iss(line);
vector<string> tokens;
string token;
//每一行以逗号分隔符读取
while (getline(iss, token, ',')) {
tokens.push_back(token);
}
if (tokens.size() == 5) {
BDay birthday = BDay(0, 0, 0);
stringstream ss(tokens[3]);
ss >> birthday.year;
ss.ignore();
ss >> birthday.month;
ss.ignore();
ss >> birthday.day;
//读取的数据放到集合中
employees.emplace_back(tokens[0].c_str(), tokens[1].c_str(), tokens[2].c_str(), birthday,
stod(tokens[4]));
} else {
cout << "Invalid line: " << line << endl;
}
}
infile.close();
//返回集合
return employees;
}
/**
* 向文件中写入员工数据
*
* @param employees 员工的数据
* @param filename 要写入的文件名
*/
static void writeEmployeeToFile(vector<CEmployee> &employees, const string &filename) {
// ios_base::app 表示在文件尾部添加新数据
ofstream outfile(filename, ios_base::app);
if (!outfile.is_open()) {
cout << "Error opening file: " << filename << endl;
return;
}
for (CEmployee employee: employees) {
outfile << employee.getName() << "," << employee.getSex() << "," << employee.getKind() << ","
<< employee.getBirthday().year << "-"
<< employee.getBirthday().month << "-"
<< employee.getBirthday().day << ","
<< employee.getTotalSalary() << endl;
}
outfile.close();
}
/**
* 根据名字删除文件中文件中员工信息
*
* @param name 员工名字
* @param filename 文件名
*/
static void deleteEmployeeFromFile(const string &name, const string &filename) {
std::ifstream infile(filename);
//ios_base::trunc 是指如果文件已经存在,则先删除源文件,然后重新创建一个空文件,以便进行下一步的写入操作
std::ofstream outfile("temp.data", ios_base::trunc);
if (!infile.is_open() || !outfile.is_open()) {
std::cout << "Error opening file: " << filename << std::endl;
return;
}
std::string line;
bool found = false;
while (std::getline(infile, line)) {
istringstream iss(line);
vector<std::string> tokens;
string token;
while (getline(iss, token, ',')) {
tokens.push_back(token);
}
// 判断是否为要删除的行
if (!tokens.empty() && tokens[0] == name) {
found = true;
//跳过这次循环,不将此行输出到临时文件中
continue;
}
//将不是要删除的行复制到临时文件
outfile << line << endl;
}
infile.close();
outfile.close();
//如果没有要删除的行,则要删除掉临时文件
if (!found) {
cout << "Could not find employee: " << name << endl;
remove("temp.data");
return;
}
//删除之前的文件,并将临时文件改名为原来的文件名
remove(filename.c_str());
rename("temp.data", filename.c_str());
cout << "Deleted employee: " << name << endl;
}
/**
* 根据员工名字修改员工的信息
*
* @param name 员工名字
* @param employee 员工要修改后的信息
* @param filename 文件名
*/
static void UpdateEmployeeFromFile(const string &name, CEmployee employee, const string &filename) {
std::ifstream infile(filename);
std::ofstream outfile("temp.data", ios_base::trunc);
if (!infile.is_open() || !outfile.is_open()) {
std::cout << "Error opening file: " << filename << std::endl;
return;
}
std::string line;
bool found = false;
while (std::getline(infile, line)) {
istringstream iss(line);
vector<std::string> tokens;
string token;
while (getline(iss, token, ',')) {
tokens.push_back(token);
}
// 判断是否为要修改的行
if (!tokens.empty() && tokens[0] == name) {
found = true;
//将要修改的数据输出到临时文件
outfile << employee.getName() << "," << employee.getSex() << "," << employee.getKind() << ","
<< employee.getBirthday().year << "-"
<< employee.getBirthday().month << "-"
<< employee.getBirthday().day << ","
<< employee.getTotalSalary() << endl;
//跳过这次循环,不仔将此行输出到临时文件中
continue;
}
//将不是要修改的行复制到临时文件
outfile << line << endl;
}
infile.close();
outfile.close();
//如果没有要删除的行,则要删除掉临时文件
if (!found) {
cout << "Could not find employee: " << name << endl;
remove("temp.data");
return;
}
//删除之前的文件,并将临时文件改名为原来的文件名
remove(filename.c_str());
rename("temp.data", filename.c_str());
cout << "Update employee: " << name << endl;
}
/**
* 根据员工名字查询员工
*
* @param name 员工名字
* @param filename 文件名
* @return
*/
static optional<CEmployee> searchByNameFormFile(const string &name, const string &filename) {
vector<CEmployee> temp = readEmployeesFromFile(filename);
for(CEmployee tempEmployee : temp){
if(tempEmployee.getName() == name){
return tempEmployee;
}
}
//如果没有找到指定员工返回空指针
cout << "Could not find employee: " << name << endl;
return nullopt;
}
/**
* 根据员工职位查询员工
*
* @param type 职位
* @param filename 文件名
* @return
*/
static vector<CEmployee> searchByTypeFromFile(const string &type, const string &filename) {
vector<CEmployee> temp = readEmployeesFromFile(filename);
vector<CEmployee> employees;
for(CEmployee employee : temp){
if(employee.getKind() == type){
employees.push_back(employee);
}
}
return employees;
}
/**
* 输出薪资文件
*
* @param name 名字
* @param filename 文件名
*/
static void getSalaryByFile(const string &type, const string &filename) {
//ios_base::trunc 是指如果文件已经存在,则先删除源文件,然后重新创建一个空文件,以便进行下一步的写入操作
std::ofstream outfile(R"(D:\C or C++\C++\Employees\Employees-management-system\C--simple-homework\salary.data)",
ios_base::trunc);
if (!outfile.is_open()) {
std::cout << "Error opening file: " << filename << std::endl;
return;
}
std::string line;
string arr[] = {"CMgr", "CManager", "CWage", "CSales"};
if (type == "kind") {
//按“雇员类型”排序输出
vector<CEmployee> employees;
for (const auto &i: arr) {
ifstream infile(filename);
if (!infile.is_open()) {
cout << "Error opening file: " << filename << endl;
return;
}
while (getline(infile, line)) {
istringstream iss(line);
vector<std::string> tokens;
string token;
while (getline(iss, token, ',')) {
tokens.push_back(token);
}
// 判断是否为查找的行 类型:post雇员类型 name名字
if (!tokens.empty() && tokens.size() == 5 && tokens[2] == i) {
BDay birthday = BDay(0, 0, 0);
stringstream ss(tokens[3]);
ss >> birthday.year;
ss.ignore();
ss >> birthday.month;
ss.ignore();
ss >> birthday.day;
employees.emplace_back(tokens[0].c_str(), tokens[1].c_str(), tokens[2].c_str(), birthday,
stod(tokens[4]));
}
}
outfile << "post is " << i << ":" << endl;
// 依次将堆顶元素移到最后一个位置,并从容器中删除
for (CEmployee emp: employees) {
outfile << "Name: " << emp.getName()
<< "; Sex: " << emp.getSex()
<< "; Salary: " << emp.getTotalSalary() << endl;
}
employees.clear();
infile.close();
}
} else if (type == "name") {
//选择是按“姓名”排序输出
vector<CEmployee> employees = readEmployeesFromFile(filename);
// 按名称排序
sort(employees.begin(), employees.end(), simpleSortByName());
for (CEmployee emp: employees) {
outfile << "Name: " << emp.getName()
<< "; Sex: " << emp.getSex()
<< "; Kind: " << emp.getKind()
<< "; Salary: " << emp.getTotalSalary() << endl;
}
} else if (type == "salary") {
//按“薪水”排序输出
vector<CEmployee> employees = readEmployeesFromFile(filename);
//增加堆排序算法
// 将容器转换为最大堆
make_heap(employees.begin(), employees.end(), heapSortBySalary());
// 依次将堆顶元素移到最后一个位置,并从容器中删除
while (!employees.empty()) {
pop_heap(employees.begin(), employees.end(), heapSortBySalary()); // 执行对堆的操作
// 获取堆顶元素
CEmployee emp = employees.back();
outfile << "name: " << emp.getName() << "; Salary: "
<< emp.getTotalSalary() << endl;
// 删除堆顶元素
employees.pop_back();
}
} else {
return;
}
outfile.close();
}
// 按工资由高到低排序的比较函数对象
struct heapSortBySalary {
bool operator()(const CEmployee &emp1, const CEmployee &emp2) {
// 返回 emp1 和 emp2 工资的大小关系
return emp1.getTotalSalary() < emp2.getTotalSalary();
}
};
//按名字排序
struct simpleSortByName {
bool operator()(CEmployee &emp1, CEmployee &emp2) {
return emp1.getName() > emp2.getName();
}
};
};
#endif //UNTITLED_FILECONTROLLER_H