ADD file via upload

main
pnperct9y 5 months ago
parent 4fd37c0ec1
commit d5990a02b7

@ -0,0 +1,63 @@
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void InputStudentInfo(vector<pair<string, double>> &students) {
string name, gender;
double score;
cout << "该系统只支持百分制成绩的平均分计算"<<endl;
cout << "请输入学生的姓名、性别和分数(输入'end'结束输入): ";
while (cin >> name >> gender >> score) {
if (name != "end") {
students.push_back(make_pair(name, score));
} else {
break;
}
cout << "请继续输入学生的姓名、性别和分数(输入'end'结束输入): ";
}
}
void CalculateAverageScore(const vector<pair<string, double>> &students, vector<double> &averages) {
for (const auto &student : students) {
if (student.second >= 60) {
// 假定性别为'm'表示男性,'f'表示女性
if (student.first[0] == 'm') {
averages[0] += student.second;
averages[1]++;
} else if (student.first[0] == 'f') {
averages[2] += student.second;
averages[3]++;
} else {
cout << "未知性别,跳过该学生。" << endl;
}
} else {
cout << "分数低于60分跳过该学生。" << endl;
}
}
}
void OutputAverageScore(const vector<double> &averages) {
cout << "男性学生的平均分数为: " << averages[0] / averages[1] << endl;
cout << "女性学生的平均分数为: " << averages[2] / averages[3] << endl;
}
void SaveToFile(const vector<double> &averages, const string &filename) {
ofstream outfile(filename);
outfile << "男性学生的平均分数为: " << averages[0] / averages[1] << endl;
outfile << "女性学生的平均分数为: " << averages[2] / averages[3] << endl;
outfile.close();
}
int main() {
vector<pair<string, double>> students;
vector<double> averages(4, 0); // 分别为男性平均分、男性人数、女性平均分、女性人数
InputStudentInfo(students);
CalculateAverageScore(students, averages);
OutputAverageScore(averages);
SaveToFile(averages, "student_stats.txt");
return 0;
}
Loading…
Cancel
Save