|
楼主 |
发表于 2020-6-27 23:11:00
|
显示全部楼层
这是修改后的代码,但还是有5个bug
#include<iostream>
#include<iomanip>
#include<windows.h>
#include<conio.h>
#include<string>
using namespace std;
const int Maxbor=10;//每位读者最多借10本书
class Reader //读者类
{
int tag;
int num;
char mima[6];
char name[10];
int borbook[Maxbor];
public:
Reader();
int getnum();
int gettag();
char*getname();
char* getmima();
void setname(char na[]);
void delreader();
void addreader(int n,char*na,char*mi);
void borrowbook(int bookid);
int retbook(int bookid);
void disp();
};
Reader::reader(){
char*Reader::getname(){return name; }
int Reader::gettag(){return tag;}//获取删除符号
int Reader::getnum(){return num;}//获取读者编号
char*Reader::getmima(){return mima;}//获取读者的密码
void Reader::setname(char na[])//设置姓名
{
strcpy(name,na);
}
void Reader::delreader (){teg=1;}//删除读者
void Reader::addreader(int n,char*na,char*mi)//增加读者
{
tag=0;
num=n;
strcpy(name,na);
strcpy(mima,mi);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
void Reader::borrowbook(int bookid)//借书操作
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return;
}
}
}
int Reader::retbook(int bookid)//还书操作
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void Reader::disp();//输出读者信息
{
cout<<endl;
cout<<setw(12)<<num<<"借书编号:[";
for(int i=0;i<Maxbor;i++)
if(borbook[i]!=0)
cout<<borbook[i]<<";";
cout<<"]"<<endl;
}
class Book //图书类
{
int tag;
int num;
char name[10];
char writer[10];
char concern[30];
char Time[10];
float price;
int onshelf;
public:
Book();
char*getname();
int getnum();
int getonshelf();
char*getwriter();
char*gettime();
char*getconcern();
float getprice();
int gettag();
void getTime(char time[]);
void setname(cher na[]);
void delbook();
void addbook()(int n,char*wri,char*con,char*tm,float pr);//增加图书
int borrowbook();//借书操作
void retbook();//还书操作
void disp();//输出图书
} ;Book::Book(){}
char*Book::getname(){return name;}//获取姓名
int Book::getonshelf(){return onshelf;}
int Book::getnum(){return num; }//获取图书编号
char*Book::getwriter(){return writer;} //获取书籍作者
char*Book::getconcern(){return concern;}//获取书籍出版社
char*Book::gettime(){return Time;}
float Book::getprice(){return price;}//获取书籍价格
int Book::gettag(){return tag; } //获取删除标记
void Book::setname(char na[]) //设置书名
{
strcpy(name,na);
}
void Book::getTime(char time[])
{
strcpy(Time,time);
}
void Book::delbook(){tag=1;}//删除图书
void Book::addbook(int n,char*na,char*wri,char*con,char*tm,float pr)//增加图书
{
tag=0;
num=n;
strcpy(name,na);
strcpy(writer,wri);
strcpy(concern,con);
strcpy(Time,tm);
price=pr;
onshelf=l;
}
int Book::borrowbook()//借书操作
{
if(onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void Book::retbook()//还书操作
{
onshelf=1;
}
void Book::disp()//输出图书
{
cout << setw(6) << num << setw(12) << name << setw(15) << concern << setw(10) << writer << setw(12) << Time<<setw(6)<<(onshelf==1?"在架";"已借")<<endl;
}
class BDatabase //图书库类
{
int top;
Book book[Maxb];
public:
BDatabase();
void clear();
int addbook(int n,char*na,char*wri,char*con,char*tm,float pr);
Book *query(int bookid);
int BDatabase::qname(char*nm);
Book BDatabase::Qname(char*nm);
int qzuozhe(char *wr);
Book*Qzuozhe(char*wr);
int qchuban(char*cb);
Book*Qchuban(char*cb);
void bookdata();
~BDatabase();
void disp();
} ;
BDatabase::BDatabase()//构造函数将book.txt读到book[]中
{
Book b;
top=1;
fstream file("book.txt",ios::in);
while(1)
{
file.read((char*)&b,sizeof(b));
if(!file)break;
top++;
book[top]=b;
}
file.close;
}
void BDatabase::clear()//全删
{
top=-1;
}
int BDatabase::addbook(int n,char*na,char*wri,char*con,char*tm,float pr)//增加图书
{
Book *p=query(n);
if(p==NULL)
{
top++;
book[top].addbook(n,na,wri,con,tm,pr);
return 1;
}
return 0;
}
Book*BDatabase::query(int bookid)//查找图书
{
for(int i=0;i<=top;i++)
if(book[i].getnum()==bookid&&book[i].gettag()=0)
return &book[i];
return NULL;
}
int BDatabase::qname(char*nm)
{
int j=0;
for(int i=0;i<=top;i++)
if((strcmp(book[i].getname().nm)==0)&&book[i].gettag()==0)
j++;
if(j==0)
return 0;
else
return j;
}
Book*BDatabase::Qname(char*nm)
{
int j=0;
Book b[100];
for (int i=0;i<=top;i++)
if((strcmp(book[i].getname().nm)==0)&&book[i].gettag()==0)
b[j++]=book[i];
if(j==0)
return NULL;
else
return b;
}
int BDatabase::qzuozhe(char *wr)
{
int e=0;
for(int i=0:i<=top;i++)
if((strcmp(book[i].getwriter().wr)==0)&&book[i].gettag()=0)
e++;
if(e==0)
return 0;
else
return b;
}
Book*BDatabase::Qzuozhe(char *wr)
{
int f=0;
Book b[100];
for(int i=0:i<=top;i++)
if((strcmp(book[i].getwriter().wr)==0)&&book[i].gettag()=0)
b[f++]=book[i];
if(f==0)
return NULL;
else
return b;
}
int BDatabase::qchuban(char *cb)
{
int g=0;
for(int i=0:i<=top;i++)
if((strcmp(book[i].getconcern().wr)==0)&&book[i].gettag()=0)
g++;
if(g==0)
return 0;
else
return g;
}
Book*BDatabase::Qchuban(char *cb)
{
int h=0;
Book b[100];
for(int i=0:i<=top;i++)
if((strcmp(book[i].getconcern().wr)==0)&&book[i].gettag()=0)
b[h++]=book[i];
if(h==0)
return NULL;
else
return b;
}
void BDatabase::bookdata()//图书库维护
{
int choice=1;
char bname[40];
int bookid;
char bwrite[15];
char bconcern[15];
char btime[15];
float bprice;
Book *b;
int r=0;
while(choice!=0)
{
cout<<endl<<endl<<endl<<endl;
cout<<" --------------------------------- "<<endl;
cout<<" -- 图书维护 --"<<endl;
cout<<" -- 1.新增 --"<<endl;
cout<<" -- 2.删除 --"<<endl;
cout<<" -- 3.查找 --"<<endl;
cout<<" -- 4.显示 --"<<endl;
cout<<" -- 5.全删 --"<<endl;
cout<<" -- 0.退出 --"<<endl;
cout<<" ----------------------------------"<<endl;
cout<<"请选择您将进行的操作:";
cin>>choice;
switch(choice)
{
case1:
cout<<"图书编号:";
cin>>bookid;
do
{
if(r!=0)
{
cout<<"该编号错误,请重新输入:";
cin>>bookid;
}
r++;
if(r==3)
break;
}
while(query(bookid)!=NULL);
if(r==3)
{
system("cls");
break;
}
cout<<"图书书名:";
cin<<bname;
cout<<"图书作者:";
cin>>bwriter;
cout<<"图书出版社:";
cin>>bconcern;
cout<<"图书出版时间:";
cin>>btime;
cout<<"图书价格";
cin>>bprice;
cout<<"图书添加成功"<<endl;
Sleep(1000);
system("cls");
addbook(bookid,bname,bwriter,bconcern,btime,bprice);
break;
case2:
cout<<"图书编号:";
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不在"<<endl;
Sleep(1000);
system("cls");
break;
}
cout<<"已删除"<<endl;
Sleep(1000);
system("cls");
b->delbook();
break;
case3:
cout<<"图书编号:";
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不在"<<endl;
Sleep(1000);
system("cls");
break;
}
system("cls");
cout<<"所查信息如下:"<<endl;
b->disp();
break;
case4:
system("cls");
cout<<"所有图书信息如下:"<<endl;
b->disp();
break;
case5:
clear();
cout<<"所有图书已删除"<<endl;
Sleep(1000);
system("cls");
break:
}
}
system("cls");
}
void BDatabase::disp()
{
Book temp;
for(int i=0;i<=top&&(book[i].gettag()==0);i++)
for(int j=i+1;j<=top;&&(book[j].gettag()==0);j++)
if(strcmp(book[i].gettime(),book[j].gettime())<0)
{
temp=book[i];
book[i]=book[j];
book[j]=temp;
}
for(int x=0;x<=top;&&(book[x].gettag()==0);x++)
book[x].disp();
}
BDatebase::~BDatabase()//析构函数,将book[]写入book .txt文件中
{
fstream file("book.txt",ios::out);
for(int i=0;i<=top;i++)
if(book[i].gettag()=0)
file.write((char*)&book[i],sizeof(book[i]));
file.close();
}
class Rdatebase //读者库类
{
int top;
Reader read[Maxr];//读者记录
public:
Rdatebase();
void disp();
void clear();
void addreader(int n,char*na,char*mi);
Reader *query(int readerid);
~Rdatebase();
void readerdata();
} ;
Rdatebase::Rdatebase()//构造函数,将reader.txt读到read[]中
{
Reader s;
top=-1;
fstream file("reader.txt",ios::in);
while(1)
{
file.read((char*)&s,sizeof(s));
if(!file)break;
top++;
read[top]=s;
}
file.close();
}
void Rdatebase::disp();
{
for(int i=0;i<=top;i++)
if(read[i],gettag()==0)
read[i].disp();
}
void Rdatebase::clear()
{
top=-1;
}
void Rdatebase::addreader(int n,char*na,char*mi)
{
top++;
read[top].addreader(n,na,mi);
}
Reader*Rdatebase::query(int readerid)
{
for(int i=0;i<=top;i++)
if(read[i].getnum()==readerid&&read[i].gettag()==0)
return &read[i];
return NULL;
}
Rdatebase::~Rdatebase()//析构函数,将red[]写入read.txt文件中
{
fstream file("d:\\reader.txt",ios::out);
for(int i=0;i<=top;i++)
if(read[i].gettag()=0)
file.write((char*)&read[i],sizeof(read[i]));
file.close();
}
void Rdarebase::readerdata()//读者库维护
{
int choice;
char rname[20];
int readerid;
char rmima[6];
Reader *r;
while (choice!=0)
{
cout<<"--------------------------------------"<<endl;
cout<<"-- 读者维护 --"<<endl;
cout<<"-- 1、新增 --"<<endl;
cout<<"-- 2、更改 --"<<endl;
cout<<"-- 3、删除 --"<<endl;
cout<<"-- 4、查找 --"<<endl;
cout<<"-- 5、显示 --"<<endl;
cout<<"-- 6、全删 --"<<endl;
cout<<"-- 0.退出 --"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"请选择"<<endl;
cin>>choice;
int a=0;
int i=0;
switch(choice)
{
case 0:
system("cls");
break;
case 1:
cout<<"读者编号:";
cin>>readerid;
do
{
if(a!=0)
{
cout<<"已存在,请重新输入:";
cin>>readerid;
}
a++;
}
while(query(reader)!=NULL);
cout<<"输入读者密码:";
for(i;i<6;i++)
{
rmima[i]=getch();
putch("-");
}
cout<<endl<<"输入读者姓名:";
cin>>rname;
cout<<"添加成功"<<endl;
addreader(readerid,rname,rmima);
Sleep(1000);
system("cls");
break;
case2:
cout<<"读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
Sleep(1000);
system("cls");
break;
}
cout<<"输入新的读者姓名:";
cin>>rname;
cout<<"读者更改成功"<<endl;
Sleep(1000);
system("cls");
r->setname(rname);
break;
case3:
cout<<"读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
Sleep(1000);
system("cls");
break;
}
cout<<"读者删除成功"<<endl;
Sleep(1000);
system("cls");
r->delreader();
break;
case4:
cout<<"读入读者编号:";
cin>>readerid;
r=query(readerid);
if(r==NULL)
{
cout<<"该读者不存在"<<endl;
Sleep(1000);
system("cls");
break;
}
system("cls");
cout<<"您所查找的读者信息如下"<<endl;
r->disp();
break;
case 5:
system("cls");
cout<<"所有读者信息如下"<<endl;
disp();
break;
case 6:
clear();
cout<<"所有读者已删除"<<endl ;
Sleep(1000);
system("cls");
break;
}
}
} |
|