请用c++ stl实现以下内容:读取data数据,将其按照demo的格式放入demo中去,要求空出总分 客观题总分 主观题总分 ,demo中格式为考号 姓名 班级 总分 学校排名 班级排名 客观题总分 主观题总分 选择1 选择2 选择3 选择4 选择5 选择6 选择7 选择8 选择9 选择10 选择11 选择12 填空题 17 18 19 20 21 22,其中选择1 选择2 选择3 选择4 选择5 选择6 选择7 选择8 选择9 选择10 选择11 选择12 一行中的数据相加填入对应行的客观题总分中,填空题 17 18 19 20 21 22一行中的数据相加填入主观题总分中,之后主观题总分和客观题总分相加填入总分中,然后读取demo,将demo中的数据按照总分对不同班级进行排名和学校排名,按照班级依次以csv格式导出不同班级的表格,然后以csv格式导出学校的表格,要求用c++ stl进行编写代码示例.
时间: 2023-07-21 13:41:07 浏览: 85
这里是用C++ STL实现的示例代码:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
struct Student {
string id;
string name;
string class_name;
int total_score;
int rank_school;
int rank_class;
int objective_score;
int subjective_score;
vector<int> objective_scores;
vector<int> subjective_scores;
};
struct Class {
string name;
vector<Student> students;
};
bool comp(const Student& s1, const Student& s2) {
return s1.total_score > s2.total_score;
}
void readData(const string& filename, vector<Student>& students) {
ifstream ifs(filename);
if (!ifs) {
cout << "Failed to open file " << filename << endl;
return;
}
string line;
while (getline(ifs, line)) {
Student student;
student.objective_score = 0;
student.subjective_score = 0;
stringstream ss(line);
ss >> student.id >> student.name >> student.class_name;
for (int i = 0; i < 12; ++i) {
int score;
ss >> score;
student.objective_scores.push_back(score);
student.objective_score += score;
}
for (int i = 0; i < 6; ++i) {
int score;
ss >> score;
student.subjective_scores.push_back(score);
student.subjective_score += score;
}
student.total_score = student.objective_score + student.subjective_score;
students.push_back(student);
}
ifs.close();
}
void writeDemo(const string& filename, const vector<Student>& students) {
ofstream ofs(filename);
ofs << "考号,姓名,班级,总分,学校排名,班级排名,客观题总分,主观题总分,选择1,选择2,选择3,选择4,选择5,选择6,选择7,选择8,选择9,选择10,选择11,选择12,填空题17,填空题18,填空题19,填空题20,填空题21,填空题22" << endl;
for (const auto& student : students) {
ofs << student.id << "," << student.name << "," << student.class_name << ",";
ofs << "," << student.total_score << "," << student.rank_school << ",";
ofs << student.rank_class << "," << student.objective_score << ",";
ofs << student.subjective_score << ",";
for (int score : student.objective_scores) {
ofs << score << ",";
}
for (int score : student.subjective_scores) {
ofs << score << ",";
}
ofs << endl;
}
ofs.close();
}
void rankSchool(vector<Student>& students) {
sort(students.begin(), students.end(), comp);
int rank = 1;
int prev_score = students[0].total_score;
for (auto& student : students) {
if (student.total_score < prev_score) {
rank++;
prev_score = student.total_score;
}
student.rank_school = rank;
}
}
void rankClass(vector<Class>& classes) {
for (auto& c : classes) {
sort(c.students.begin(), c.students.end(), comp);
int rank = 1;
int prev_score = c.students[0].total_score;
for (auto& student : c.students) {
if (student.total_score < prev_score) {
rank++;
prev_score = student.total_score;
}
student.rank_class = rank;
}
}
}
void writeClass(const string& filename, const Class& c) {
ofstream ofs(filename);
ofs << "考号,姓名,班级,总分,学校排名,班级排名,客观题总分,主观题总分,选择1,选择2,选择3,选择4,选择5,选择6,选择7,选择8,选择9,选择10,选择11,选择12,填空题17,填空题18,填空题19,填空题20,填空题21,填空题22" << endl;
for (const auto& student : c.students) {
ofs << student.id << "," << student.name << "," << student.class_name << ",";
ofs << "," << student.total_score << "," << student.rank_school << ",";
ofs << student.rank_class << "," << student.objective_score << ",";
ofs << student.subjective_score << ",";
for (int score : student.objective_scores) {
ofs << score << ",";
}
for (int score : student.subjective_scores) {
ofs << score << ",";
}
ofs << endl;
}
ofs.close();
}
void writeRanking(const string& filename, const vector<Student>& students) {
ofstream ofs(filename);
ofs << "排名,考号,姓名,班级,总分,学校排名,班级排名" << endl;
int rank = 1;
int prev_score = students[0].total_score;
for (const auto& student : students) {
if (student.total_score < prev_score) {
rank++;
prev_score = student.total_score;
}
ofs << rank << "," << student.id << "," << student.name << "," << student.class_name << ",";
ofs << "," << student.total_score << "," << student.rank_school << ",";
ofs << student.rank_class << endl;
}
ofs.close();
}
int main() {
vector<Student> students;
readData("data.csv", students);
rankSchool(students);
map<string, Class> classes;
for (const auto& student : students) {
if (classes.find(student.class_name) == classes.end()) {
classes[student.class_name].name = student.class_name;
}
classes[student.class_name].students.push_back(student);
}
rankClass(vector<Class>(classes.begin(), classes.end()));
writeDemo("demo.csv", students);
writeRanking("school_ranking.csv", students);
for (const auto& p : classes) {
const auto& c = p.second;
writeClass(c.name + ".csv", c);
}
return 0;
}
```
在这个示例中,我们定义了 `Student` 结构体来存储每个学生的信息,包括学号、姓名、班级、客观题总分、主观题总分、总分、学校排名和班级排名等。我们还定义了 `Class` 结构体来存储班级信息,包括班级名称和学生信息。
我们首先使用 `readData` 函数从文件中读取学生信息,并计算出每个学生的总分、客观题分数和主观题分数。然后,我们使用 `rankSchool` 函数对所有学生按照总分进行排序,并计算出每个学生在整个学校中的排名。接下来,我们使用 `map` 来对学生按照班级进行分组,并使用 `rankClass` 函数对每个班级内的学生按照总分进行排序,并计算出每个学生在班级内的排名。
最后,我们使用 `writeDemo` 函数将学生信息写入到 `demo.csv` 文件中,并使用 `writeRanking` 函数将学生按照总分排序后的排名写入到 `school_ranking.csv` 文件中。我们还使用 `writeClass` 函数将每个班级的学生信息写入到对应的文件中。
这个示例代码中使用了多种 STL 容器和算法,包括 `vector`、`sort`、`map` 等。这些容器和算法能够方便地处理各种数据结构和进行各种操作,使得 C++ 编程更加高效和便捷。
阅读全文