#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
图书管理系统——C++实现
需积分: 49 151 浏览量
2018-07-10
21:55:16
上传
评论 7
收藏 14.53MB ZIP 举报
Happyunlim
- 粉丝: 92
- 资源: 3
最新资源
- 校园帮项目,毕业设计/课程设计/javaWeb/SSM
- C++ plotting library,matplotlib-cpp-master.zip
- 案例源码matplotlib-examples-master.zip
- 基于JavaScript 实现的KMP 算法
- 基于C++实现二叉树的创建,遍历,添加,查找与删除
- 基于C语言实现二叉树的基本操作
- 毕业设计基于STM32的测量温度与压力的数据处理设计C语言完整源码+论文.zip
- 基于MATLAB的PCA算法人脸识别项目源码+GUI界面+说明文档.zip
- 基于STM32的测量温度与压力的数据处理设计源码+论文(毕业设计).zip
- Vision Transformer 网络对不同氨气氧气浓度轨迹RAS 图像数据集的分类,包含训练权重和数据集、迁移学习
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈