From d5990a02b7eb4d710edd6f7a54e8b5f74de38142 Mon Sep 17 00:00:00 2001 From: pnperct9y Date: Sun, 23 Jun 2024 12:42:06 +0800 Subject: [PATCH] ADD file via upload --- main.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e929cb1 --- /dev/null +++ b/main.cpp @@ -0,0 +1,63 @@ +#include +#include +#include + +using namespace std; + +void InputStudentInfo(vector> &students) { + string name, gender; + double score; + cout << "该系统只支持百分制成绩的平均分计算"<> name >> gender >> score) { + if (name != "end") { + students.push_back(make_pair(name, score)); + } else { + break; + } + cout << "请继续输入学生的姓名、性别和分数(输入'end'结束输入): "; + } +} + +void CalculateAverageScore(const vector> &students, vector &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 &averages) { + cout << "男性学生的平均分数为: " << averages[0] / averages[1] << endl; + cout << "女性学生的平均分数为: " << averages[2] / averages[3] << endl; +} + +void SaveToFile(const vector &averages, const string &filename) { + ofstream outfile(filename); + outfile << "男性学生的平均分数为: " << averages[0] / averages[1] << endl; + outfile << "女性学生的平均分数为: " << averages[2] / averages[3] << endl; + outfile.close(); +} + +int main() { + vector> students; + vector averages(4, 0); // 分别为男性平均分、男性人数、女性平均分、女性人数 + + InputStudentInfo(students); + CalculateAverageScore(students, averages); + OutputAverageScore(averages); + SaveToFile(averages, "student_stats.txt"); + + return 0; +}