#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
//定义时间日期类表示学生的出生年月日
class Date
{
public:
void SetDate () //设置年月
{
cout<<"请输入出生年份"<<endl;
cin>>year;
cout<<"请输入出生月份"<<endl;
cin>>month;
cout<<"请输入出生日"<<endl;
cin>>day;
}
int GetYear() //获取年份
{
return year;
}
int GetMonth ( ) //获取月
{
return month;
}
int GetDay () //获取日
{
return day;
}
private:
int year;
int month;
int day;
};
//定义学生类,包含学生的学号、姓名、出生年月日
class Student
{
public:
void SetStuValues (Date &birthday)
{
cout<<"请输入学生的学号"<<endl;
cin>>num;
cout<<"请输入学生的姓名"<<endl;
cin>>name;
birthday. SetDate ();
}
void SetNum () //设置学生的学号
{
cin>>num;
}
int GetNum () //获得学生的学号
{
return num;
}
void SetName ()//设置学生的姓
{
cin>>name;
}
string GetName ()//获得学生的姓名
{
return name;
}
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 ("student. txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<GetNum ()<<""<<GetName()<<""<<birthday.GetYear() <<""<<birthday.GetMonth () <<""<<birthday. GetDay ()<<endl;
cout<<"是否继续输入学生信息输入y(继续)或者n(停止)";
cin>>choice;
if(choice=='y')
{
InputStudent (birthday);
}
outfile.close ();
system ("pause");
}
//2.浏览全部学生记录
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;
infile. get ();
if (infile.peek ()=='\n') break;
}
infile. close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int count = 0;
int year;
int month;
int day;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号";
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;
find = true;
}
infile. get ( );
if (infile. peek ()=='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile. close ();
system ("pause");
}
//4. 按学号删除学生记录
void DeleteByNum ()
{
int count = 0;
int year;
int month;
int day;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号";
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<<endl;
}
else
{
cout<<num<<""<<name<<""<<year<<""<<month<<""<<day<<endl;
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile. peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("student, txt" ) ; //删除文件
rename ("studentcopy, txt" , " student. txt" ) ;
}
//5.按学号修改学生的姓名
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day;
int findnum;
string altername;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的姓名";
cin>>altername;
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<<endl;
}
else
{
outfile <<num<<""<<altername<<""<<year<<""<<month<<""<<day<<endl;
find = true;
cout<<"已经修改该学生"<<endl;
}
infile. get ();
if (infile. peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile. close ();
outfile. close ();
remove ("student, txt" ) ; //删除文件
rename ("studentcopy, txt" , " student. txt" ) ;
}
//5.按学号修改学生的姓名
void AlterNameByNum ()
{
int count =0;
int year;
int month;
int day;
int findnum;
string altername;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的姓名";
cin>>altername;
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<<endl;
}
else
{
outfile <<num<<""<<altername<<""<<year<<""<<month<<""<<day<<endl;
find = true;
cout<<"已经修改该学生"<<endl;
}
infile. get ();
if (infile. peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile. close ();
outfile. close ();
Select ();
break;
case 6:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}
如果把代码修改为学生的数据属性有:学号、姓名、性别.出生日期、课程成绩。先把学生的数据属性存入到student.txt文件,其中每个学生有三门课成绩,先求三门课的平均分、三门课的最高分和最低分,再存入student.txt文件。并且把代码原有的功能修改学生姓名改成修改学生的出生日期,然后学生管理系统需要实现的功能如下:
(1)浏览全部学生记录;
(2)按学号查找学生记录;
(3)按学号删除学生记录;
(4)按学号修改学生的出生日期。
(5)按学号修改学生某门课的成绩。