请用c++ stl实现以下内容:读取data.txt数据,将其按照demo.xls的格式放入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格式导出学校的表格,完成后输出end。要求用c++ stl进行编写代码示例.
时间: 2023-07-21 14:41:18 浏览: 151
代码示例如下:
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
struct Student {
string id;
string name;
string cls;
int objScore[12];
int subScore[6];
int totalScore;
int schRank;
int clsRank;
Student() {
for (int i = 0; i < 12; i++) {
objScore[i] = 0;
}
for (int i = 0; i < 6; i++) {
subScore[i] = 0;
}
totalScore = 0;
schRank = 0;
clsRank = 0;
}
};
bool cmp(const Student& a, const Student& b) {
return a.totalScore > b.totalScore;
}
void readData(vector<Student>& students) {
ifstream fin("data.txt");
string line;
while (getline(fin, line)) {
stringstream ss(line);
Student s;
ss >> s.id >> s.name >> s.cls;
int sumObj = 0;
for (int i = 0; i < 12; i++) {
ss >> s.objScore[i];
sumObj += s.objScore[i];
}
s.objScore[12] = sumObj;
int sumSub = 0;
for (int i = 0; i < 6; i++) {
ss >> s.subScore[i];
sumSub += s.subScore[i];
}
s.subScore[6] = sumSub;
s.totalScore = sumObj + sumSub;
students.push_back(s);
}
fin.close();
}
void updateRank(vector<Student>& students) {
sort(students.begin(), students.end(), cmp);
int rank = 1;
map<string, int> schRankMap;
map<string, int> clsRankMap;
string lastSch = "";
string lastCls = "";
for (int i = 0; i < students.size(); i++) {
if (i == 0 || students[i].totalScore < students[i - 1].totalScore) {
rank = i + 1;
}
students[i].schRank = rank;
if (students[i].cls != lastCls) {
rank = 1;
}
students[i].clsRank = rank;
schRankMap[students[i].cls] += students[i].totalScore;
lastSch = students[i].cls;
lastCls = students[i].cls;
}
for (int i = 0; i < students.size(); i++) {
students[i].totalScore = schRankMap[students[i].cls];
}
}
void exportCsv(vector<Student>& students, string cls) {
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++) {
if (students[i].cls == cls) {
fout << students[i].id << "," << students[i].name << "," << students[i].cls << "," << students[i].totalScore << "," << students[i].schRank << "," << students[i].clsRank << ",";
for (int j = 0; j < 12; j++) {
fout << students[i].objScore[j] << ",";
}
for (int j = 0; j < 6; j++) {
fout << students[i].subScore[j] << ",";
}
fout << endl;
}
}
fout.close();
}
void exportSchCsv(vector<Student>& students) {
ofstream fout("school.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++) {
fout << students[i].id << "," << students[i].name << "," << students[i].cls << "," << students[i].totalScore << "," << students[i].schRank << "," << students[i].clsRank << ",";
for (int j = 0; j < 12; j++) {
fout << students[i].objScore[j] << ",";
}
for (int j = 0; j < 6; j++) {
fout << students[i].subScore[j] << ",";
}
fout << endl;
}
fout.close();
}
int main() {
vector<Student> students;
readData(students);
updateRank(students);
exportCsv(students, "class1");
exportCsv(students, "class2");
exportSchCsv(students);
cout << "end" << endl;
return 0;
}
```
阅读全文