class Student
{
public:
void InputStudent(Date &birthday)
{
ofstream outfile("student.txt", ios::app);
cout << "请输入学号:";
cin >> num;
cout << "请输入姓名:";
cin >> name;
birthday.SetDate();
cout << "请输入性别:";
cin >> gender;
cout << "请输入3门课成绩:";
cin >> score1 >> score2 >> score3;
double average = (score1 + score2 + score3) / 3.0;
int maxscore = max({score1, score2, score3});
int minscore = min({score1, score2, score3});
outfile << num << " " << name << " " << birthday.GetYear() << " " << birthday.GetMonth()
<< " " << birthday.GetDay()<< " " << gender << " " << score1 << " " << score2 << " " <<
score3 << " " << average << " " << maxscore << " " << minscore << endl;
cout << "学生信息录入成功!" << endl;
outfile.close();
}
//2.浏览学生全部信息
void OutputStudents()
{
ifstream infile("student.txt");
string line;
while (getline(infile, line))
{
cout << line << endl;
}
infile.close();
}
//3.按学号查找学生记录
void FindByNum(int findnum)
{
ifstream infile("student.txt");
int num;
string line;
bool found = false;
while (infile >> num)
{
if (num == findnum)
{
getline(infile, line);
cout << num << " " << line << endl;
found = true;
break;
}
getline(infile, line);
}
infile.close();
if (!found)
{
cout << "未找到学号为 " << findnum << " 的学生" << endl;
}
}
//4.按学号删除学生记录
void DeleteByID(int findnum)
{
fstream file("student.txt");
ofstream temp("temp.txt");
string line;
int num;
bool found = false;
while (file >> num)
{
getline(file, line);
if (num != findnum)
{
temp << num << " " << line << endl;
}
else
{
found = true;
}
}
file.close();
temp.close();
if (found)
{
remove("student.txt");
rename("temp.txt", "student.txt");
cout << "已成功删除学号为 " << findnum << " 的学生记录!" << endl;
}
else
{
cout << "未找到学号为 " << findnum << " 的学生记录。" << endl;
}
}
//5.按学号修改出生日期
void AlterDateByNum(int findnum)
{
fstream file("student.txt");
ofstream temp("temp.txt");
int num;
string name, gender, line;
int year, month, day, score1, score2, score3;
double average;
bool found = false;
while (file >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average)
{
if (num == findnum)
{
Date newDate;
newDate.SetDate();
temp << num << " " << name << " " << newDate.GetYear() << " " << newDate.GetMonth() << " " << newDate.GetDay()
<< " " << gender << " " << score1 << " " << score2 << " " << score3 << " " << average << endl;
found = true;
cout << "已成功修改学号为 " << num << " 的学生的出生日期!" << endl;
}
else
{
temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " "
<< score1 << " " << score2 << " " << score3 << " " << average << endl;
}
}
file.close();
temp.close();
remove("student.txt");
rename("temp.txt", "student.txt");
if (!found)
{
cout << "未找到学号为 " << findnum << " 的学生" << endl;
}
}
//6. 按学号修改学生成
void AlterScoreByNum(int findnum)
{
fstream file("student.txt");
ofstream temp("temp.txt");
int num;
string name, gender, line;
int year, month, day, score1, score2, score3;
double average;
bool found = false;
while (file >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average)
{
if (num == findnum)
{
int newScore1, newScore2, newScore3;
cout << "请输入新的三门课成绩: ";
cin >> newScore1 >> newScore2 >> newScore3;
double newAverage = (newScore1 + newScore2 + newScore3) / 3.0;
int newMax = max({newScore1, newScore2, newScore3});
int newMin = min({newScore1, newScore2, newScore3});
temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " "
<< newScore1 << " " << newScore2 << " " << newScore3 << " " <<
newAverage << " " << newMax << " " << newMin << endl;
found = true;
cout << "已成功修改学号为 " << num << " 的学生成绩!" << endl;
}
else
{
temp << num << " " << name << " " << year << " " << month << " " << day << " " << gender << " "
<< score1 << " " << score2 << " " << score3 << " " << average << endl;
}
}
file.close();
temp.close();
remove("student.txt");
rename("temp.txt", "student.txt");
if (!found)
{
cout << "未找到学号为 " << findnum << " 的学生" << endl;
}
}
private:
int num;
string name;
string gender;
int score1, score2, score3;
};
每一行代码的注释,特别是关于文件部分的