#include<iostream>
#include<iomanip>
#include<windows.h>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
const int Maxbor = 10;//每位读者最多借10本书
class Reader { //读者类
int tag;
int num;
char mima[6];
char name[10];
int borbook[Maxbor];
public:
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();
};
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() { tag = 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:
char* getname();
int getnum();
int getonshelf();
char* getwriter();
char* gettime();
char* getconcern();
float getprice();
int gettag();
void getTime(char time[]);
void setname(char na[]);
void delbook();
int borrowbook();//借书操作
void retbook();//还书操作
void disp();//输出图书
void addbook(int n, char* na, char* wri, char* con, char* tm, float pr);
};
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 = 1;
}
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[Maxbor];
public:
BDatabase();
void clear();
int addbook(int n, char* na, char* wri, char* con, char* tm, float pr);
Book* query(int bookid);
int qname(char* nm);
Book* 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("d:\\book.txt", ios::in);
for (;;) {
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++;
}
}
return j;
}
Book* BDatabase::Qname(char* nm)
{
int j = 0;
Book* b = new Book[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) {
return b;
}
else {
delete[]b;
return 0;
}
}
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++;
}
}
return e;
}
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(),cb) == 0) && book[i].gettag() == 0) {
g++;
}
}
return g;
}
Book* BDatabase::Qchuban(char* cb)
{
int h = 0;
Book* b = new Book[100];
for (int i = 0; i <= top; i++) {
if ((strcmp(book[i].getconcern(),cb) == 0) && book[i].gettag() == 0) {
b[h++] = book[i];
}
}
if (h) {
return b;
}
else {
delete[]b;
return 0;
}
}
void BDatabase::bookdata()//图书库维护
{
int choice = 1;
char bname[40];
int bookid;
char bwriter[15];
char bconcern[15];
char btime[15];
float bprice;
Book* b;
int r = 0;
while (choice)
{
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) {
case 1:
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;
case 2:
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;
case 3:
cout << "图书编号:";
cin >> bookid;
b = query(bookid);
if (b == NULL)
{
cout << "该图书不在" << endl;
Sleep(1000);
system("cls");
break;
}
system("cls");
cout << "所查信息如下:" << endl;
b->disp();
break;
case 4:
system("cls");
cout << "所有图书信息如下:" << endl;
b->disp();
break;
case 5:
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();
}
}
BDatabase::~BDatabase()//析构函数,将book[]写入book .txt文件中
{
fstream file("d:\\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[Maxbor];//读者记录
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("d:\\reader.txt", ios::in);
for (;;) {
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()//析构函数,将read[]写入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 Rdatebase::readerdata()//读者库维护
{
int choice;
char rname[20];
int readerid;
char rmima[6];
Reader* r;
while (choice)
{
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(readerid) != 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;
case 2:
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;
case 3:
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;
case 4:
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;
}
}
}
int main()
{
return 0;
}