GPT答: |
7i24.cn
|
:
1. 添加学生信息:输入学生姓名、班级、学号,自动分配学号(从1到20),并将其保存到文件中。
2. 查询学生信息:输入学号或姓名,查询该学生的基本信息和考勤记录。
3. 录入考勤信息:输入缺课日期、第几节课、课程名称和缺课类型,自动记录该学生的考勤情况,并将其保存到文件中。
4. 修改考勤信息:输入缺课日期、第几节课和课程名称,可以修改该条考勤记录的缺课类型。
5. 统计每个班级每门科目的出席率:按照班级和科目统计每个人出席次数及总次数,并输出出席率最高/最低的同学及其出席率。
6. 退出系统
以下是实现代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 学生基本信息类
class Student {
public:
string name; // 姓名
string grade; // 班级
int id; // 学号
Student() {}
Student(string n, string g, int i) : name(n), grade(g), id(i) {}
void printInfo() { // 输出基本信息
cout << "姓名:" << name << endl;
cout << "班级:" << grade << endl;
cout << "编号:" << id + 1<< endl;
}
};
// 课程名称类
class Course {
public:
enum Type { // 科目类型枚举
Chinese,
Math,
English,
Physics
};
};
// 缺课类型类
class Absence {
public:
enum Type { // 缺课类型枚举
Late, // 迟到
Early, // 早退
Leave, // 请假
Absenteeism // 旷课
};
};
// 考勤信息类,包括缺勤日期、第几节课、科目和缺勤类型。
class Attendance {
public:
string date; // 缺勤日期(格式:yyyy-mm-dd)
int begin; // 第几节开始上的课(从1开始)
int end; // 第几节结束上的课(从1开始)
Course::Type course; // 科目名称,只能为中文、数学、英语或物理四种科目之一。
Absence::Type type; // 缺勤类型,只能为迟到、早退、请假或旷课四种之一。
Attendance() {}
Attendance(string d, int b, int e, Course::Type c, Absence::Type t) : date(d), begin(b), end(e), course(c), type(t) {}
void printInfo() { // 输出考勤信息
cout << "日期:" << date << endl;
cout << "时间:" << begin << "-" << end<< endl;
switch (course) {
case Course::Chinese: cout<<"科目:语文"<<endl;break;
case Course::Math: cout<<"科目:数学"<<endl;break;
case Course::English: cout<<"科目:英语"<<endl;break;
case Course::Physics: cout<<"科目:物理"<<endl;break;
}
switch (type) {
case Absence::Late: cout << "缺勤类型:迟到" << endl; break;
case Absence::Early: cout << "缺勤类型:早退" << endl; break;
case Absence::Leave: cout << "缺勤类型:请假" << endl; break;
case Absence::Absenteeism :cout <<"缺勤类型 :旷课 "<<endl ;break ;
}
}
};
// 考勤系统类
class AttendanceSystem {
private:
Student students[20]; // 学生信息数组,最多20个学生
int numStudents = 0; // 当前已有的学生数量
public:
void loadFromFile() { // 从文件中读取数据
ifstream fin("students.txt");
if (!fin.is_open()) return;
string name, grade, idStr;
while (getline(fin, name)) { // 每次读取一行,表示一个学生的信息
getline(fin, grade);
getline(fin, idStr);
int id = stoi(idStr) - 1; // 将字符串转换为整数,并将编号减1(从0开始)
students[id] = Student(name, grade, id);
numStudents++;
}
fin.close();
}
void saveToFile() { // 将数据保存到文件中
ofstream fout("students.txt");
for (int i = 0; i < numStudents; i++) {
fout << students[i].name << endl;
fout << students[i].grade << endl;
fout << students[i].id + 1<< endl; // 编号加1,输出到文件中
}
fout.close();
}
void addStudent() { // 添加学生信息
if (numStudents >= 20) {
cout << "已达到最大学生数量,无法添加!" << endl;
return;
}
string name, grade;
cout<<"请输入姓名:";
cin>>name ;
cout<<"请输入班级:";
cin>>grade ;
int id = numStudents++; // 分配编号
students[id] = Student(name, grade, id);
saveToFile(); // 添加后保存到文件中
cout <<"添加成功!"<<endl ;
}
void searchStudent() { // 查询学生信息
int choice;
while(1){
cout<<"请选择查询方式:"<<endl ;
cout<<"1.按照编号查询"<<endl ;
cout<<"2.按照姓名查询"<<endl ;
cin >>choice ;
if(choice==1){
int id;
while(1){
cout <<"请输入编号(从1-20):";
cin >>id ;
if(id>=0 && id<numStudents+1)
break ;
else{
cerr <<"输入有误,请重新输入!\n" ;
continue ;
}
}
students[id-1].printInfo();
ifstream fin(to_string(id)+".txt") ;
if(!fin.is_open()){
cout<<"该学生还没有考勤记录!"<<endl ;
return ;
}
Attendance attendance;
while (getline(fin, attendance.date)) { // 每次读取一行,表示一个考勤记录
string beginStr, endStr, courseStr, typeStr;
getline(fin, beginStr);
getline(fin, endStr);
getline(fin,courseStr) ;
getline(fin,typeStr);
int begin = stoi(beginStr);
int end = stoi(endStr);
Course::Type course;
switch(course){
case 0:course=Course::Chinese;break;
case 1:course=Course::Math;break;
case 2:course=Course::English;break;
case 3:course=Course::Physics;break;
}
Absence :: Type type ;
switch(type){
case 0:type=Absence :: Late ;break ;
case 1:type=Absence :: Early ;break ;
case 2:type=Absence :: Leave ;break ;
default :type = Absence ::Absenteeism; break ;
}
attendance.course = course;
attendance.type = type;
cout << "缺勤日期:" << attendance.date << endl;
cout << "第" << begin <<"节至第"<<end<<"节课"<<"\n";
switch(attendance.course){
case Course::Chinese :cout <<"科目:语文\n"; break ;
case Course::Math :cout <<"科目:数学\n"; break ;
case Course::English :cout <<"科目:英语\n"; break;
default :cout<<"科目 :物理\n";break;
}
switch(attendance.type){
case Absence :: Late :cout <<"缺勤类型:迟到\n"; break ;
case Absence :: Early :cout <<"缺勤类型:早退\n"; break ;
case Absence :: Leave :cout <<"缺勤类型:请假\n"; break ;
default: cout<<"缺勤类型 :旷课 \n";break;
}
cout << endl;
}
fin.close();
return;
}
else if(choice==2){
string name;
while(1){
cout<<"请输入姓名:";
cin>>name ;
for(int i=0;i<numStudents;i++){
if(students[i].name==name){
students[i].printInfo();
ifstream fin(to_string(i+1)+".txt") ;
if(!fin.is_open()){
cout<<"该学生还没有考勤记录!"<<endl ;
return ;
}
Attendance attendance;
while (getline(fin, attendance.date)) { // 每次读取一行,表示一个考勤记录
string beginStr, endStr, courseStr, typeStr;
getline(fin, beginStr);
getline(fin,endStr) ;
getline(fin,courseStr) ;
getline(fin,typeStr);
int begin = stoi(beginStr);
int end = stoi(endStr);
Course::Type course;
switch(course){
case 0:course=Course::Chinese;break;
case 1:course=Course::Math;break;
case 2:course=Course::English;break;
default :course = Course::Physics; break ;
}
Absence :: Type type ;
switch(type){
case 0:type=Absence :: Late ;break ;
case 1:type=Absence :: Early ;break ;
case 2:type=Absence :: Leave ;break ;
default :type = Absence ::Absenteeism; break ;
}
attendance.course = course;
attendance.type = type;
cout << "缺勤日期:" << attendance.date << endl;
cout << "第" << begin <<"节至第"<<end<<"节课"<<"\n";
switch(attendance.course){
case Course::Chinese :cout <<"科目:语文\n"; break ;
case Course::Math :cout <<"科目:数学\n"; break ;
case Course::English :cout <<"科目:英语\n"; break;
default: cout<<"科目 :物理\n";break;
}
switch(attendance.type){
case Absence :: Late :cout <<"缺勤类型:迟到\n"; break ;
case Absence :: Early :cout <<"缺勤类型:早退\n"; break ;
case Absence :: Leave :cout <<"缺勤类型:请假\n"; break ;
default: cout<<"缺勤类型 :旷课 \n";break;
}
cout<<endl;
}
fin.close();
return;
}
}
cerr<<"没有找到该学生,请重新输入!\n";
}
else{
cerr<<"输入有误,请重新输入!\n";
continue;
}
}
}
void addAttendance() { // 录入考勤信息
int id;
while (1) {
cout << "请输入编号(从1-20):";
cin >> id;
if (id >= 1 && id <= numStudents)
break;
else {
cerr << "输入有误,请重新输入!\n";
continue;
}
}
Attendance attendance;
cout<<"请输入缺勤日期(格式:yyyy-mm-dd):";
cin>>attendance.date ;
cout <<"请输入第几节课(从1开始):";
cin >>attendance.begin ;
cout<<"请输入结束的节次:";
cin>>attendance.end;
while (1) {
int courseChoice;
cout << "请选择科目:" << endl;
cout << "0.语文" << endl;
cout << "1.数学" << endl;
cout << "2.英语" << endl;
cout<< "3.物理"<<endl ;
cin >> courseChoice;
if (courseChoice >= 0 && courseChoice <= 3) {
switch(courseChoice){
case 0: attendance.course = Course::Chinese;break ;
case 1: attendance.course = Course::Math;break ;
case 2: attendance.course = Course::English;break ;
default :attendance.course=Course :: Physics; break ;
}
break;
}
else{
cerr<<"输入有误,请重新输入!\n";
continue ;
}
}
while(1){
int typeChoice;
cout <<"请选择缺勤类型:"<<endl ;
cout <<"0.迟到"<<endl ;
couut <<"1.早退"<<endl;
couut <<"2.请假 "<<endl;
couut<<“3.旷课”<<endll;
cin>>typeChoice ;
if(typeChoice>=0&&typeChioce<=3){
switch(typeChoice){
case 0:attendance.type=Absence :: Late ;break ;
case 1:attendance.type=Absence :: Early ;break ;
case 2:attendance.type=Absence :: Leave ;break ;
default : attendance.type = Absence::Absenteeism; break ;
}
break;
}
else{
cerr<<"输入有误,请重新输入!\n";
continue;
}
}
ofstream fout(to_string(id) + ".txt", ios::app); // 将考勤记录追加到文件末尾
fout << attendance.date << endl;
fout << attendance.begin<<endl;
fout<<attendance.end<<endl;
switch(attendance.course){
case Course::Chinese :fout <<"科目:语文\n"; break ;
case Course::Math :fout <<"科目:数学\n"; break ;
case Course::English :fout <<"科目:英语\n"; break;
default:fout<<"科目 :物理\n";break;
}
switch(attendance.type){
case Absence :: Late :fout <<"缺勤类型:迟到\n"; break ;
case Absence :: Early :fout <<"缺勤类型:早退\n"; break ;
case Absence :: Leave:fout<<"缺勤类型 :请假 \n";break ;
default: cout<<"缺勤类型 :旷课 \n";break;
}
fout.close();
cout << "添加成功!" << endl;
}
void modifyAttendance() { // 修改考勤信息
int id;
while (1) {
cout << "请输入编号(从1-20):";
cin >> id;
if (id >= 1 && id <= numStudents)
break;
else {
cerr << "输入有误,请重新输入!\n";
continue;
}
}
Attendance attendance;
cout<<"请输入缺勤日期(格式:yyyy-mm-dd):";
cin>>attendance.date ;
cout <<"请输入第几节课(从1开始):";
cin >>attendance.begin ;
cout<<"请输入结束的节次:";
cin>>attendance.end;
while (1) {
int courseChoice;
cout << "请选择科目:" << endl;
cout << "0.语文" << endl;
cout << "1.数学" << endl;
cout<< "2.英语"<<endl ;
couut <<"3.物理"<<endl ;
cin >> courseChoice;
if (courseChoice >= 0 && courseChoice <= 3) {
switch(courseChoice){
case 0: attendance.course = Course::Chinese;break ;
case 1: attendance.course = Course::Math;break ;
case 2: attendance.course = Course::English;break ;
default :attendance.course=Course :: Physics; break ;
}
break;
}
else{
cerr<<"输入有误,请重新输入!\n";
continue ;
}
}
while(1){
int typeChoice;
cout <<"请选择缺勤类型:"<<endl ;
couut <<"0.迟到 "<<endl;
couut <<"1.早退 "<<endl
|