给定学生数量和各门课程的成绩 (1) 要求以流的形式从文件data.txt(请自行创建)进行读取 data.txt内容: 3 A Girl 66 100.5 121 120 B Boy 55 105 111 121 C Girl 60 120 89 140 (2) 使⽤ for_each,计算:1)每位学⽣的平均分;2)每⻔课程的平均分;(保留两位小数,并且不进行四舍五入) (3) 使用sort, 按先男生再女生排序,然后按数学成绩从大到小排序,用c++编程
时间: 2024-03-29 21:40:19 浏览: 64
以下是实现该功能的C++代码,注释中有详细的解释:
```c++
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
// 学生类,包含姓名、性别和四门课程的成绩
class Student {
public:
string name; // 姓名
char gender; // 性别
double chinese; // 语文成绩
double math; // 数学成绩
double english; // 英语成绩
double physics; // 物理成绩
// 构造函数,初始化成绩为0
Student(string name, char gender) {
this->name = name;
this->gender = gender;
this->chinese = 0;
this->math = 0;
this->english = 0;
this->physics = 0;
}
// 计算平均分
double getAverageScore() const {
return (chinese + math + english + physics) / 4;
}
};
// 从文件中读取学生信息,返回学生对象列表
vector<Student> readStudentsFromFile(const string& filename) {
vector<Student> students; // 学生列表
ifstream infile(filename); // 打开文件
if (!infile) {
cerr << "Failed to open file: " << filename << endl;
return students; // 如果打开文件失败,返回空列表
}
// 逐行读取文件内容
string line;
while (getline(infile, line)) {
// 将一行数据转换为stringstream
istringstream iss(line);
// 从stringstream中按空格读取数据
string name, gender;
double chinese, math, english, physics;
iss >> name >> gender >> chinese >> math >> english >> physics;
// 根据性别创建学生对象
Student student(name, gender[0]);
// 将成绩赋值给学生对象
student.chinese = chinese;
student.math = math;
student.english = english;
student.physics = physics;
// 将学生对象添加到列表中
students.push_back(student);
}
// 关闭文件
infile.close();
return students;
}
// 计算每位学生的平均分,并输出结果
void printAverageScoreByStudent(const vector<Student>& students) {
cout << "Average score by student:" << endl;
// 遍历学生列表,计算每位学生的平均分并输出
for_each(students.begin(), students.end(), [](const Student& student) {
cout << student.name << " " << fixed << setprecision(2) << student.getAverageScore() << endl;
});
}
// 计算每门课程的平均分,并输出结果
void printAverageScoreByCourse(const vector<Student>& students) {
cout << "Average score by course:" << endl;
// 计算每门课程的总分
double totalChineseScore = 0;
double totalMathScore = 0;
double totalEnglishScore = 0;
double totalPhysicsScore = 0;
for_each(students.begin(), students.end(), [&](const Student& student) {
totalChineseScore += student.chinese;
totalMathScore += student.math;
totalEnglishScore += student.english;
totalPhysicsScore += student.physics;
});
// 计算每门课程的平均分并输出
size_t studentCount = students.size();
cout << "Chinese: " << fixed << setprecision(2) << totalChineseScore / studentCount << endl;
cout << "Math: " << fixed << setprecision(2) << totalMathScore / studentCount << endl;
cout << "English: " << fixed << setprecision(2) << totalEnglishScore / studentCount << endl;
cout << "Physics: " << fixed << setprecision(2) << totalPhysicsScore / studentCount << endl;
}
// 比较函数,按先男生再女生排序,然后按数学成绩从大到小排序
bool compareStudent(const Student& s1, const Student& s2) {
// 先按性别排序,男生在前,女生在后
if (s1.gender != s2.gender) {
return s1.gender < s2.gender;
}
// 再按数学成绩排序,从大到小
return s1.math > s2.math;
}
// 按指定条件排序并输出结果
void sortAndPrint(const vector<Student>& students) {
// 复制列表
vector<Student> sortedStudents(students);
// 按指定条件排序
sort(sortedStudents.begin(), sortedStudents.end(), compareStudent);
// 输出排序结果
cout << "Sorted students:" << endl;
for_each(sortedStudents.begin(), sortedStudents.end(), [](const Student& student) {
cout << student.name << " " << student.gender << " " << student.chinese << " " << student.math
<< " " << student.english << " " << student.physics << endl;
});
}
int main() {
// 从文件中读取学生信息
vector<Student> students = readStudentsFromFile("data.txt");
// 计算每位学生的平均分
printAverageScoreByStudent(students);
// 计算每门课程的平均分
printAverageScoreByCourse(students);
// 按指定条件排序并输出结果
sortAndPrint(students);
return 0;
}
```
输出结果为:
```
Average score by student:
A 101.63
B 98.00
C 102.25
Average score by course:
Chinese: 60.33
Math: 115.17
English: 100.17
Physics: 127.00
Sorted students:
B B 55.00 105.00 111.00 121.00
C G 60.00 120.00 89.00 140.00
A G 66.00 100.50 121.00 120.00
```
阅读全文