#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
//定义时间日期类表示学生的出生年月日
class Date
{
public:
void SetDate () //设置年月
{
cout<<"请输入出生年份:";
cin>>year;
cout<<"请输入出生月份:";
cin>>month;
cout<<"请输入出生日:";
cin>>day;
}
int GetYear()
{return year;}//获取年份
int GetMonth()
{return month;}//获取月
int GetDay()
{return day;} //获取日
void SetBirthDate()
{SetDate();}
private:
int year,month,day;
};
//定义学生类,包含学生的学号、姓名、出生年月日
class Student
{
public:
void SetStuValues (Date &birthday)
{
cout<<"请输入学生的学号:";
cin>>num;
cout<<"请输入学生的姓名:";
cin>>name;
cout<<"请输入学生的性别(M/F):";
cin>>gender;
birthday.SetDate ();
cout<<"请输入学生的第一门课成绩:";
cin>>score1;
cout<<"请输入学生的第二门课成绩:";
cin>>score2;
cout<<"请输入学生的第三门课成绩:";
cin>>score3;
average=(score1+score2+score3)/3.0;
minscore=min({score1,score2,score3});
maxscore=max({score1,score2,score3});
cout<<"最高分为:"<<maxscore<<endl;
cout<<"最低分为:"<<minscore<<endl;
cout<<"最平均分为:"<<average<<endl;
}
void SetBirthday (Date &birthday) //设置年月日
{
birthday.SetDate();
}
int GetBirYear (Date &birthday) //获得年
{return birthday.GetYear();
}
int GetBirMonth (Date &birthday) //获得月
{
return birthday.GetMonth();
}
int GetBirDay (Date &birthday) //获得日
{
return birthday.GetDay();
}
void InputStudent (Date &birthday)
{
char choice;
cout<<"输入学生记录:"<<endl;
ofstream outfile ("D:\\DEV\\abc.txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<num<<" "<<name<<" "<<gender<<" "
<<birthday.GetYear() <<" "<<birthday.GetMonth ()<<" "<<birthday.GetDay ()<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
cout<<"是否继续输入学生信息输入y(继续)或者n(停止):";
cin>>choice;
if(choice=='y')
{
InputStudent (birthday);
}
outfile.close ();
system ("pause");
}
//2.浏览全部学生记录
void OutputStudent ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
infile. get ();
if (infile.peek ()=='\n') break;
}
infile.close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号:";
cin>>findnum;
ifstream infile ("D:\\DEV\\abc.txt" , ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num ==findnum)
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
find = true;
}
infile.get ( );
if (infile.peek ()=='\n') break;
}
if(! find)
{cout<<"查无此人"<<endl;
}
infile.close ();
system ("pause");
}
//4. 按学号删除学生记录
void DeleteByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号:";
cin>>findnum;
ifstream infile ("D:\\DEV\\student.txt", ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt", ios::app);
if(! outfile) //打开文件
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num != findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile.peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("D:\\DEV\\abc.txt" ) ; //删除文件
rename ("temp.txt" , "D:\\DEV\\abc.txt" ) ;
}
//5.按学号修改学生的出生日期
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day;
int findnum;
int alteryear;
int altermonth;
int alterday;
bool find = false;
cout<<"请输入要修改学生的学号:";
cin>>findnum;
cout<<"请输入要修改学生的出生日期:";
cin>>alteryear>>altermonth>>alterday;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<score1<<" "<<
score2<<" "<<score3<<" "<<endl;
find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" D:\\DEV\\abc.txt" ) ; //删除文件
rename ("temp.txt", "D:\\DEV\\abc.txt");
}
//6.按学号修改学生的出生日期
void AlterScoreByNum ()
{
int count =0;
int findnum;
int alteryear;
int altermonth;
int alterday;
int newScore1;
int newScore2;
int newScore3;
bool find = false;
cout<<"请输入要修改学生的学号:";
cin>>findnum;
cout<<"请输入要修改学生的成绩:";
cin>>newScore1>>newScore2>>newScore3;
newaverage=(newScore1+newScore2+newScore3)/3.0;
newmin=min({newScore1,newScore2,newScore3});
newmax=max({newScore1,newScore2,newScore3});
cout<<"最高分为:"<<newmax<<endl;
cout<<"最低分为:"<<newmin<<endl;
cout<<"最平均分为:"<<newaverage<<endl;
fstream file ("D:\\DEV\\abc.txt");
if(!file)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (!file.eof ())
{
file>>num>>name>>gender>>alteryear>>altermonth>>alterday>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<newaverage<<" "<<newmax<<" "<<newmin<<" "<<endl;
}
else
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<
newScore1<<" "<<newScore2<<" "<<newScore3<<" "<<endl;
find = true;
cout<<"已经修改该学生"<<endl;
}
file.get ();
if (file.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
file.close ();
outfile.close ();
remove ("D:\\DEV\\abc.txt");
rename ("temp.txt", "D:\\DEV\\abc.txt");
}
private:
int num;
string name;
Date birthday;
int minscore;
int maxscore;
double average;
int newmax;
int newmin;
double newaverage;
char gender;
int score1,score2,score3;
};
void MainShow ()
{
cout<<"\t\t ***学生信息系统***"<<endl;
cout<<"\t\t ***1.输入学生记录**"<<endl;
cout<<"\t\t ***2.浏览全部学生记录***"<<endl;
cout<<"\t\t ***3.按学号查找学生记录**"<<endl;
cout<<"\t\t ***4.按学号删除学生记录***"<<endl;
cout<<"\t\t ***5.按学号修改学生的出生日期*** "<<endl;
cout<<"\t\t ***6.按学号修改学生的成绩*** "<<endl;
cout<<"\t\t ***7.退出系统***"<<endl;
}
void Select()
{
int num;
Student stu;
Date birthday;
cout<<"请选择功能:";
cin>>num;
switch(num)
{
case 1:
stu.InputStudent (birthday);
Select ();
break;
case 2:
stu.OutputStudent ();
Select ();
break;
case 3:
stu.FindByNum ();
Select ();
break;
case 4:
stu.DeleteByNum ();
Select ();
break;
case 5:
stu.AlterNameByNum();
Select ();
break;
case 6:
stu.AlterScoreByNum();
Select ();
break;
case 7:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}
为什么选择功能2,输出的结果是这样,为什么不是输出平均分,最高分,最低分