main.cpp:
#include <iostream>
using namespace std;
//main() 函数的实现,程序的主界面的引导
int main()
{
char choice;
BDatabase BookDB;
while(choice!='0')
{
jiemian1();
cin>>choice;
switch(choice)
{
case '1':
BookDB.bookdata();
break;
case '0':
cout<<"\n\n\t\t\t欢迎再次使用图书管理系统\n\n";
break;
default:cout<<"\n\n\t\t\t输入错误,请从新输入:\n\n";
}
}
return 0;
}
book.cpp:
#include "book.h"
#include <iostream>
using namespace std;
int i=0;
/*********************************************************
* 功能描述: 构造函数,将book.txt读到book[]中
* 输入参数:
* 输出参数:
* 返回值 :
* 其它说明: BDatabase类中函数
************************************************************/
BDatabase::BDatabase()
{
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();
}
/*********************************************************
* 功能描述: 增加图书
* 输入参数:图书编号 类型 书名
* 输出参数:
* 返回值 :0
* 其它说明: BDatabase类中函数
************************************************************/
int BDatabase::addbook(int n,char *na,char *kin) //增加图书
{
Book *p=query(n);
if(NULL==p)
{
top++;
book[top].addbook(n,na,kin);
return 1;
}
return 0;
}
/*********************************************************
* 功能描述: 查找图书
* 输入参数:图书编号
* 输出参数:图书名称和图书类型
* 返回值 :NULL
* 其它说明: BDatabase类中函数
************************************************************/
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;
}
/*********************************************************
* 功能描述: 显示函数
* 输入参数:
* 输出参数:
* 返回值 :
* 其它说明: BDatabase类中函数
************************************************************/
void BDatabase:: disp()
{
for (int i=0; i<=top; i++)
if (book[i].gettag()==0)
book[i].disp();
}
/*********************************************************
* 功能描述: 析构函数
* 输入参数:
* 输出参数:
* 返回值 :
* 其它说明: BDatabase类中函数
************************************************************/
BDatabase::~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();
}
/*********************************************************
* 功能描述: 实现菜单选择功能
* 输入参数: 1~6
* 输出参数: 各个分块功能函数
* 返回值 : 各个分块功能函数结果
* 其它说明:
************************************************************/
void BDatabase::bookdata()
{
char choice;
char bname[40];
char bkind[40];
char newType[20];
int bookid;
Book *b;
while (choice!='0')
{
jiemian2();
cin>>choice;
switch (choice)
{
case '1':
cout<<"输入图书编号:"<<endl;
cin>>bookid;
cout<<"输入图书类型:"<<endl;
cin>>newType;
cout<<"输入图书书名:"<<endl;
cin>>bname;
addbook(bookid,bname,newType);
break;
case '2':
cout<<"输入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
cout<<"输入新的书名:"<<endl;
cin>>bname;
b->setname(bname);
cout<<"输入新的类型:"<<endl;
cin>>bkind;
b->setkind(bkind);
break;
case '3':
cout<<"读入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if(b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
char tg1;
cout<<"确定要删除此记录吗?(Y/N)"<<endl;
cin>>tg1;
if(tg1!='n'&&tg1!='N')
{
b->delbook();
cout<<"已成功删除该书记录!"<<endl;
break;
}
case '4':
cout<<"读入图书编号:"<<endl;
cin>>bookid;
b=query(bookid);
if (b==NULL)
{
cout<<"该图书不存在"<<endl;
break;
}
cout<<"图书编号 图书名称 图书类型 \n";
b->disp();
break;
case '5':
cout<<"图书编号 图书名称 图书类型 \n";
disp();
break;
case '6':
char tg2;
cout<<"确定要清空所有记录吗?(Y/N)"<<endl;
cin>>tg2;
if(tg2!='n'&&tg2!='N')
{
clear();
cout<<"已成功清空记录!"<<endl;
break;
}
case '0':
break;
default:
cout<<"\n\n\t\t\t输入错误,请从新输入(数字为0~6):\n\n";
}
}
}
/*********************************************************
* 功能描述: 开始引导界面
* 输入参数: 无
* 输出参数: 界面
* 返回值 : 无
* 其它说明:
************************************************************/
void jiemian1()
{
cout<<"\t******************************************************************\n";
cout<<" \n\n";
cout<<"\n\n\t\t\t欢迎使用图书管理系统\n\n";
cout<<endl<<endl<<"\t\t\t图 书 管 理 系 统\n\n\n\n";
cout<<setw(83)<<"by (姓名——李艺)\n\n\n\n";
cout<<" \n\n";
cout<<"\t******************************************************************\n";
cout<<"\t\t\t1 进 入 系 统\n\n\t\t\t0 离 开"<<endl;
if(i==0)
{
cout<<"请选择: ";
i++;
}
else
{
cout<<"请从新输入:";
i--;
}
}
/*********************************************************
* 功能描述: 菜单界面
* 输入参数: 无
* 输出参数: 界面
* 返回值 : 无
* 其它说明:
************************************************************/
void jiemian2()
{
cout<<"\t******************************************************************\n";
cout<<" \n\n";
cout<<"\t\t\t图 书 管 理 系 统\n\n";
cout<<"\t\t\t1 新增图书记录\n";
cout<<"\t\t\t2 更改图书记录\n";
cout<<"\t\t\t3 删除图书记录\n";
cout<<"\t\t\t4 查找图书记录\n";
cout<<"\t\t\t5 显示图书记录\n";
cout<<"\t\t\t6 清空全部记录(慎用)\n";
cout<<"\t\t\t0 退出系统\n";
cout<<" \n\n";
cout<<"\t******************************************************************\n";
cout<<"请选择:";
}
book.h:
#define StrNum 20
#define Type 20
#include <iomanip>
#include <string.h>
#include <fstream> //输入/输出文件流类
#include <iostream>
using namespace std;
const int Maxb=100; //最多可存储的图书
void jiemian1();
void jiemian2();
class Book
{
private:
int tag; //删除标记
int num; //图书编号
int add; //增加标记
char number[StrNum]; //编号
char name[StrNum]; //书名
char kind[Type]; //图书类型
public:
Book() {}
char getkind() //获取图书类型
{
return kind[Type];
}
char getname() //获取书名
{
return name[StrNum];
}
int getnum() //获取图书编号
{
return num;
}
int gettag() //获取删除标记
{
return tag;
}
void setname(char na[]) //设置书名
{
strcpy(name,na);
}
void setkind(char kin[]) //设置类型
{
strcpy(kind,kin);
}
void delbook() //删除图书
{
tag=1;
}
void addbook(int n,char *na,char *kin) //增加图书
{
tag=0;
num=n;
strcpy(name,na);
strcpy(kind,kin);
}
void disp() //输出图书
{
cout.flags (ios::left); //设置对齐的标志位为左
cout<<setw(15)<<num<<setw(20)<<name<<setw(15)<<kind<<endl;
}
};
/*****************************************************************************************/
class BDatabase
{
private:
int top; //图书记录指针
Book book[Maxb]; //图书记录
public:
BDatabase(); //构造函数,将book.txt读到book[]中
void clear()
{
top=-1; //全删
}
int addbook(int ,char *,char *); //增加图书
Book *query(int ); //查找图书
void bookdata(); //图书库
void disp();
~BDatabase(); //析构函数,将book[]写到book.txt文件中
};