#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
enum AbsenceType { LATE = 1, LEAVE_EARLY, ASK_FOR_LEAVE, CUT_CLASS };
struct Record {
string date;
int classNumber;
string className;
string studentName;
AbsenceType type;
string toString() const {
return date + "#" + to_string(classNumber) + "#" + className + "#" +
studentName + "#" + to_string(type);
}
void print() const {
cout << date << " " << classNumber << "\t "
<< left << setw(15) << className << "\t"
<< left << setw(18) << studentName << " ";
switch(type) {
case LATE: cout << "late"; break;
case LEAVE_EARLY: cout << "leave early"; break;
case ASK_FOR_LEAVE: cout << "ask for leave"; break;
case CUT_CLASS: cout << "cut a class"; break;
}
cout << endl;
}
};
class AttendanceSystem {
private:
vector<Record> records;
bool isValidDate(const string& date) {
if (date.length() != 6) return false;
int year = stoi(date.substr(0, 2));
int month = stoi(date.substr(2, 2));
int day = stoi(date.substr(4, 2));
if (year < 0 || year > 99) return false;
if (month < 1 || month > 12) return false;
const int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
int maxDay = daysInMonth[month];
if (month == 2 && isLeapYear) {
maxDay = 29;
}
return day >= 1 && day <= maxDay;
}
void saveToFile() {
ofstream out("data.txt");
for (const auto& record : records) {
out << record.toString() << endl;
}
out << "!" << endl;
}
void loadFromFile() {
ifstream in("data.txt");
if (!in) return;
records.clear();
string line;
while (getline(in, line) && line != "!") {
size_t pos = 0;
Record record;
pos = line.find('#');
record.date = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('#');
record.classNumber = stoi(line.substr(0, pos));
line.erase(0, pos + 1);
pos = line.find('#');
record.className = line.substr(0, pos);
line.erase(0, pos + 1);
pos = line.find('#');
record.studentName = line.substr(0, pos);
line.erase(0, pos + 1);
record.type = static_cast<AbsenceType>(stoi(line));
records.push_back(record);
}
}
public:
AttendanceSystem() {
loadFromFile();
}
void showAllRecords() {
if (records.empty()) {
cout << "No records found.\n";
return;
}
cout << "\nN date ordinal name of class\tname of student\t type\n";
for (size_t i = 0; i < records.size(); ++i) {
cout << i << " ";
records[i].print();
}
}
void addRecord() {
Record newRecord;
do {
cout << "Date (YYMMDD): ";
cin >> newRecord.date;
} while (!isValidDate(newRecord.date));
do {
cout << "Class number (1-10): ";
cin >> newRecord.classNumber;
} while (newRecord.classNumber < 1 || newRecord.classNumber > 10);
cout << "Class name: ";
cin >> newRecord.className;
cout << "Student name: ";
cin >> newRecord.studentName;
int type;
do {
cout << "Absence type (1-late, 2-leave early, 3-ask for leave, 4-cut class): ";
cin >> type;
} while (type < 1 || type > 4);
newRecord.type = static_cast<AbsenceType>(type);
auto it = find_if(records.begin(), records.end(), [&](const Record& r) {
return r.date == newRecord.date &&
r.classNumber == newRecord.classNumber &&
r.className == newRecord.className &&
r.studentName == newRecord.studentName &&
r.type == newRecord.type;
});
if (it != records.end()) {
char choice;
cout << "This record already exists. Add anyway? (y/n): ";
cin >> choice;
if (tolower(choice) != 'y') return;
}
records.push_back(newRecord);
saveToFile();
}
void searchStudent() {
if (records.empty()) {
cout << "No records found.\n";
return;
}
string name;
cout << "Enter student name to search: ";
cin >> name;
bool found = false;
for (size_t i = 0; i < records.size(); ++i) {
if (records[i].studentName == name) {
if (!found) {
cout << "\nN date ordinal name of class\tname of student\t type\n";
found = true;
}
cout << i << " ";
records[i].print();
}
}
if (!found) {
cout << "No records found for student " << name << endl;
}
}
void editRecord() {
if (records.empty()) {
cout << "No records found.\n";
return;
}
showAllRecords();
size_t index;
cout << "Enter record number to edit: ";
cin >> index;
if (index >= records.size()) {
cout << "Invalid record number.\n";
return;
}
Record& record = records[index];
int choice;
cout << "What to edit?\n"
<< "1. Date\n2. Class number\n3. Class name\n4. Student name\n5. Type\n6. All\nChoice: ";
cin >> choice;
if (choice == 1 || choice == 6) {
do {
cout << "New date (YYMMDD): ";
cin >> record.date;
} while (!isValidDate(record.date));
}
if (choice == 2 || choice == 6) {
do {
cout << "New class number (1-10): ";
cin >> record.classNumber;
} while (record.classNumber < 1 || record.classNumber > 10);
}
if (choice == 3 || choice == 6) {
cout << "New class name: ";
cin >> record.className;
}
if (choice == 4 || choice == 6) {
cout << "New student name: ";
cin >> record.studentName;
}
if (choice == 5 || choice == 6) {
int type;
do {
cout << "New type (1-late, 2-leave early, 3-ask for leave, 4-cut class): ";
cin >> type;
} while (type < 1 || type > 4);
record.type = static_cast<AbsenceType>(type);
}
saveToFile();
}
void deleteRecord() {
if (records.empty()) {
cout << "No records found.\n";
return;
}
showAllRecords();
int choice;
cout << "Enter record number to delete (-1 to delete all): ";
cin >> choice;
if (choice == -1) {
records.clear();
} else if (choice >= 0 && static_cast<size_t>(choice) < records.size()) {
records.erase(records.begin() + choice);
} else {
cout << "Invalid choice.\n";
return;
}
saveToFile();
}
void generateStatistics() {
if (records.empty()) {
cout << "No records found.\n";
return;
}
string startDate, endDate;
do {
cout << "Enter date range (YYMMDD-YYMMDD): ";
cin >> startDate >> endDate;
} while (!isValidDate(startDate) || !isValidDate(endDate));
vector<Record> filtered;
copy_if(records.begin(), records.end(), back_inserter(filtered),
[&](const Record& r) {
return r.date >= startDate && r.date <= endDate && r.type == CUT_CLASS;
});
if (filtered.empty()) {
cout << "No records found in this period.\n";
return;
}
cout << "\nStatistics from " << startDate << " to " << endDate << ":\n";
cout << "1. By student\n2. By class\nChoice: ";
int choice;
cin >> choice;
if (choice == 1) {
vector<pair<string, int>> counts;
for (const auto& r : filtered) {
auto it = find_if(counts.begin(), counts.end(),
[&](const pair<string, int>& p) { return p.first == r.studentName; });
if (it != counts.end()) {
it->second++;
} else {
counts.emplace_back(r.studentName, 1);
}
}
sort(counts.begin(), counts.end(),
[](const pair<string, int>& a, const pair<string, int>& b) {
return b.second < a.second;
});
cout << "Student name Count\n";
for (const auto& p : counts) {
cout << left << setw(20) << p.first << p.second << endl;
}
} else if (choice == 2) {
vector<pair<string, int>> counts;
for (const auto& r : filtered) {
auto it = find_if(counts.begin(), counts.end(),
[&](const pair<string, int>& p) { return p.first == r.className; });
if (it != counts.end()) {
it->second++;
} else {
counts.emplace_back(r.className, 1);
}
}
sort(counts.begin(), counts.end(),
[](const pair<string, int>& a, const pair<string, int>& b) {
return b.second < a.second;
});
cout << "Class name Count\n";
for (const auto& p : counts) {
cout << left << setw(20) << p.first << p.second << endl;
}
}
}
};
#include<windows.h>
int main() {
SetConsoleOutputCP(65001);
AttendanceSystem system;
while (true) {
cout << "\n学生考勤管理系统\n"
<< "1. 显示所有记录\n"
<< "2. 添加新记录\n"
<< "3. 搜索学生\n"
<< "4. 编辑记录\n"
<< "5. 删除记录\n"
<< "6. 生成统计信息\n"
<< "7. 退出\n"
<< "退出: ";
int choice;
cin >> choice;
switch (choice) {
case 1: system.showAllRecords(); break;
case 2: system.addRecord(); break;
case 3: system.searchStudent(); break;
case 4: system.editRecord(); break;
case 5: system.deleteRecord(); break;
case 6: system.generateStatistics(); break;
case 7: return 0;
default: cout << "Invalid choice.\n";
}
cout << "\nContinue? (y/n): ";
char cont;
cin >> cont;
if (tolower(cont) != 'y') break;
}
return 0;
} 给我生成一个这个代码的程序设计报告
最新发布