#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
//库存管理,买入本书,借出的本数,借出的时间
class Library{
int book_num; //买入本数
int borrow_num; // 借出的本数
string borrow_time;//借出的时间
public:
Library();
Library(int book_num,int borrow_num,string borrow_time)
{
this->book_num=book_num;
this->borrow_num=borrow_num;
this->borrow_time=borrow_time;
}
int Getbook_num()
{
return book_num;
}
int Getborrow_num()
{
return borrow_num;
}
string Getborrow_time()
{
return borrow_time;
}
private:
};
//图书管理,书名,出版时间,作者,入库时间,价格
class Book{
private:
string name; //名称
string press_time; //出版时间
string author; // 作者
string enter_time; //入库时间
double price; //价格
Library L; //图书馆信息;
Book* next;
public:
Book();
Book(string name/*名称*/,string press_time/*出版时间*/,string author/*作者*/,string enter_time/*入库时间*/,double price/*价格*/,int book_num/*数量*/,int borrow_num/*借出数*/,string borrow_time/*借出时间*/):L(book_num,borrow_num,borrow_time){
this->name=name;
this->press_time=press_time;
this->author=author;
this->enter_time=enter_time;
this->price=price;
next=NULL;
}
//结尾添加
void setNext(Book *p){next =p;}
Book* Getnext(){
return next;
}
string Getname(){
return name;
}
string Getpress_time(){
return press_time;
}
string Getauthor(){
return author;
}
string Getenter_time(){
return enter_time;
}
double Getprice(){
return price;
}
void show(){
cout<<"图书馆情况:"<<endl;
cout<<"图书馆买入本数:"<<L.Getbook_num()<<endl;
cout<<"图书馆借出的本数:"<<L.Getborrow_num()<<endl;
cout<<"图书借出时间:"<<L.Getborrow_time()<<endl;
}
};
//链表定义
class Linklist{
private:
Book* head;
public:
Linklist(){head=NULL;}
Linklist(string name,string press_time,string author,string enter_time,double price,int book_num,int borrow_num,string borrow_time){
head=new Book(name,press_time,author,enter_time,price,book_num,borrow_num,borrow_time);
}
void addAtEnd(string name,string press_time,string author,string enter_time,double price,int book_num,int borrow_num,string borrow_time);
void visitAllNode();
void deleteNum(string num);
bool findName(string name);
};
//遍历链表
void Linklist::visitAllNode()
{
Book* p;
p=head;
//输出
if(p==NULL){cout<<"空链表!"<<endl;}
else
{
while(p->Getnext()!=NULL)
{
cout<<"图书情况:"<<endl;
cout<<"书名:"<<p->Getname()<<endl;
cout<<"出版时间:"<<p->Getpress_time()<<endl;
cout<<"作者:"<<p->Getauthor()<<endl;
cout<<"入库时间:"<<p->Getenter_time()<<endl;
cout<<"价格:"<<p->Getprice()<<endl;
p->show();
cout<<endl;
p=p->Getnext();
}
cout<<"图书情况:"<<endl;
cout<<"书名:"<<p->Getname()<<endl;
cout<<"出版时间:"<<p->Getpress_time()<<endl;
cout<<"作者:"<<p->Getauthor()<<endl;
cout<<"入库时间:"<<p->Getenter_time()<<endl;
cout<<"价格:"<<p->Getprice()<<endl;
p->show();
}
}
//往链表尾添加节点
void Linklist::addAtEnd(string name,string press_time,string author,string enter_time,double price,int book_num,int borrow_num,string borrow_time){
Book *p=head;
if(head==NULL){ head = new Book(name,press_time,author,enter_time,price,book_num,borrow_num,borrow_time);}
else
{
while(p->Getnext()!=NULL)
{ p= p->Getnext(); }/*用p来指向最后一个节点*/
p->setNext(new Book(name,press_time,author,enter_time,price,book_num,borrow_num,borrow_time));//插入节点
}
}
//删除节点
void Linklist::deleteNum(string num)
{
Book *p=head,*follow=p;
if(p->Getname()== num)//若删除的节点为第1个节点
{
head = p->Getnext();
delete p;
}
else
{
while(p->Getnext()!=NULL && p->Getname()!= num)
{
follow = p;
//follow一直指向p的前一个节点
p = p->Getnext();
}
if(p->Getname() == num)
{
follow->setNext(p->Getnext());
delete p;
}
}
}
//查找节点
bool Linklist::findName(string name){
Book *p =head;
if(p->Getname()== name)
return 1;
else{
while(p->Getnext()!=NULL){
p=p->Getnext();
if(p->Getname()==name)
{
return 1;
break;
}
}
return 0;
}
}
//主程序
int main(){
string name; //名称
string press_time; //出版时间
string author; // 作者
string enter_time; //入库时间
double price; //价格
int book_num; //买入本数
int borrow_num; // 借出的本数
string borrow_time;//借出的时间
char option;
Linklist *pl=NULL;
while(1)
{
cout<<"1: 创建图书链表链表"<<endl;
cout<<"2: 在链表尾插入新节点"<<endl;
cout<<"3: 查找书名为name的图书:"<<endl;
cout<<"4: 删除节点"<<endl;
cout<<"5: 遍历节点"<<endl;
cout<<"0: 退出"<<endl;
cout<<"\n请输入你的选择:";
cin>>option;
switch(option){
case '0':
break;
case '1':
cout<<"请输入第一本图书和图书馆的相关信息节点值:"<<endl<<"(名称,出版时间,作者,入库时间,价格,买入本数,借出本书,借出时间)"<<endl;
cin>>name>>press_time>>author>>enter_time>>price>>book_num>>borrow_num>>borrow_time;
pl = new Linklist(name,press_time,author,enter_time,price,book_num,borrow_num,borrow_time);
cout<<endl;
pl->visitAllNode();
break;
case '2':
cout<<"请输入新节点图书和图书馆信息的值:";
cin>>name>>press_time>>author>>enter_time>>price>>book_num>>borrow_num>>borrow_time;
if(pl==NULL)
{
pl = new Linklist(name,press_time,author,enter_time,price,book_num,borrow_num,borrow_time);
}
else
{
pl->addAtEnd(name,press_time,author,enter_time,price,book_num,borrow_num,borrow_time);
}
cout<<endl;
pl->visitAllNode();
break;
case '3':
cout<<"请输入查找的书名,如果找到返回1,否则返回0:";
cin>>name;
cout<<endl;
cout<<"输出查询结果:"<<pl->findName(name)<<endl;
break;
case '4':
cout<<"请输入要删除的节点的书名:";
cin>>name;
pl->deleteNum(name);
cout<<endl;
pl->visitAllNode();
break;
case '5':
pl->visitAllNode();
break;
default:
cout<<"你输入错误,请重新输入!"<<endl;
}
if(option == '0' )
break;
}
return 0;
}
版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。