用c++设计一个程序,基本要求: 考勤信息记录了学生的缺课情况,它包括:缺课日期、第几节课、课程名称、学生姓名、缺课类型 (迟到、早退、请假及旷课)。 功能要求: 4)统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序; 5)统计某段时间内,有学生旷课的课程及旷课人次,按旷课人次由多到少排序;
时间: 2024-01-08 19:03:21 浏览: 31
好的,下面是一个简单的示例程序,实现了你提出的基本要求:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
// 学生缺课信息结构体
struct Record {
string date; // 缺课日期
int section; // 第几节课
string courseName; // 课程名称
string studentName; // 学生姓名
string type; // 缺课类型
};
// 打印菜单
void printMenu() {
cout << "1. 录入学生缺课记录" << endl;
cout << "2. 修改某个学生的缺课记录" << endl;
cout << "3. 查询某个学生的缺课情况" << endl;
cout << "4. 统计某段时间内旷课学生姓名及旷课次数" << endl;
cout << "5. 统计某段时间内有学生旷课的课程及旷课人次" << endl;
cout << "6. 退出系统" << endl;
cout << "请选择操作(输入数字1-6):";
}
// 录入学生缺课记录
void addRecord(vector<Record>& records) {
Record record;
cout << "请输入缺课日期(yyyy-mm-dd):";
cin >> record.date;
cout << "请输入第几节课(1-8):";
cin >> record.section;
cout << "请输入课程名称:";
cin >> record.courseName;
cout << "请输入学生姓名:";
cin >> record.studentName;
cout << "请输入缺课类型(迟到、早退、请假或旷课):";
cin >> record.type;
records.push_back(record);
cout << "已添加缺课记录!" << endl;
}
// 修改学生缺课记录
void modifyRecord(vector<Record>& records) {
string studentName;
cout << "请输入要修改的学生姓名:";
cin >> studentName;
for (auto& record : records) {
if (record.studentName == studentName) {
cout << "请输入新的缺课日期(原值:" << record.date << "):";
cin >> record.date;
cout << "请输入新的第几节课(原值:" << record.section << "):";
cin >> record.section;
cout << "请输入新的课程名称(原值:" << record.courseName << "):";
cin >> record.courseName;
cout << "请输入新的缺课类型(迟到、早退、请假或旷课,原值:" << record.type << "):";
cin >> record.type;
cout << "已修改缺课记录!" << endl;
return;
}
}
cout << "找不到该学生的缺课记录!" << endl;
}
// 查询学生缺课情况
void queryRecord(const vector<Record>& records) {
string studentName;
cout << "请输入要查询的学生姓名:";
cin >> studentName;
for (const auto& record : records) {
if (record.studentName == studentName) {
cout << "缺课日期:" << record.date << endl;
cout << "第几节课:" << record.section << endl;
cout << "课程名称:" << record.courseName << endl;
cout << "学生姓名:" << record.studentName << endl;
cout << "缺课类型:" << record.type << endl;
return;
}
}
cout << "找不到该学生的缺课记录!" << endl;
}
// 统计某段时间内旷课学生姓名及旷课次数
void countAbsentStudents(const vector<Record>& records) {
string startDate, endDate;
cout << "请输入统计起始日期(yyyy-mm-dd):";
cin >> startDate;
cout << "请输入统计结束日期(yyyy-mm-dd):";
cin >> endDate;
map<string, int> absentCounts;
for (const auto& record : records) {
if (record.type == "旷课" && record.date >= startDate && record.date <= endDate) {
absentCounts[record.studentName]++;
}
}
if (absentCounts.empty()) {
cout << "该时间段内没有学生旷课!" << endl;
} else {
cout << "旷课学生名单及旷课次数:" << endl;
vector<pair<string, int>> absentPairs(absentCounts.begin(), absentCounts.end());
sort(absentPairs.begin(), absentPairs.end(), [](const pair<string, int>& a, const pair<string, int>& b) {
return a.second > b.second;
});
for (const auto& absentPair : absentPairs) {
cout << absentPair.first << ": " << absentPair.second << "次" << endl;
}
}
}
// 统计某段时间内有学生旷课的课程及旷课人次
void countAbsentCourses(const vector<Record>& records) {
string startDate, endDate;
cout << "请输入统计起始日期(yyyy-mm-dd):";
cin >> startDate;
cout << "请输入统计结束日期(yyyy-mm-dd):";
cin >> endDate;
map<string, int> absentCounts;
for (const auto& record : records) {
if (record.type == "旷课" && record.date >= startDate && record.date <= endDate) {
absentCounts[record.courseName]++;
}
}
if (absentCounts.empty()) {
cout << "该时间段内没有学生旷课!" << endl;
} else {
cout << "有学生旷课的课程及旷课人次:" << endl;
vector<pair<string, int>> absentPairs(absentCounts.begin(), absentCounts.end());
sort(absentPairs.begin(), absentPairs.end(), [](const pair<string, int>& a, const pair<string, int>& b) {
return a.second > b.second;
});
for (const auto& absentPair : absentPairs) {
cout << absentPair.first << ": " << absentPair.second << "人次" << endl;
}
}
}
int main() {
vector<Record> records;
int op;
while (true) {
printMenu();
cin >> op;
switch (op) {
case 1:
addRecord(records);
break;
case 2:
modifyRecord(records);
break;
case 3:
queryRecord(records);
break;
case 4:
countAbsentStudents(records);
break;
case 5:
countAbsentCourses(records);
break;
case 6:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的操作,请重新选择!" << endl;
break;
}
}
}
```
这个程序在原有基础上增加了两个操作:统计某段时间内旷课学生姓名及旷课次数,按旷课次数由多到少排序;统计某段时间内有学生旷课的课程及旷课人次,按旷课人次由多到少排序。程序使用了一个 `map` 来存储旷课学生姓名及旷课次数,或者旷课课程及旷课人次,并使用了 `sort` 算法来进行排序。你可以根据自己的需要对程序进行改进和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)