#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 Book
{
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 ();
}
//1. 输入学生记录
void InputBook (Date &birthday)
{
char choice;
cout<<"输入书籍记录"<<endl;
ofstream outfile ("book.txt",ios::app); //打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
}
SetStuValues(birthday);
outfile<<"GetNum()"<<" "<<"GetName()" <<" "<<"birthday.GetYear()" <<" "<<"birthday. GetMonth()"<<" "<<"birthday. GetDay()"<<endl;
outfile.close();
cout<<"是否继续输入书籍信息 输入y(继续)或者n(停止)";
cin>>choice;
if (choice=='y')
{
InputBook(birthday);
}
outfile. close ();
system("pause");
}
//2. 浏览全部学生记录
void OutputBook ()
{
int count =0;
int year;
int month;
int day;
ifstream infile ("student.txt",ios::in);
if (infile)
{
cout<<"文件打开成功"<<endl;
}
else
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day;
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<endl;
infile.close ();
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 ("book.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 ("book. txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("bookcopy. 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 ("book. txt"); //删除文件
rename ("bookcopy.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 ("book. txt",ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("bookcopy.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<<" "<<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 ("book. txt"); //删除文件
rename ("bookcopy.txt","book.txt");
}
private:
int num;
string name;
Date birthday;
};
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;
Date stp;
Book stu;
Date birthday;
cout<<"请选择功能"<<endl;
cin>>num;
switch(num)
{
case 1:
stu. InputBook (birthday);
MainShow();
Select ();
break;
case 2:
stu. OutputBook();
MainShow ();
Select ();
break;
case 3:
stu. FindByNum();
MainShow();
Select ();
break;
case 4:
stu. DeleteByNum ();
MainShow();
Select ();
break;
case 5:
stu. AlterNameByNum();
MainShow();
Select();
break;
case 6:
stp.SetDate();
MainShow();
Select();
break;
case 7:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow();
Select ();
return 0;
}
126行不应该close文件啊。才读一行就close啊