这个是我们的开始题目。在清明的时候我仔细的想了想。我在家做了一下。下面就是我的源代码:
#include<iostream>
#include<string>
#include<fstream>
#define N 1
using namespace std;
class lab
{
private:
char ISBN[20]; //图书号
char bookname[20];
char authorname[20];
char flno[20];//分类号
char department[20];//出版单位
char time[15];
float price;
int count;
public:
void input(lab l[],int n);
void output(lab l[],int n);
void output1(lab l[],int n);
void reeadfile(lab l[],int n);
void showinfor(lab l[],int n);
void saveinfor(lab l[],int n);
void saveinfor1(lab l[],int n);
void search(lab l[],int n);
void search1(lab l[],int n);
void search2(lab l[],int n);
void search3(lab l[],int n);
void search4(lab l[],int n);
void search5(lab l[],int n);
void search6(lab l[],int n);
void search7(lab l[],int n);
void delete1(lab l[],int n);
};
void lab::input(lab l[],int n)
{
cout<<"请输入您要录入的图书的信息(作者名 书名 出版单位 分类号 图书编号 价格 时间):"<<endl;
for(int i=0;i<n;i++)
cin>>l[i].authorname>>l[i].bookname>>
l[i].department>>l[i].flno>>l[i].ISBN>>l[i].price>>l[i].time;
}
void lab::reeadfile(lab l[],int n)
{
ifstream f2;
f2.open("d:\图书馆信息库.txt");
for(int i=0;i<8;i++)
f2>>l[i].authorname>>l[i].bookname>>
l[i].department>>l[i].flno>>l[i].ISBN>>l[i].price>>l[i].time;
}
void lab::output1(lab l[],int n)
{
cout<<"录入后的图书信息为:"<<endl;
for(int i=0;i<N;i++)
cout<<l[i].authorname<<" "<<l[i].bookname<<" "<<
l[i].department<<" "<<l[i].flno<<" "<<l[i].ISBN<<" "<<l[i].price<<" "<<l[i].time;
cout<<endl;
}
void lab::saveinfor1(lab l[],int n)
{
ofstream f3;
f3.open("d:\图书馆.txt");
for(int i=0;i<n;i++)
{ f3<<l[i].authorname<<" "<<l[i].bookname<<" "<<
l[i].department<<" "<<l[i].flno<<" "<<l[i].ISBN<<" "<<l[i].price<<" "<<l[i].time;
f3<<endl;
}
f3.close();
}
void lab::saveinfor(lab l[],int n)
{
ofstream f1;
f1.open("d:\图书馆.txt");
for(int i=0;i<N+8;i++)
{ f1<<l[i].authorname<<" "<<l[i].bookname<<" "<<
l[i].department<<" "<<l[i].flno<<" "<<l[i].ISBN<<" "<<l[i].price<<" "<<l[i].time;
f1<<endl;
}
f1.close();
}
void lab::output(lab l[],int n)
{
//cout<<"您录入后的图书信息为:"<<endl;
for(int i=0;i<N+8;i++)
{ cout<<l[i].authorname<<" "<<l[i].bookname<<" "<<
l[i].department<<" "<<l[i].flno<<" "<<l[i].ISBN<<" "<<l[i].price<<" "<<l[i].time;
cout<<endl;
}
}
void lab::search1(lab l[],int n)
{
string t;int m=-1;
cout<<"请输入您要查询的作者名:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
{ if(l[i].authorname==t)
{ m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search2(lab l[],int n)
{
string t;int m=-1;
cout<<"请输入您要查询的书名:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
{ if(l[i].bookname==t)
{ m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search3(lab l[],int n)
{
string t;int m=-1;
cout<<"请输入您要查询的出版单位:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
{ if(l[i].department==t)
{m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search4(lab l[],int n)
{
string t;int m=-1;
cout<<"请输入您要查询的分类号:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
{ if(l[i].flno==t)
{ m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search5(lab l[],int n)
{
string t;int m=-1;
cout<<"请输入您要查询的图书编号:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
{ if(l[i].ISBN==t)
{ m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search6(lab l[],int n)
{
int t;int m=-1;
cout<<"请输入您要查询的价格:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
if(l[i].price==t)
{m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search7(lab l[],int n)
{
string t;int m=-1;
cout<<"请输入您要查询的时间:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
{ if(l[i].time==t)
{ m=i;
cout<<l[m].authorname<<" "<<l[m].bookname<<" "<<
l[m].department<<" "<<l[m].flno<<" "<<l[m].ISBN<<" "<<l[m].price<<" "<<l[m].time;
cout<<endl;}
}
if(m==-1)
cout<<"抱歉!本图书馆暂未收录您所查询的图书:"<<endl;
}
void lab::search(lab l[],int n)
{
string t;
int m,k;
cout<<"请输入您想通过查询的方式:"<<endl;
cout<<"1-作者名查询 2-书名查询 3-出版单位查询 4-分类号查询 5-图书编号查询 6-价格查询 7-时间查询"<<endl;
cin>>k;
switch(k)
{
case 1:search1(l,N);break;
case 2:search2(l,N);break;
case 3:search3(l,N);break;
case 4:search4(l,N);break;
case 5:search5(l,N);break;
case 6:search6(l,N);break;
case 7:search7(l,N);break;
}
}
void lab::delete1(lab l[],int n)
{
string t;
char t1;
int x=-1;
cout<<"请输入您认为无价值的书的名称:"<<endl;
cin>>t;
for(int i=0;i<8;i++)
if(l[i].bookname==t)
{x=i;
cout<<"您要删除的图书的资料为:"<<endl;
cout<<l[x].authorname<<" "<<l[x].bookname<<" "<<
l[x].department<<" "<<l[x].flno<<" "<<l[x].ISBN<<" "<<l[x].price<<" "<<l[x].time;
cout<<"Are you sure to delete the informatiom of the book(Y/N):"<<endl;
cin>>t1;
if(t1=='y'||t1=='Y')
{for(int i=0;i<x;i++)
l[i]=l[i];
for(int i=x;i<8;i++)
l[i]=l[i+1];
cout<<"删除后的信息为:"<<endl;
for(int i=0;i<7;i++)
{ cout<<l[i].authorname<<" "<<l[i].bookname<<" "<<
l[i].department<<" "<<l[i].flno<<" "<<l[i].ISBN<<" "<<l[i].price<<" "<<l[i].time;
cout<<endl;}
}
else
cout<<"您放弃了删除该图书请认真保存该图书吧:"<<endl;
}
if(x==-1)
cout<<"被系统内暂无无用的图书:"<<endl;
}
void main()
{
lab l[N];
lab zxh;
zxh.reeadfile(l,N);zxh.saveinfor(l,N);
int k;
cout<<"********************淮阴师范学院图书馆欢迎您********************"<<endl;
cout<<"1-图书信息的录入 2-图书信息的查询 3-图书信息的删除 4-退出系统"<<endl;
cout<<"请输入您要选择的功能的序号:"<<endl;
cin>>k;
switch(k)
{
case 1:zxh.input(l,N);zxh.output1(l,N);zxh.output(l,N);break;
case 2:zxh.search(l,8);break;
case 3:zxh.delete1(l,8);break;
case 4:cout<<"********欢迎使用本系统!欢迎下次继续使用本系统********"<<endl;break;
}
}