请用较老版本的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 16:43:22 浏览: 49
首先,我们需要定义一个结构体来存储每个考生的信息:
```c++
struct Student {
string id; // 考号
string name; // 姓名
string cls; // 班级
int total_score; // 总分
int sch_rank; // 学校排名
int cls_rank; // 班级排名
int obj_score; // 客观题总分
int subj_score; // 主观题总分
int choices[12]; // 选择题得分
int blanks[6]; // 填空题得分
};
```
然后,我们需要读取数据并将其存储到一个 vector 中:
```c++
vector<Student> students;
ifstream fin("data.txt");
string line;
while (getline(fin, line)) {
istringstream iss(line);
Student s;
iss >> s.id >> s.name >> s.cls;
// 空出总分、客观题总分、主观题总分
for (int i = 0; i < 3; i++) iss >> line;
// 读取选择题得分
for (int i = 0; i < 12; i++) iss >> s.choices[i];
// 读取填空题得分
for (int i = 0; i < 6; i++) iss >> s.blanks[i];
students.push_back(s);
}
```
接下来,我们需要计算每个考生的客观题总分和主观题总分,并将其累加到对应的总分中:
```c++
for (auto& s : students) {
// 计算客观题总分
for (int i = 0; i < 12; i++) s.obj_score += s.choices[i];
// 计算主观题总分
for (int i = 0; i < 6; i++) s.subj_score += s.blanks[i];
// 计算总分
s.total_score = s.obj_score + s.subj_score;
}
```
最后,我们需要按照要求将数据输出到 demo 中:
```c++
ofstream fout("demo.txt");
fout << "考号 姓名 班级 总分 学校排名 班级排名 客观题总分 主观题总分 选择1 选择2 选择3 选择4 选择5 选择6 选择7 选择8 选择9 选择10 选择11 选择12 填空题 17 18 19 20 21 22" << endl;
// 按照总分排序
sort(students.begin(), students.end(), [](const Student& s1, const Student& s2) {
return s1.total_score > s2.total_score;
});
int rank = 0, pre_score = -1;
for (int i = 0; i < students.size(); i++) {
auto& s = students[i];
fout << s.id << " " << s.name << " " << s.cls << " " << s.total_score << " ";
// 计算学校排名和班级排名
if (s.total_score != pre_score) {
rank = i + 1;
pre_score = s.total_score;
}
s.sch_rank = rank;
rank = 0, pre_score = -1;
for (int j = 0; j < students.size(); j++) {
if (students[j].cls == s.cls) {
if (students[j].total_score != pre_score) {
rank = j + 1;
pre_score = students[j].total_score;
}
if (students[j].id == s.id) s.cls_rank = rank;
}
}
fout << s.sch_rank << " " << s.cls_rank << " " << s.obj_score << " " << s.subj_score << " ";
for (int j = 0; j < 12; j++) fout << s.choices[j] << " ";
for (int j = 0; j < 6; j++) fout << s.blanks[j] << " ";
fout << endl;
}
```
完整代码如下:
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)