#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
enum class Gender { Male, Female };
// 定义分数类表示学生的单科成绩
class Score {
public:
string course;
int grade;
Score(string c, int g) : course(c), grade(g) {}
};
// 更新时间日期类表示学生的出生年月日和性别
class Date {
public:
void SetDate() {
cout << "请输入出生年份:" << endl;
cin >> year;
cout << "请输入出生月份:" << endl;
cin >> month;
cout << "请输入出生日:" << endl;
cin >> day;
}
int GetYear() const {
return year;
}
int GetMonth() const {
return month;
}
int GetDay() const {
return day;
}
void SetGender(Gender gender) {
this->gender = gender;
}
Gender GetGender() const {
return gender;
}
private:
int year;
int month;
int day;
Gender gender;
};
// 更新学生类,包含学生的学号、姓名、出生年月日和性别,以及三门课成绩
class Student {
public:
void SetStuValues(Date &birthday) {
cout << "请输入学生的学号:" << endl;
cin >> num;
cout << "请输入学生的姓名:" << endl;
cin >> name;
birthday.SetDate();
cout << "请输入学生的性别(M/F):" << endl;
char genderChoice;
cin >> genderChoice;
if (genderChoice == 'M' || genderChoice == 'm')
birthday.SetGender(Gender::Male);
else if (genderChoice == 'F' || genderChoice == 'f')
birthday.SetGender(Gender::Female);
else {
cout << "无效的性别选择,默认为男" << endl;
birthday.SetGender(Gender::Male);
}
}
void AddScore(Score score) {
scores.push_back(score);
}
double CalculateAverageGrade() const {
double sum = 0;
for (const auto &score : scores) {
sum += score.grade;
}
return sum / scores.size();
}
int GetMaxGrade() const {
int maxGrade = scores[0].grade;
for (const auto &score : scores) {
if (score.grade > maxGrade) {
maxGrade = score.grade;
}
}
return maxGrade;
}
int GetMinGrade() const {
int minGrade = scores[0].grade;
for (const auto &score : scores) {
if (score.grade < minGrade) {
minGrade = score.grade;
}
}
return minGrade;
}
// 更新学生类,包含学生的学号、姓名、出生年月日和性别,以及三门课成绩
class Student {
public:
void SetStuValues(Date &birthday) {
cout << "请输入学生的学号:" << endl;
cin >> num;
cout << "请输入学生的姓名:" << endl;
cin >> name;
birthday.SetDate();
cout << "请输入学生的性别(M/F):" << endl;
char genderChoice;
cin >> genderChoice;
if (genderChoice == 'M' || genderChoice == 'm')
birthday.SetGender(Gender::Male);
else if (genderChoice == 'F' || genderChoice == 'f')
birthday.SetGender(Gender::Female);
else {
cout << "无效的性别选择,默认为男" << endl;
birthday.SetGender(Gender::Male);
}
}
void AddScore(Score score) {
scores.push_back(score);
}
double CalculateAverageGrade() const {
double sum = 0;
for (const auto &score : scores) {
sum += score.grade;
}
return sum / scores.size();
}
int GetMaxGrade() const {
int maxGrade = scores[0].grade;
for (const auto &score : scores) {
if (score.grade > maxGrade) {
maxGrade = score.grade;
}
}
return maxGrade;
}
int GetMinGrade() const {
int minGrade = scores[0].grade;
for (const auto &score : scores) {
if (score.grade < minGrade) {
minGrade = score.grade;
}
}
return minGrade;
}
void OutputStudent() {
int count = 0;
int year;
int month;
int day;
ifstream infile("student.txt", ios::in);
if (!infile) {
cout << "文件打开失败" << endl;
}
while (!infile.eof()) {
infile >> num >> name >> year >> month >> day;
cout << num << "" << name << "" << year << "" << month << ""
<< day << endl;
int gradeSum = 0;
int gradeCount = 0;
while (!infile.eof()) {
string course;
int grade;
infile >> course >> grade;
gradeSum += grade;
gradeCount++;
if (infile.peek() == '\n') break;
}
cout << "平均分: " << gradeSum / gradeCount << ", 最高分: "
<< GetMaxGrade() << ", 最低分: " << GetMinGrade() << endl;
infile.get();
if (infile.peek() == '\n') break;
}
infile.close();
system("pause");
}
void FindByNum() {
int count = 0;
int year;
int month;
int day;
int findnum;
bool found = false;
cout << "请输入要查找学生的学号:" << endl;
cin >> findnum;
ifstream infile("student.txt", ios::in);
if (!infile) {
cout << "文件打开失败" << endl;
}
while (!infile.eof()) {
infile >> num >> name >> year >> month >> day;
if (num == findnum) {
cout << num << "" << name << "" << year << "" << month << ""
<< day << endl;
cout << "平均分: " << CalculateAverageGrade() << ", 最高分: "
<< GetMaxGrade() << ", 最低分: " << GetMinGrade() << endl;
found = true;
}
infile.get();
if (infile.peek() == '\n') break;
}
if (!found) {
cout << "查无此人" << endl;
}
infile.close();
system("pause");
}
void DeleteByNum() {
int count = 0;
int year;
int month;
int day;
int findnum;
bool found = false;
cout << "请输入要删除学生的学号:" << endl;
cin >> findnum;
ifstream infile("student.txt", ios::in);
if (!infile) {
cout << "文件打开失败" << endl;
}
ofstream outfile("studentcopy.txt", ios::app);
if (!outfile) { // 打开文件
cout << "文件打开失败" << endl;
exit(0);
}
while (!infile.eof()) {
infile >> num >> name >> year >> month >> day;
if (num != findnum) {
outfile << num << "" << name << "" << year << "" << month << ""
<< day << "";
for (const auto &score : scores) {
outfile << score.course << "" << score.grade << endl;
}
} else {
cout << num << "" << name << "" << year << "" << month << ""
<< day << endl;
found = true;
cout << "已经删除该学生" << endl;
}
infile.get();
if (infile.peek() == '\n') break;
}
if (!found) {
cout << "查无此人" << endl;
}
infile.close();
outfile.close();
remove("student.txt"); // 删除文件
rename("studentcopy.txt", "student.txt");
}
void UpdateGradeByNum(int targetCourse, int newGrade) {
int findnum;
cout << "请输入要修改成绩的学生学号:" << endl;
cin >> findnum;
cout << "请输入要修改的课程名称:" << endl;
string targetCourseStr;
cin >> targetCourseStr;
for (auto &score : scores) {
if (score.course == targetCourseStr) {
score.grade = newGrade;
cout << "已将" << targetCourseStr << "的成绩修改为" << newGrade
<< endl;
break;
}
}
OutputStudent();
}
private:
int num;
string name;
Date birthdate;
vector<Score> scores;
};
void MainShow() {
cout << "欢迎使用学生管理系统" << endl;
cout << "1. 输入学生信息" << endl;
cout << "2. 浏览所有学生记录" << endl;
cout << "3. 按学号查找学生记录" << endl;
cout << "4. 按学号删除学生记录" << endl;
cout << "5. 按学号修改学生的出生日期" << endl;
cout << "6. 按学号修改学生某门课的成绩" << endl;
cout << "7. 系统退出" << endl;
}
void Select() {
int choice;
cout << "请选择操作:" << endl;
cin >> choice;
根据这个代码继续完成图片的要求