读取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:52:15 浏览: 39
好的,以下是一个简单的C++代码示例,实现了题目要求的功能:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;
struct Student {
string id; // 考号
string name; // 姓名
string cls; // 班级
int score; // 总分
int school_rank; // 学校排名
int cls_rank; // 班级排名
int obj_score; // 客观题总分
int sub_score; // 主观题总分
vector<int> obj_answers; // 客观题答案
vector<string> sub_answers; // 主观题答案
};
// 读取数据文件,返回学生数据列表
vector<Student> read_data(const string& filename) {
vector<Student> students;
ifstream infile(filename);
string line;
while (getline(infile, line)) {
istringstream iss(line);
Student stu;
iss >> stu.id >> stu.name >> stu.cls;
int obj_total = 0;
int sub_total = 0;
for (int i = 0; i < 12; i++) {
int ans;
iss >> ans;
stu.obj_answers.push_back(ans);
obj_total += ans;
}
for (int i = 0; i < 6; i++) {
string ans;
iss >> ans;
stu.sub_answers.push_back(ans);
sub_total += stoi(ans);
}
stu.obj_score = obj_total;
stu.sub_score = sub_total;
stu.score = obj_total + sub_total;
students.push_back(stu);
}
return students;
}
// 对学生列表按照总分从大到小排序
void sort_students_by_score(vector<Student>& students) {
sort(students.begin(), students.end(), [](const Student& s1, const Student& s2) {
return s1.score > s2.score;
});
}
// 计算学校排名和班级排名
void calc_rank(vector<Student>& students) {
// 计算学校排名
map<int, int, greater<int>> score2count;
for (const auto& stu : students) {
score2count[stu.score]++;
}
int rank = 1;
for (auto& kv : score2count) {
kv.second = rank++;
}
for (auto& stu : students) {
stu.school_rank = score2count[stu.score];
}
// 计算班级排名
map<string, map<int, int, greater<int>>> cls2score2count;
for (const auto& stu : students) {
cls2score2count[stu.cls][stu.score]++;
}
for (auto& kv1 : cls2score2count) {
int rank = 1;
for (auto& kv2 : kv1.second) {
kv2.second = rank++;
}
for (auto& stu : students) {
if (stu.cls == kv1.first) {
stu.cls_rank = kv1.second[stu.score];
}
}
}
}
// 将学生列表按照班级导出成csv文件
void export_csv_by_cls(const vector<Student>& students) {
map<string, ofstream> cls2file;
for (const auto& stu : students) {
if (cls2file.find(stu.cls) == cls2file.end()) {
cls2file[stu.cls].open(stu.cls + ".csv");
cls2file[stu.cls] << "考号,姓名,班级,总分,学校排名,班级排名,客观题总分,主观题总分,选择1,选择2,选择3,选择4,选择5,选择6,选择7,选择8,选择9,选择10,选择11,选择12,填空题17,填空题18,填空题19,填空题20,填空题21,填空题22" << endl;
}
cls2file[stu.cls] << stu.id << "," << stu.name << "," << stu.cls << ",," << stu.school_rank << "," << stu.cls_rank << "," << stu.obj_score << "," << stu.sub_score;
for (int ans : stu.obj_answers) {
cls2file[stu.cls] << "," << ans;
}
for (string ans : stu.sub_answers) {
cls2file[stu.cls] << "," << ans;
}
cls2file[stu.cls] << endl;
}
for (auto& kv : cls2file) {
kv.second.close();
}
}
// 将学生列表按照总分导出成csv文件
void export_csv_by_score(const vector<Student>& students) {
ofstream outfile("school.csv");
outfile << "考号,姓名,班级,总分,学校排名,班级排名,客观题总分,主观题总分,选择1,选择2,选择3,选择4,选择5,选择6,选择7,选择8,选择9,选择10,选择11,选择12,填空题17,填空题18,填空题19,填空题20,填空题21,填空题22" << endl;
for (const auto& stu : students) {
outfile << stu.id << "," << stu.name << "," << stu.cls << "," << stu.score << "," << stu.school_rank << ",," << stu.obj_score << "," << stu.sub_score;
for (int ans : stu.obj_answers) {
outfile << "," << ans;
}
for (string ans : stu.sub_answers) {
outfile << "," << ans;
}
outfile << endl;
}
outfile.close();
}
int main() {
vector<Student> students = read_data("data.txt");
sort_students_by_score(students);
calc_rank(students);
export_csv_by_cls(students);
export_csv_by_score(students);
return 0;
}
```
以上代码仅供参考,具体实现还需要根据实际情况进行调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)