#pragma once
#define NUM1 128
#define NUM2 50
#define Col 80
#define Line 25
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
using namespace std;
 
class Book {
public:
	Book() {}
	Book(char* bookno,char* Name, char* lsbn, char* Price, char* Author,char* time,char* banci);
	~Book() {}
public:
	char* Getbookno();
	void Setbookno(char *bookno);
	char* GetName();
	void SetName(char *Name);
	char* Getlsbn();
	void Setlsbn(char* lsbn);
	char* GetPrice();
	void SetPrice(char* Price);
	char* GetAuthor();
	void SetAuthor(char* Author);
	char* Gettime();
	void Settime(char* time);
	char* Getbanci();
	void Setbanci(char* banci);
	virtual void WriteData();
	virtual void DeleteData(int count);
	void GetBookFromFile(int count);
	char m_bookno[NUM2];
protected:
	char m_Name[NUM1];
	char m_lsbn[NUM1];
	char m_Price[NUM2];
	char m_Author[NUM2];
	char m_time[NUM1];
	char m_banci[NUM1];
};
 
class Reader {
public:
	Reader() {}
	Reader(char* Name, char* age, char* sex, char* No, char* address);
	~Reader() {}
public:
	char* GetName();
	void SetName(char *Name);
	char* Getage();
	void Setage(char* age);
	char* Getsex();
	void Setsex(char* sex);
	char* GetNo();
	void SetNo(char* No);
	char* Getaddress();
	void Setaddress(char* address);
	virtual void WriteData();
	virtual void DeleteData(int count);
	void GetReaderFromFile(int count);
protected:
	char n_Name[NUM1];
	char n_age[NUM1];
	char n_sex[NUM2];
	char n_No[NUM2];
	char n_address[NUM2];
};
 
class Borrow {
public:
	Borrow() {}
	Borrow(char* bookno, char* readerno, char* outtime, char* intime);
	~Borrow() {}
public:
	char* Getbookno();
	void Setbookno(char *bookno);
	char* Getreaderno();
	void Setreaderno(char* readerno);
	char* Getouttime();
	void Setouttime(char* outtime);
	char* Getintime();
	void Setintime(char* intime);
	virtual void WriteData();
	virtual void DeleteData(int count);
	void GetBorrowFromFile(int count);
protected:
	char n_bookno[NUM2];
	char n_readerno[NUM2];
	char n_outtime[NUM2];
	char n_intime[NUM2];
};
 
void Wait() {
	int inputpage = 0;
	cout << "enter:返回主菜单       q:退出" << endl;
	char buf[256];
	gets_s(buf);
	if (buf[0] == 'q')
		system("exit");
}
 
void SetScreenGrid() {
	char sysSetBuf[80];
	sprintf(sysSetBuf, "mode con cols=%d lines=%d", Col, Line);
	system(sysSetBuf);
}
 
void SetSysCaption() {
	system("title Sample");
}
 
void ClearScreen() {
	system("cls");
}
 
void SetSysCaption(const char* Text) {
	char sysSetBuf[80];
	sprintf(sysSetBuf, "title %s", Text);
	system(sysSetBuf);
}
 
void ShowWelcome() {
	for (int i = 0; i < 2; i++) {
		cout << endl;
	}
	cout << setw(40);
	cout << "**************" << endl;
	cout << setw(40);
	cout << "图书管理系统" << endl;
	cout << setw(40);
	cout << "**************" << endl;
}
 
void ShowMenu() {
	cout << setw(40);
	cout << "请选择功能:" << endl;
	cout << endl;
	cout << setw(38);
	cout << "1、添加新书" << endl;
	cout << endl;
	cout << setw(42);
	cout << "2、浏览图书信息" << endl;
	cout << endl;
	cout << setw(38);
	cout << "3、删除图书" << endl;
	cout << endl;
	cout << setw(38);
	cout << "4、添加读者" << endl;
	cout << endl;
	cout << setw(42);
	cout << "5、浏览读者信息" << endl;
	cout << endl;
	cout << setw(38);
	cout << "6、删除读者" << endl;
	cout << endl;
	cout << setw(42);
	cout << "7、添加借阅信息" << endl;
	cout << endl;
	cout << setw(42);
	cout << "8、浏览借阅信息" << endl;
	cout << endl;
	cout << setw(42);
	cout << "9、删除借阅信息" << endl;
}
 
int GetSelect() {
	char buf[256];
	gets_s(buf);
	return atoi(buf);
}
#pragma once
#define NUM1 128
#define NUM2 50
#define Col 80
#define Line 25
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
using namespace std;
 
class Book {
public:
	Book() {}
	Book(char* bookno,char* Name, char* lsbn, char* Price, char* Author,char* time,char* banci);
	~Book() {}
public:
	char* Getbookno();
	void Setbookno(char *bookno);
	char* GetName();
	void SetName(char *Name);
	char* Getlsbn();
	void Setlsbn(char* lsbn);
	char* GetPrice();
	void SetPrice(char* Price);
	char* GetAuthor();
	void SetAuthor(char* Author);
	char* Gettime();
	void Settime(char* time);
	char* Getbanci();
	void Setbanci(char* banci);
	virtual void WriteData();
	virtual void DeleteData(int count);
	void GetBookFromFile(int count);
	char m_bookno[NUM2];
protected:
	char m_Name[NUM1];
	char m_lsbn[NUM1];
	char m_Price[NUM2];
	char m_Author[NUM2];
	char m_time[NUM1];
	char m_banci[NUM1];
};
 
class Reader {
public:
	Reader() {}
	Reader(char* Name, char* age, char* sex, char* No, char* address);
	~Reader() {}
public:
	char* GetName();
	void SetName(char *Name);
	char* Getage();
	void Setage(char* age);
	char* Getsex();
	void Setsex(char* sex);
	char* GetNo();
	void SetNo(char* No);
	char* Getaddress();
	void Setaddress(char* address);
	virtual void WriteData();
	virtual void DeleteData(int count);
	void GetReaderFromFile(int count);
protected:
	char n_Name[NUM1];
	char n_age[NUM1];
	char n_sex[NUM2];
	char n_No[NUM2];
	char n_address[NUM2];
};
 
class Borrow {
public:
	Borrow() {}
	Borrow(char* bookno, char* readerno, char* outtime, char* intime);
	~Borrow() {}
public:
	char* Getbookno();
	void Setbookno(char *bookno);
	char* Getreaderno();
	void Setreaderno(char* readerno);
	char* Getouttime();
	void Setouttime(char* outtime);
	char* Getintime();
	void Setintime(char* intime);
	virtual void WriteData();
	virtual void DeleteData(int count);
	void GetBorrowFromFile(int count);
protected:
	char n_bookno[NUM2];
	char n_readerno[NUM2];
	char n_outtime[NUM2];
	char n_intime[NUM2];
};
 
void Wait() {
	int inputpage = 0;
	cout << "enter:返回主菜单       q:退出" << endl;
	char buf[256];
	gets_s(buf);
	if (buf[0] == 'q')
		system("exit");
}
 
void SetScreenGrid() {
	char sysSetBuf[80];
	sprintf(sysSetBuf, "mode con cols=%d lines=%d", Col, Line);
	system(sysSetBuf);
}
 
void SetSysCaption() {
	system("title Sample");
}
 
void ClearScreen() {
	system("cls");
}
 
void SetSysCaption(const char* Text) {
	char sysSetBuf[80];
	sprintf(sysSetBuf, "title %s", Text);
	system(sysSetBuf);
}
 
void ShowWelcome() {
	for (int i = 0; i < 2; i++) {
		cout << endl;
	}
	cout << setw(40);
	cout << "**************" << endl;
	cout << setw(40);
	cout << "图书管理系统" << endl;
	cout << setw(40);
	cout << "**************" << endl;
}
 
void ShowMenu() {
	cout << setw(40);
	cout << "请选择功能:" << endl;
	cout << endl;
	cout << setw(38);
	cout << "1、添加新书" << endl;
	cout << endl;
	cout << setw(42);
	cout << "2、浏览图书信息" << endl;
	cout << endl;
	cout << setw(38);
	cout << "3、删除图书" << endl;
	cout << endl;
	cout << setw(38);
	cout << "4、添加读者" << endl;
	cout << endl;
	cout << setw(42);
	cout << "5、浏览读者信息" << endl;
	cout << endl;
	cout << setw(38);
	cout << "6、删除读者" << endl;
	cout << endl;
	cout << setw(42);
	cout << "7、添加借阅信息" << endl;
	cout << endl;
	cout << setw(42);
	cout << "8、浏览借阅信息" << endl;
	cout << endl;
	cout << setw(42);
	cout << "9、删除借阅信息" << endl;
}
 
int GetSelect() {
	char buf[256];
	gets_s(buf);
	return atoi(buf);
}
#include "Book.h"
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
using namespace std;
 
Book::Book(char* bookno,char* Name, char* lsbn, char* Price, char* Author, char* time, char* banci) {
	strncpy(m_bookno, bookno, NUM2);
	strncpy(m_Name, Name, NUM1);
	strncpy(m_lsbn, lsbn, NUM1);
	strncpy(m_Price, Price, NUM2);
	strncpy(m_Author, Author, NUM2);
	strncpy(m_time, time, NUM1);
	strncpy(m_banci, banci, NUM1);
}
 
char* Book::Getbookno() {
	return m_bookno;
}
void Book::Setbookno(char* bookno) {
	strncpy(m_bookno, bookno, NUM2);
}
 
char* Book::GetName() {
	return m_Name;
}
void Book::SetName(char* Name) {
	strncpy(m_Name, Name, NUM1);
}
 
char* Book::Getlsbn() {
	return m_lsbn;
}
void Book::Setlsbn(char* lsbn) {
	strncpy(m_lsbn, lsbn, NUM1);
}
 
char* Book::GetPrice() {
	return m_Price;
}
void Book::SetPrice(char* Price) {
	strncpy(m_Price, Price, NUM2);
}
 
char* Book::GetAuthor() {
	return m_Author;
}
void  Book::SetAuthor(char* Author) {
	strncpy(m_Author, Author, NUM2);
}
 
char* Book::Gettime() {
	return m_time;
}
void Book::Settime(char* time) {
	strncpy(m_time, time, NUM1);
}
 
char* Book::Getbanci() {
	return m_banci;
}
void Book::Setbanci(char* banci) {
	strncpy(m_banci, banci, NUM1);
}
 
void Book::WriteData() {
	ofstream file;
	file.open("F://book.dat", ios::binary | ios::app);
	try {
		file.write(m_bookno, NUM2);
		file.write(m_Name, NUM1);
		file.write(m_lsbn, NUM1);
		file.write(m_Price, NUM2);
		file.write(m_Author, NUM2);
		file.write(m_time, NUM1);
		file.write(m_banci, NUM1);
	}
	catch (...) {
		throw "file error occurred";
		file.close();
	}
	file.close();
}
 
void Book::GetBookFromFile(int count) {
	char bookno[NUM2];
	char Name[NUM1];
	char lsbn[NUM1];
	char Price[NUM2];
	char Author[NUM2];
	char time[NUM1];
	char banci[NUM1];
	ifstream file;
	file.open("F://book.dat", ios::binary);
	try {
		file.seekg(count*(NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2), ios::beg);
		file.read(bookno, NUM2);
		if (file.tellg() > 0) {
			strncpy(m_bookno, bookno, NUM2);
		}
		file.read(Name, NUM1);
		if (file.tellg() > 0) {
			strncpy(m_Name, Name, NUM1);
		}
		file.read(lsbn, NUM1);
		if (file.tellg() > 0) {
			strncpy(m_lsbn, lsbn, NUM1);
		}
		file.read(Price, NUM2);
		if (file.tellg() > 0) {
			strncpy(m_Price, Price, NUM2);
		}
		file.read(Author, NUM2);
		if (file.tellg() > 0) {
			strncpy(m_Author, Author, NUM2);
		}
		file.read(time, NUM1);
		if (file.tellg() > 0) {
			strncpy(m_time, time, NUM1);
		}
		file.read(banci, NUM1);
		if (file.tellg() > 0) {
			strncpy(m_banci, banci, NUM1);
		}
	}
	catch (...) {
		throw"file error occurred";
		file.close();
	}
	file.close();
}
 
void Book::DeleteData(int count) {
	long respos;
	int DataCount = 0;
	fstream file;
	fstream tmpfile;
	ofstream ofile;
	char TempBuf[NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2];
	file.open("F://book.dat", ios::binary | ios::in | ios::out);
	tmpfile.open("F://temp.dat", ios::binary | ios::in | ios::out|ios::trunc);
	file.seekg(0, ios::end);
	respos = file.tellg();
	DataCount = respos / (NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2);
	if (count<0 && count>DataCount) {
		throw"Input number error";
	}
	else {
		file.seekg(count*(NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2), ios::beg);
		for (int j = 0; j < (DataCount - count); j++) {
			memset(TempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2);
			file.read(TempBuf, NUM1 + NUM2 + NUM1 + NUM2+NUM1+NUM1+NUM2);
			tmpfile.write(TempBuf, NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2);
		}
		file.close();
		tmpfile.seekg(0, ios::beg);
		ofile.open("F://book.dat");
		ofile.seekp((count-1)*(NUM1+NUM1+NUM2+NUM2+NUM1+NUM1+NUM2),ios::beg);
		for (int i = 0; i < (DataCount - count); i++) {
			memset(TempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2);
			tmpfile.read(TempBuf, NUM1 + NUM2 + NUM1 + NUM2+NUM1+NUM1+NUM2);
			ofile.write(TempBuf, NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2);
		}
	}
	tmpfile.close();
	ofile.close();
	remove("temp.dat");
}
 
void AddBook() {
	char ibookno[NUM2];
	char iName[NUM1];
	char ilsbn[NUM1];
	char iPrice[NUM2];
	char iAuthor[NUM2];
	char itime[NUM1];
	char ibanci[NUM1];
	cout << "输入图书编号:" << endl;
	cin >> ibookno;
	cout << "输入书名:"<< endl;
	cin >> iName;
	cout << "输入ISBN编号:" << endl;
	cin >> ilsbn;
	cout << "输入价格:" << endl;
	cin >> iPrice;
	cout << "输入作者:" << endl;
	cin >> iAuthor;
	cout << "输入出版时间:" << endl;
	cin >> itime;
	cout << "输入版次:" << endl;
	cin >> ibanci;
	Book book(ibookno,iName, ilsbn, iPrice, iAuthor,itime,ibanci);
	book.WriteData();
	cout << "Write Finish" << endl;
	Wait();
}
long GetFileLength(ifstream &ifs) {
	long tmppos;
	long respos;
	tmppos = ifs.tellg();
	ifs.seekg(0, ios::end);
	respos = ifs.tellg();
	ifs.seekg(tmppos, ios::beg);
	return respos;
}
void WaitView(int page) {
	char a;
	cin >> a;
	if (a = 'n') {
		page = page + 1;
	}
}
 
void ViewData(int selpage = 1) {
	int page = 0;
	int curpage = 0;
	int datacount = 0;
	char inbookno[NUM2];
	char inName[NUM1];
	char inlsbn[NUM1];
	char price[NUM2];
	char inAuthor[NUM2];
	char intime[NUM1];
	char inbanci[NUM1];
	bool index = false;
	int filelength;
	curpage = selpage;
	ifstream file;
	file.open("F://book.dat", ios::binary);
	filelength = GetFileLength(file);
	datacount = filelength/(NUM1+NUM1+NUM2+NUM2+NUM1+NUM1+NUM2);
	if (datacount >= 1)index = true;
	page = datacount / 20 + 1;
	ClearScreen();
	cout << "共有记录" << datacount << "    ";
	cout << "共有页数" << page << "   ";
	cout << "当前页数" << curpage << "  ";
	cout << "     n: 显示下一页  m:返回" << endl;
	cout << setw(5) << "index";
	cout << setw(10) << "BookNO";
	cout << setw(20) << "Name" << setw(20) << "lsbn";
	cout << setw(15) << "Price" << setw(15) << "Author";
	cout << setw(15) << "Time" << setw(15) << "Banci";
	cout << endl;
	try {
		file.seekg((curpage - 1) * 20 * (NUM1 + NUM1 + NUM2 + NUM2+NUM1+NUM1+NUM2), ios::beg);
		if (!file.fail()) {
			for (int i = 1; i < 21; i++) {
				memset(inbookno, 0, 20);
				memset(inName, 0, 128);
				memset(inlsbn, 0, 128);
				memset(price, 0, 50);
				memset(inAuthor, 0, 50);
				memset(intime, 0, 50);
				memset(inbanci, 0, 50);
				if (index) cout << setw(5) << (curpage - 1) * 20 + i;
				cout << setw(10);
				file.read(inbookno, NUM2);
				cout << inbookno;
				cout << setw(20);
				file.read(inName, NUM1);
				cout << inName;
				file.read(inlsbn, NUM1);
				cout << setw(20);
				cout << inlsbn;
				file.read(price, NUM2);
				cout << setw(15);
				cout << price;
				file.read(inAuthor, NUM2);
				cout << setw(15);
				cout << inAuthor;
				file.read(intime, NUM1);
				cout << setw(15);
				cout << intime;
				file.read(inbanci, NUM1);
				cout << setw(15);
				cout << inbanci;
				cout << endl;
				if (file.tellg() < 0)index = false;
				else index = true;
			}
		}
	}
	catch (...) {
		cout << "throw file exception" << endl;
		throw"file error occurred";
		file.close();
	}
	if (curpage < page) {
		curpage = curpage + 1;
		WaitView(curpage);
	}
	else WaitView(curpage);
	file.close();
}
 
void DelectBookFromFile() {
	char bookno1[NUM2];
	cout << "Input delect BookNO:" << endl;
	cin >> bookno1;
	int count = 0;
	fstream file;
	file.open("F://book.dat", ios::binary | ios::in | ios::out);
	file.seekg(0, ios::end);
	int res = file.tellg();
	int a = NUM1 + NUM1 + NUM2 + NUM2 + NUM1 + NUM1 + NUM2;
	char buf[NUM2];
	file.seekg(0, ios::beg);
	for (int j = NUM2; j <= res; j=j+a) {
		count += 1;
		file.seekg(NUM2,ios::cur);
		memset(buf, 0, NUM2);
		file.read(buf, NUM2);
		if(bookno1>=buf){
			break;
		}
		file.seekg(a - NUM2, ios::cur);
	}
	file.close();
	Book tmpbook;
	tmpbook.DeleteData(count);
 
	int count1 = 0;
	fstream file1;
	file1.open("F://Borrow.dat", ios::binary | ios::in | ios::out);
	file1.seekg(0, ios::end);
	int res1 = file1.tellg();
	int a1 = NUM2 + NUM2 + NUM2 + NUM2;
	char buf1[NUM2];
	file1.seekg(0, ios::beg);
	for (int j1 = NUM2; j1 <= res1; j1 = j1 + a1) {
		count1 += 1;
		file1.seekg(NUM2, ios::cur);
		memset(buf1, 0, NUM2);
		file1.read(buf1, NUM2);
		if (bookno1>=buf1) {
			break;
		}
		file1.seekg(a1 - NUM2, ios::cur);
	}
	file1.close();
	Borrow tmpbook1;
	tmpbook1.DeleteData(count1);
	cout << "Delect Finish" << endl;
	system("pause");
	Wait();
}
 
Reader::Reader(char* Name, char* age, char* sex, char* No, char* address) {
	strncpy(n_Name, Name, NUM1);
	strncpy(n_age, age, NUM1);
	strncpy(n_sex, sex, NUM2);
	strncpy(n_No, No, NUM2);
	strncpy(n_address, address, NUM2);
}
 
char* Reader::GetName() {
	return n_Name;
}
void Reader::SetName(char* Name) {
	strncpy(n_Name, Name, NUM1);
}
 
char* Reader::Getage() {
	return n_age;
}
void Reader::Setage(char* age) {
	strncpy(n_age, age, NUM1);
}
 
char* Reader::Getsex() {
	return n_sex;
}
void Reader::Setsex(char* sex) {
	strncpy(n_sex, sex, NUM2);
}
 
char* Reader::GetNo() {
	return n_No;
}
void  Reader::SetNo(char* No) {
	strncpy(n_No, No, NUM2);
}
 
char* Reader::Getaddress() {
	return n_address;
}
void Reader::Setaddress(char* address) {
	strncpy(n_address, address, NUM2);
}
 
void Reader::WriteData() {
	ofstream file;
	file.open("F://Reader.dat", ios::binary | ios::app);
	try {
		file.write(n_Name, NUM1);
		file.write(n_age, NUM1);
		file.write(n_sex, NUM2);
		file.write(n_No, NUM2);
		file.write(n_address, NUM2);
	}
	catch (...) {
		throw "file error occurred";
		file.close();
	}
	file.close();
}
 
void Reader::GetReaderFromFile(int count) {
	char Name[NUM1];
	char age[NUM1];
	char sex[NUM2];
	char No[NUM2];
	char address[NUM2];
	ifstream file;
	file.open("F://Reader.dat", ios::binary);
	try {
		file.seekg(count*(NUM1 + NUM1 + NUM2 + NUM2 + NUM2), ios::beg);
		file.read(Name, NUM1);
		if (file.tellg() > 0) {
			strncpy(n_Name, Name, NUM1);
		}
		file.read(age, NUM1);
		if (file.tellg() > 0) {
			strncpy(n_age, age, NUM1);
		}
		file.read(sex, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_sex, sex, NUM2);
		}
		file.read(No, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_No, No, NUM2);
		}
		file.read(address, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_address, address, NUM2);
		}
	}
	catch (...) {
		throw"file error occurred";
		file.close();
	}
	file.close();
}
 
void Reader::DeleteData(int count) {
	long respos;
	int DataCount = 0;
	fstream file;
	fstream tmpfile;
	ofstream ofile;
	char TempBuf[NUM1 + NUM1 + NUM2 + NUM2 + NUM2];
	file.open("F://Reader.dat", ios::binary | ios::in | ios::out);
	tmpfile.open("F://tempReader.dat", ios::binary | ios::in | ios::out | ios::trunc);
	file.seekg(0, ios::end);
	respos = file.tellg();
	DataCount = respos / (NUM1 + NUM1 + NUM2 + NUM2 + NUM2);
	if (count<0 && count>DataCount) {
		throw"Input number error";
	}
	else {
		file.seekg(count*(NUM1 + NUM1 + NUM2 + NUM2 + NUM2), ios::beg);
		for (int j = 0; j < (DataCount - count); j++) {
			memset(TempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2 + NUM2);
			file.read(TempBuf, NUM1 + NUM2 + NUM1 + NUM2 + NUM2);
			tmpfile.write(TempBuf, NUM1 + NUM1 + NUM2 + NUM2 + NUM2);
		}
		file.close();
		tmpfile.seekg(0, ios::beg);
		ofile.open("F://Reader.dat");
		ofile.seekp((count - 1)*(NUM1 + NUM1 + NUM2 + NUM2 + NUM2), ios::beg);
		for (int i = 0; i < (DataCount - count); i++) {
			memset(TempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2 + NUM2);
			tmpfile.read(TempBuf, NUM1 + NUM2 + NUM1 + NUM2 + NUM2);
			ofile.write(TempBuf, NUM1 + NUM1 + NUM2 + NUM2 + NUM2);
		}
	}
	tmpfile.close();
	ofile.close();
	remove("tempReader.dat");
}
 
void AddReader() {
	char iName[NUM1];
	char iage[NUM1];
	char isex[NUM2];
	char iNo[NUM2];
	char iaddress[NUM2];
	cout << "输入读者姓名:" << endl;
	cin >> iName;
	cout << "输入读者年龄:" << endl;
	cin >> iage;
	cout << "输入读者性别:" << endl;
	cin >> isex;
	cout << "输入读者编号:" << endl;
	cin >> iNo;
	cout << "输入读者地址:" << endl;
	cin >> iaddress;
	Reader reader(iName, iage, isex, iNo, iaddress);
	reader.WriteData();
	cout << "Write Finish" << endl;
	Wait();
}
 
void ViewReaderData(int selpage = 1) {
	int page = 0;
	int curpage = 0;
	int datacount = 0;
	char inName[NUM1];
	char inage[NUM1];
	char insex[NUM2];
	char inNo[NUM2];
	char inaddress[NUM2];
	bool index = false;
	int filelength;
	curpage = selpage;
	ifstream file;
	file.open("F://Reader.dat", ios::binary);
	filelength = GetFileLength(file);
	datacount = filelength / (NUM1 + NUM1 + NUM2 + NUM2 + NUM2);
	if (datacount >= 1)index = true;
	page = datacount / 20 + 1;
	ClearScreen();
	cout << "共有记录" << datacount << "    ";
	cout << "共有页数" << page << "   ";
	cout << "当前页数" << curpage << "  ";
	cout << "     n: 显示下一页  m:返回" << endl;
	cout << setw(5) << "index";
	cout << setw(22) << "Name" << setw(22) << "Age";
	cout << setw(15) << "Sex" << setw(15) << "NO";
	cout << setw(15) << "Address" ;
	cout << endl;
	try {
		file.seekg((curpage - 1) * 20 * (NUM1 + NUM1 + NUM2 + NUM2 + NUM2), ios::beg);
		if (!file.fail()) {
			for (int i = 1; i < 21; i++) {
				memset(inName, 0, 128);
				memset(inage, 0, 128);
				memset(insex, 0, 50);
				memset(inNo, 0, 50);
				memset(inaddress, 0, 50);
				if (index) cout << setw(5) << (curpage - 1) * 20 + i;
				cout << setw(22);
				file.read(inName, NUM1);
				cout << inName;
				file.read(inage, NUM1);
				cout << setw(22);
				cout << inage;
				file.read(insex, NUM2);
				cout << setw(15);
				cout <<insex;
				file.read(inNo, NUM2);
				cout << setw(15);
				cout << inNo;
				file.read(inaddress, NUM2);
				cout << setw(15);
				cout << inaddress;
				cout << endl;
				if (file.tellg() < 0)index = false;
				else index = true;
			}
		}
	}
	catch (...) {
		cout << "throw file exception" << endl;
		throw"file error occurred";
		file.close();
	}
	if (curpage < page) {
		curpage = curpage + 1;
		WaitView(curpage);
	}
	else WaitView(curpage);
	file.close();
}
 
void DelectReaderFromFile() {
	char readerno[NUM2];
	cout << "Input delect ReaderNo:" << endl;
	cin >> readerno;
	int count = 0;
	fstream file;
	file.open("F://Reader.dat", ios::binary | ios::in | ios::out);
	file.seekg(0, ios::end);
	int res = file.tellg();
	int a = NUM1 + NUM1 + NUM2 + NUM2 + NUM2;
	char buf[NUM2];
	file.seekg(0, ios::beg);
	for (int j = NUM2; j <= res; j = j + a) {
		count += 1;
		file.seekg(NUM2, ios::cur);
		memset(buf, 0, NUM2);
		file.read(buf, NUM2);
		if (readerno >= buf) {
			break;
		}
		file.seekg(a - NUM2, ios::cur);
	}
	file.close();
	Reader tmpbook;
	tmpbook.DeleteData(count);
 
	int count2 = 0;
	fstream file2;
	file2.open("F://Borrow.dat", ios::binary | ios::in | ios::out);
	file2.seekg(0, ios::end);
	int res2 = file2.tellg();
	int a2 = NUM2 + NUM2 + NUM2 + NUM2;
	char buf2[NUM2];
	file2.seekg(0, ios::beg);
	for (int j2 = NUM2; j2 <= res2; j2 = j2 + a2) {
		count2 += 1;
		file2.seekg(NUM2, ios::cur);
		memset(buf2, 0, NUM2);
		file2.read(buf2, NUM2);
		if (readerno >= buf2) {
			break;
		}
		file2.seekg(a2 - NUM2, ios::cur);
	}
	file2.close();
	Borrow tmpbook2;
	tmpbook2.DeleteData(count2);
	cout << "Delect Finish" << endl;
	system("pause");
	Wait();
}
 
Borrow::Borrow(char* bookno, char* readerno, char* outtime, char* intime) {
	strncpy(n_bookno, bookno, NUM2);
	strncpy(n_readerno, readerno, NUM2);
	strncpy(n_outtime, outtime, NUM2);
	strncpy(n_intime, intime, NUM2);
}
 
char* Borrow::Getbookno() {
	return n_bookno;
}
void Borrow::Setbookno(char* bookno) {
	strncpy(n_bookno, bookno, NUM2);
}
 
char* Borrow::Getreaderno() {
	return n_readerno;
}
void Borrow::Setreaderno(char* readerno) {
	strncpy(n_readerno, readerno, NUM2);
}
 
char* Borrow::Getouttime() {
	return n_outtime;
}
void Borrow::Setouttime(char* outtime) {
	strncpy(n_outtime, outtime, NUM2);
}
 
char* Borrow::Getintime() {
	return n_intime;
}
void  Borrow::Setintime(char* intime) {
	strncpy(n_intime, intime, NUM2);
}
 
void Borrow::WriteData() {
	ofstream file;
	file.open("F://Borrow.dat", ios::binary | ios::app);
	try {
		file.write(n_bookno, NUM2);
		file.write(n_readerno, NUM2);
		file.write(n_outtime, NUM2);
		file.write(n_intime, NUM2);
	}
	catch (...) {
		throw "file error occurred";
		file.close();
	}
	file.close();
}
 
void Borrow::GetBorrowFromFile(int count) {
	char bookno[NUM2];
	char readerno[NUM2];
	char outtime[NUM2];
	char intime[NUM2];
	ifstream file;
	file.open("F://Borrow.dat", ios::binary);
	try {
		file.seekg(count*(NUM2 + NUM2 + NUM2 + NUM2), ios::beg);
		file.read(bookno, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_bookno, bookno, NUM2);
		}
		file.read(readerno, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_readerno, readerno, NUM2);
		}
		file.read(outtime, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_outtime, outtime, NUM2);
		}
		file.read(intime, NUM2);
		if (file.tellg() > 0) {
			strncpy(n_intime, intime, NUM2);
		}
	}
	catch (...) {
		throw"file error occurred";
		file.close();
	}
	file.close();
}
 
void Borrow::DeleteData(int count) {
	long respos;
	int DataCount = 0;
	fstream file;
	fstream tmpfile;
	ofstream ofile;
	char TempBuf[NUM2 + NUM2 + NUM2 + NUM2];
	file.open("F://Borrow.dat", ios::binary | ios::in | ios::out);
	tmpfile.open("F://tempBorrow.dat", ios::binary | ios::in | ios::out | ios::trunc);
	file.seekg(0, ios::end);
	respos = file.tellg();
	DataCount = respos / (NUM2 + NUM2 + NUM2 + NUM2);
	if (count<0 && count>DataCount) {
		throw"Input number error";
	}
	else {
		file.seekg(count*(NUM2 + NUM2 + NUM2 + NUM2), ios::beg);
		for (int j = 0; j < (DataCount - count); j++) {
			memset(TempBuf, 0, NUM2 + NUM2 + NUM2 + NUM2);
			file.read(TempBuf, NUM2 + NUM2 + NUM2 + NUM2);
			tmpfile.write(TempBuf, NUM2 + NUM2 + NUM2 + NUM2);
		}
		file.close();
		tmpfile.seekg(0, ios::beg);
		ofile.open("F://Borrow.dat");
		ofile.seekp((count - 1)*(NUM2 + NUM2 + NUM2 + NUM2), ios::beg);
		for (int i = 0; i < (DataCount - count); i++) {
			memset(TempBuf, 0, NUM2 + NUM2 + NUM2 + NUM2);
			tmpfile.read(TempBuf, NUM2 + NUM2 + NUM1 + NUM2);
			ofile.write(TempBuf, NUM2 + NUM2 + NUM2 + NUM2);
		}
	}
	tmpfile.close();
	ofile.close();
	remove("tempBorrow.dat");
}
 
void AddBorrow() {
	char ibookno[NUM2];
	char ireaderno[NUM2];
	char iouttime[NUM2];
	char iintime[NUM2];
	cout << "输入图书编号:" << endl;
	cin >> ibookno;
	cout << "输入读者编号:" << endl;
	cin >> ireaderno;
	cout << "输入借出时间:" << endl;
	cin >> iouttime;
	cout << "输入应还时间:" << endl;
	cin >> iintime;
	Borrow borrow(ibookno, ireaderno, iouttime, iintime);
	borrow.WriteData();
	cout << "Write Finish" << endl;
	Wait();
}
 
void ViewBorrowData(int selpage = 1) {
	int page = 0;
	int curpage = 0;
	int datacount = 0;
	char inbookno[NUM2];
	char inreaderno[NUM2];
	char inouttime[NUM2];
	char inintime[NUM2];
	bool index = false;
	int filelength;
	curpage = selpage;
	ifstream file;
	file.open("F://Borrow.dat", ios::binary);
	filelength = GetFileLength(file);
	datacount = filelength / (NUM2 + NUM2 + NUM2 + NUM2);
	if (datacount >= 1)index = true;
	page = datacount / 20 + 1;
	ClearScreen();
	cout << "共有记录" << datacount << "    ";
	cout << "共有页数" << page << "   ";
	cout << "当前页数" << curpage << "  ";
	cout << "     n: 显示下一页  m:返回" << endl;
	cout << setw(5) << "index";
	cout << setw(15) << "图书编号" << setw(15) << "读者编号";
	cout << setw(15) << "借出时间" << setw(15) << "应还时间";
	cout << endl;
	try {
		file.seekg((curpage - 1) * 20 * (NUM2+ NUM2 + NUM2 + NUM2), ios::beg);
		if (!file.fail()) {
			for (int i = 1; i < 21; i++) {
				memset(inbookno, 0, 50);
				memset(inreaderno, 0, 50);
				memset(inouttime, 0, 50);
				memset(inintime, 0, 50);
				if (index) cout << setw(5) << (curpage - 1) * 20 + i;
				cout << setw(15);
				file.read(inbookno, NUM2);
				cout << inbookno;
				file.read(inreaderno, NUM2);
				cout << setw(15);
				cout << inreaderno;
				file.read(inouttime, NUM2);
				cout << setw(15);
				cout << inouttime;
				file.read(inintime, NUM2);
				cout << setw(15);
				cout << inintime;
				cout << endl;
				if (file.tellg() < 0)index = false;
				else index = true;
			}
		}
	}
	catch (...) {
		cout << "throw file exception" << endl;
		throw"file error occurred";
		file.close();
	}
	if (curpage < page) {
		curpage = curpage + 1;
		WaitView(curpage);
	}
	else WaitView(curpage);
	file.close();
}
 
void DelectBorrowFromFile() {
	char borrow1[NUM2], borrow2[NUM2];
	cout << "请输入图书编号:" << endl;
	cin >> borrow1;
	cout << "请输入读者编号:" << endl;
	cin >> borrow2;
	int count = 0;
	fstream file;
	file.open("F://Borrow.dat", ios::binary | ios::in | ios::out);
	file.seekg(0, ios::end);
	int res = file.tellg();
	int a = NUM2 + NUM2 + NUM2+ NUM2;
	char buf1[NUM2],buf2[NUM2];
	file.seekg(0, ios::beg);
	for (int j = NUM2; j <= res; j = j + a) {
		count += 1;
		file.seekg(NUM2, ios::cur);
		memset(buf1, 0, NUM2);
		file.read(buf1, NUM2);
		file.seekg(NUM2, ios::cur);
		memset(buf2, 0, NUM2);
		file.read(buf2, NUM2);
		if ((borrow1>=buf1)&&(borrow2>=buf2)) {
			break;
		}
		file.seekg(a - NUM2-NUM2, ios::cur);
	}
	file.close();
	Borrow tmpbook;
	tmpbook.DeleteData(count);
	cout << "Delect Finish" << endl;
	system("pause");
	Wait();
}
 
void main() {
	ShowWelcome();
	while (1) {
		ClearScreen();
		ShowWelcome();
		ShowMenu();
		switch (GetSelect()) {
		case 1:
			ClearScreen();
			AddBook();
			break;
		case 2:
			ClearScreen();
			ViewData();
			break;
		case 3:
			ClearScreen();
			DelectBookFromFile();
			break;
		case 4:
			ClearScreen();
			AddReader();
			break;
		case 5:
			ClearScreen();
			ViewReaderData();
			break;
		case 6:
			ClearScreen();
			DelectReaderFromFile();
			break;
		case 7:
			ClearScreen();
			AddBorrow();
			break;
		case 8:
			ClearScreen();
			ViewBorrowData();
			break;
		case 9:
			ClearScreen();
			DelectBorrowFromFile();
			break;
		}
	}
}