#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)或(f/m):";
cin>>gender;
birthday.SetDate ();
cout<<"请输入学生3门课成绩:";
cin>>score1>>score2>>score3;
average=(score1+score2+score3)/3.0;
minscore=min({score1,score2,score3});
maxscore=max({score1,score2,score3});
}
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<<" "<<birthday.GetYear() <<" "<<birthday.GetMonth ()<<" "<<birthday.GetDay ()
<<" "<<gender<<" "<<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;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day>>gender>>score1>>score2>>score3>>average>>maxscore>>minscore;
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
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 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>>year>>month>>day>>score1>>score2>>score3>>gender>>average>>maxscore>>minscore;
if (num ==findnum)
{
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
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 findnum;
bool find = false;
cout<<"请输入要删除学生的学号:";
cin>>findnum;
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>>year>>month>>day>>score1>>score2>>score3>>gender>>average>>maxscore>>minscore;
if (num != findnum)
{
outfile <<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<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 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>>year>>month>>day>>gender>>score1>>score2>>score3>>average>>maxscore>>minscore;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{
outfile <<num<<" "<<name<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<average<<" "<<maxscore<<" "<<minscore<<" "<<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 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});
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>>alteryear>>altermonth>>alterday>>gender>>score1>>score2>>score3>>newaverage>>newmax>>newmin;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<newaverage<<" "<<newmax<<" "<<newmin<<" "<<endl;
}
else
{
outfile <<num<<" "<<name<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<gender<<" "<<
newScore1<<" "<<newScore2<<" "<<newScore3<<" "<<newaverage<<" "<<newmax<<" "<<newmin<<" "<<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;
char gender;
int newmax;
int newmin;
int minscore;
int maxscore;
double average;
double newaverage;
int score1,score2,score3;
};
void MainShow ()
{
cout<<"\t学生信息系统"<<endl;
cout<<"\t1.输入学生记录"<<endl;
cout<<"\t2.浏览全部学生记录"<<endl;
cout<<"\t3.按学号查找学生记录"<<endl;
cout<<"\t4.按学号删除学生记录"<<endl;
cout<<"\t5.按学号修改学生的出生日期"<<endl;
cout<<"\t6.按学号修改学生的成绩"<<endl;
cout<<"\t7.退出系统"<<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时,只显示一个学生的信息