读取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 09:41:07 浏览: 119
完美邮件读取,各种格式附件获取,完美DEMO
3星 · 编辑精心推荐
好的,下面是一个C++ STL的示例代码,实现了读取data数据,计算客观题总分和主观题总分,计算总分和排名,按照班级和学校进行排名和导出的功能。代码注释比较详细,希望对您有所帮助。
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
// 定义一个结构体存储学生信息
struct Student {
string id; // 考号
string name; // 姓名
string cls; // 班级
int objScore; // 客观题总分
int subScore; // 主观题总分
int totalScore; // 总分
int schoolRank; // 学校排名
int clsRank; // 班级排名
vector<int> objAns; // 客观题答案
vector<int> subAns; // 主观题答案
};
// 比较函数,用于根据总分排序
bool cmpTotalScore(const Student& s1, const Student& s2) {
return s1.totalScore > s2.totalScore;
}
// 比较函数,用于根据总分和班级排序
bool cmpClsTotalScore(const pair<string, int>& p1, const pair<string, int>& p2) {
return p1.second > p2.second;
}
int main() {
// 读取data数据
vector<Student> students;
ifstream fin("data.txt");
if (!fin) {
cout << "Failed to open file!" << endl;
return 0;
}
string line;
while (getline(fin, line)) {
stringstream ss(line);
Student stu;
ss >> stu.id >> stu.name >> stu.cls;
int score;
for (int i = 0; i < 12; i++) {
ss >> score;
stu.objAns.push_back(score);
stu.objScore += score;
}
for (int i = 0; i < 6; i++) {
ss >> score;
stu.subAns.push_back(score);
stu.subScore += score;
}
stu.totalScore = stu.objScore + stu.subScore;
students.push_back(stu);
}
fin.close();
// 计算排名
sort(students.begin(), students.end(), cmpTotalScore);
int rank = 1;
for (int i = 0; i < students.size(); i++) {
if (i > 0 && students[i].totalScore != students[i - 1].totalScore) {
rank = i + 1;
}
students[i].schoolRank = rank;
}
map<string, vector<Student>> cls2students;
for (int i = 0; i < students.size(); i++) {
cls2students[students[i].cls].push_back(students[i]);
}
for (auto it = cls2students.begin(); it != cls2students.end(); it++) {
sort(it->second.begin(), it->second.end(), cmpTotalScore);
rank = 1;
for (int i = 0; i < it->second.size(); i++) {
if (i > 0 && it->second[i].totalScore != it->second[i - 1].totalScore) {
rank = i + 1;
}
it->second[i].clsRank = rank;
}
}
// 导出数据到csv文件
// 按班级导出
for (auto it = cls2students.begin(); it != cls2students.end(); it++) {
string cls = it->first;
vector<Student>& students = it->second;
sort(students.begin(), students.end(), cmpClsTotalScore);
ofstream fout(cls + ".csv");
fout << "考号,姓名,班级,总分,学校排名,班级排名,客观题总分,主观题总分,选择1,选择2,选择3,选择4,选择5,选择6,选择7,选择8,选择9,选择10,选择11,选择12,填空题17,填空题18,填空题19,填空题20,填空题21,填空题22" << endl;
for (int i = 0; i < students.size(); i++) {
Student& stu = students[i];
fout << stu.id << "," << stu.name << "," << stu.cls << ",";
fout << "," << stu.schoolRank << "," << stu.clsRank << ",";
fout << stu.objScore << "," << stu.subScore << ",";
for (int j = 0; j < stu.objAns.size(); j++) {
fout << stu.objAns[j] << ",";
}
for (int j = 0; j < stu.subAns.size(); j++) {
fout << stu.subAns[j] << ",";
}
fout << endl;
}
fout.close();
}
// 按学校导出
map<string, int> school2score;
for (int i = 0; i < students.size(); i++) {
school2score[students[i].cls.substr(0, 2)] += students[i].totalScore;
}
vector<pair<string, int>> schools;
for (auto it = school2score.begin(); it != school2score.end(); it++) {
schools.push_back(make_pair(it->first, it->second));
}
sort(schools.begin(), schools.end(), cmpClsTotalScore);
ofstream fout("school.csv");
fout << "学校,总分" << endl;
for (int i = 0; i < schools.size(); i++) {
fout << schools[i].first << "," << schools[i].second << endl;
}
fout.close();
return 0;
}
```
阅读全文