用C++写一个“学生考勤信息管理系统”,包含注册登录;基础数据添加;考勤数据;请假系统。要求输入的学生的学号姓名保存在新建的txt记事本文件中。
时间: 2024-02-13 18:04:36 浏览: 41
这是一个比较大的项目,需要一定的编程基础和时间。以下是一个简单的示例代码,包括学生类、考勤类、请假类和主函数。
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <ctime>
using namespace std;
// 学生类
class Student {
public:
Student(int id, const string& name) : id(id), name(name) {}
int getId() const { return id; }
string getName() const { return name; }
private:
int id;
string name;
};
// 考勤类
class Attendance {
public:
Attendance(int id) : id(id) {}
void signIn() { signInTime = time(0); }
void signOut() { signOutTime = time(0); }
bool isSignedIn() const { return signInTime > 0; }
bool isSignedOut() const { return signOutTime > 0; }
time_t getSignInTime() const { return signInTime; }
time_t getSignOutTime() const { return signOutTime; }
private:
int id;
time_t signInTime = 0;
time_t signOutTime = 0;
};
// 请假类
class Leave {
public:
Leave(int id, const string& reason, const string& startTime, const string& endTime) :
id(id), reason(reason), startTime(startTime), endTime(endTime) {}
int getId() const { return id; }
string getReason() const { return reason; }
string getStartTime() const { return startTime; }
string getEndTime() const { return endTime; }
private:
int id;
string reason;
string startTime;
string endTime;
};
// 学生管理类
class StudentManager {
public:
void addStudent(const Student& student) {
students[student.getId()] = student;
}
bool hasStudent(int id) const {
return students.count(id) > 0;
}
Student getStudent(int id) const {
return students.at(id);
}
vector<Student> getAllStudents() const {
vector<Student> result;
for (auto& pair : students) {
result.push_back(pair.second);
}
return result;
}
private:
map<int, Student> students;
};
// 考勤管理类
class AttendanceManager {
public:
void signIn(int id) {
if (!hasAttendance(id)) {
attendances[id] = Attendance(id);
}
attendances[id].signIn();
}
void signOut(int id) {
if (!hasAttendance(id)) {
attendances[id] = Attendance(id);
}
attendances[id].signOut();
}
bool hasAttendance(int id) const {
return attendances.count(id) > 0;
}
Attendance getAttendance(int id) const {
return attendances.at(id);
}
vector<Attendance> getAllAttendances() const {
vector<Attendance> result;
for (auto& pair : attendances) {
result.push_back(pair.second);
}
return result;
}
private:
map<int, Attendance> attendances;
};
// 请假管理类
class LeaveManager {
public:
void addLeave(const Leave& leave) {
leaves.push_back(leave);
}
vector<Leave> getAllLeaves() const {
return leaves;
}
private:
vector<Leave> leaves;
};
// 主函数
int main() {
StudentManager studentManager;
AttendanceManager attendanceManager;
LeaveManager leaveManager;
// 读取学生信息
ifstream ifs("students.txt");
string line;
while (getline(ifs, line)) {
istringstream iss(line);
int id;
string name;
iss >> id >> name;
studentManager.addStudent(Student(id, name));
}
// 登录
int id;
cout << "请输入学号:";
cin >> id;
if (!studentManager.hasStudent(id)) {
cout << "学号不存在!" << endl;
return 0;
}
// 添加考勤信息
attendanceManager.signIn(id);
attendanceManager.signOut(id);
// 添加请假信息
string reason, startTime, endTime;
cout << "请输入请假原因:";
cin >> reason;
cout << "请输入开始时间(YYYY-MM-DD):";
cin >> startTime;
cout << "请输入结束时间(YYYY-MM-DD):";
cin >> endTime;
leaveManager.addLeave(Leave(id, reason, startTime, endTime));
// 输出所有考勤信息
cout << "考勤信息:" << endl;
for (auto& attendance : attendanceManager.getAllAttendances()) {
auto student = studentManager.getStudent(attendance.getId());
cout << student.getName() << "(" << student.getId() << "):";
if (attendance.isSignedIn()) {
cout << "签到时间:" << ctime(&attendance.getSignInTime());
}
if (attendance.isSignedOut()) {
cout << "签退时间:" << ctime(&attendance.getSignOutTime());
}
}
// 输出所有请假信息
cout << "请假信息:" << endl;
for (auto& leave : leaveManager.getAllLeaves()) {
auto student = studentManager.getStudent(leave.getId());
cout << student.getName() << "(" << student.getId() << "):" << leave.getReason() << ",从" << leave.getStartTime() << "到" << leave.getEndTime() << endl;
}
return 0;
}
```
需要注意的是,此代码只是一个简单的示例,还有很多细节需要注意。例如,需要处理输入错误、文件读写错误等情况。同时,此代码仅供参考,具体实现可能会因为需求的不同而有所不同。