请用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一行中的数据相加填入主观题总分中,之后每一行的主观题总分和客观题总分相加填入对应行的总分中
时间: 2023-07-21 22:43:22 浏览: 92
可以参考以下代码实现:
```cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
// 定义数据结构
struct Student {
string id;
string name;
string class_num;
int total_score = 0;
int school_rank = 0;
int class_rank = 0;
int objective_score = 0;
int subjective_score = 0;
vector<int> objective_answers;
vector<string> subjective_answers;
};
// 定义常量
const int NUM_OF_OBJECTIVE_QUESTIONS = 12;
const int NUM_OF_SUBJECTIVE_QUESTIONS = 6;
// 读取数据文件
vector<Student> read_data_file(string filename) {
vector<Student> students;
ifstream ifs(filename);
if (!ifs) {
cerr << "Failed to open file: " << filename << endl;
return students;
}
string line;
while (getline(ifs, line)) {
istringstream iss(line);
string id, name, class_num;
iss >> id >> name >> class_num;
Student stu;
stu.id = id;
stu.name = name;
stu.class_num = class_num;
int score;
for (int i = 0; i < NUM_OF_OBJECTIVE_QUESTIONS; i++) {
iss >> score;
stu.objective_answers.push_back(score);
stu.objective_score += score;
}
string answer;
for (int i = 0; i < NUM_OF_SUBJECTIVE_QUESTIONS; i++) {
iss >> answer;
stu.subjective_answers.push_back(answer);
}
iss >> stu.total_score;
iss >> stu.school_rank;
iss >> stu.class_rank;
students.push_back(stu);
}
ifs.close();
return students;
}
// 将数据按照 demo 的格式输出到文件
void write_demo_file(string filename, const vector<Student>& students) {
ofstream ofs(filename);
if (!ofs) {
cerr << "Failed to write file: " << filename << endl;
return;
}
ofs << "考号 姓名 班级 总分 学校排名 班级排名 客观题总分 主观题总分 选择1 选择2 选择3 选择4 选择5 选择6 选择7 选择8 选择9 选择10 选择11 选择12 填空题 17 18 19 20 21 22" << endl;
for (const auto& stu : students) {
ofs << stu.id << " " << stu.name << " " << stu.class_num << " ";
ofs << " "; // 空出总分
ofs << stu.school_rank << " " << stu.class_rank << " ";
ofs << stu.objective_score << " " << stu.subjective_score << " ";
for (const auto& ans : stu.objective_answers) {
ofs << ans << " ";
}
ofs << stu.subjective_answers[0] << " ";
ofs << stu.subjective_answers[1] << " ";
ofs << stu.subjective_answers[2] << " ";
ofs << stu.subjective_answers[3] << " ";
ofs << stu.subjective_answers[4] << " ";
ofs << stu.subjective_answers[5] << " ";
ofs << endl;
}
ofs.close();
}
int main() {
// 读取数据文件
string data_filename = "data.txt";
vector<Student> students = read_data_file(data_filename);
// 计算主观题总分
for (auto& stu : students) {
stu.subjective_score = accumulate(stu.subjective_answers.begin(), stu.subjective_answers.end(), 0);
}
// 计算总分并按照总分排序
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.objective_score + a.subjective_score > b.objective_score + b.subjective_score;
});
for (int i = 0; i < students.size(); i++) {
students[i].total_score = students[i].objective_score + students[i].subjective_score;
}
// 计算学校排名和班级排名
vector<int> school_scores;
for (const auto& stu : students) {
school_scores.push_back(stu.total_score);
}
sort(school_scores.begin(), school_scores.end(), greater<int>());
school_scores.erase(unique(school_scores.begin(), school_scores.end()), school_scores.end()); // 去重
for (auto& stu : students) {
auto it = lower_bound(school_scores.begin(), school_scores.end(), stu.total_score, greater<int>());
stu.school_rank = distance(school_scores.begin(), it) + 1;
}
for (int i = 1; i <= 3; i++) { // 假设有 3 个班级
vector<int> class_scores;
for (const auto& stu : students) {
if (stu.class_num == to_string(i)) {
class_scores.push_back(stu.total_score);
}
}
sort(class_scores.begin(), class_scores.end(), greater<int>());
class_scores.erase(unique(class_scores.begin(), class_scores.end()), class_scores.end()); // 去重
for (auto& stu : students) {
if (stu.class_num == to_string(i)) {
auto it = lower_bound(class_scores.begin(), class_scores.end(), stu.total_score, greater<int>());
stu.class_rank = distance(class_scores.begin(), it) + 1;
}
}
}
// 将数据按照 demo 的格式输出到文件
string demo_filename = "demo.txt";
write_demo_file(demo_filename, students);
return 0;
}
```
阅读全文