// Library_botao.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define ElemType Book //宏定义
#define LIST_INT_SIZE 1 //改为1,改用ListInsert增加空间
#include <iostream>
#include <string.h>
#include <stdio.h>
#include "malloc.h"
#include <fstream>
using namespace std;
void bookFirst(); //先初始化书籍
void bookOut(); //输出
//定义一个结构体,表示书籍的信息
struct Book
{
int bookId;
char bookName[20];
char bookAuthor[20]; //作者
int bookCount;
//char bookLender[20]; //借阅者
};
//每次初始化书籍时,给定一本书固定的bookId号,不会改变 //同一种书用同一个书号
/************************************************************************/
/* 定义线性表,用于对图书号建索引,加快查询速度 */
/************************************************************************/
//定义一个顺序表
struct Sqlist{
ElemType *elem;
int length;
int listsize;
}L={NULL,0,0};
//定义一个枚举类型,让所有的函数返回一个枚举类型的结果
enum status
{
OK,FAILED,EMPTY,NOTINIT,OVERFLOW1,NULLHEAD//OVERFLOW在visual studio无法使用,改为OVERFLOW1
};
void bookFirst()
{
ofstream of("book.dat",ios::out|ios::binary); //定义文件输出流,文件不存在时创建文件
if (!of)
{
cout<<"The file open error!"<<endl;
}
else
{
Book *book=new Book;
cout<<"bookId:";
cin>>book->bookId;
cout<<"bookName:";
cin>>book->bookName;
cout<<"bookAuthor:";
cin>>book->bookAuthor;
cout<<"bookCount:";
cin>>book->bookCount;
of.write((char*)book,sizeof(Book));
}
of.close();
ofstream of1("book.dat",ios::app|ios::binary);
Book *book1=new Book;
cout<<"bookId:";
cin>>book1->bookId;
cout<<"bookName:";
cin>>book1->bookName;
cout<<"bookAuthor:";
cin>>book1->bookAuthor;
cout<<"bookCount:";
cin>>book1->bookCount;
of1.write((char*)book1,sizeof(Book));
of1.close();
}
void bookOut()
{
ifstream inFile("book.dat",ios::in|ios::binary);
if (! inFile)
{
cout<<"The file open error"<<endl;
}
else
{
Book *b=new Book;
inFile.read((char*)b,sizeof(Book));
cout<<"bookId:"<<b->bookId;
cout<<" "<<"bookNname:"<<b->bookName;
cout<<" "<<"bookAuthor:"<<b->bookAuthor;
cout<<" "<<"bookCunt:"<<b->bookCount;
cout<<endl;
}
inFile.close();
}
/************************************************************************/
/* 定义一个链式表进行对书籍信息的操作 */
/************************************************************************/
typedef struct Lnode
{
ElemType book;
struct Lnode *next;
}Lnode, *LinkList;
//单链表初始化
LinkList LinkListInit()
{
LinkList head;
head=(Lnode *)malloc(sizeof(Lnode));
if (NULL==head)
{
cout<<"error:申请空间失败!"<<endl;
}
head->next=NULL;
strcpy((head->book).bookName,""); //初始化头结点
(head->book).bookCount=0;
(head->book).bookId=0;
// cout<<"LinkList已初始化......."<<endl;
return head;
}
//创建单链表 ,增加元素 //将文件中的书籍信息初始化到链表中 。。。。然后再进行操作
status LinkList_Creat(LinkList head,ElemType e)
{
LinkList q;
q=head;
while(q->next!=NULL) //让指针指到最后
q=q->next;
//ElemType e; //从文件中读取 for循环
ifstream inFile("book.dat",ios::in|ios::binary);
if (!inFile)
{
cout<<"Open File error!"<<endl;
}
else
{
Book *b=new Book;
while(inFile.read((char *)b,sizeof(Book)))
{
q->next=(Lnode *)malloc(sizeof(Lnode));
strcpy(q->next->book.bookName,b->bookName);
strcpy(q->next->book.bookAuthor,b->bookAuthor);
q->next->book.bookId=b->bookId;
q->next->book.bookCount=b->bookCount;
b=new Book;
q=q->next;
q->next=NULL;
}
inFile.close();
}
return OK;
}
status search(LinkList head,ElemType e) //查询书籍
{
LinkList q;
q=head;
int n=0;
while(NULL!=q->next)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
cout<<"您要查询的书籍为:"<<endl;
cout<<q->next->book.bookId<<" "<<q->next->book.bookId<<" "<<q->next->book.bookAuthor<<" "<<q->next->book.bookCount<<endl;
}
q=q->next;
}
if (n==0)
{
cout<<"不好意思,本馆暂时没有你们要借的书。。。。。。。。。。。。"<<endl;
}
return OK;
}
//增加一本新书
status LinkList_Add(LinkList head,ElemType e)
{
//遍历链表,在表的最后直接加上
LinkList q;
q=head;
int n=0; //判断图书馆是否已有该书的记录
while(q->next!=NULL)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
q->next->book.bookCount++;
}
q=q->next;
}
if (n==0) //说明以前图书馆不存在该书
{
q->next=(Lnode *)malloc(sizeof(Lnode));
q->next->book=e;
q->next->book.bookCount=1; //图书馆有了这样的书 1本
}
q->next->next=NULL;
return OK;
}
//借书
status LinkList_Lend(LinkList head, ElemType e)
{
//遍历链表看要借的书是否在管中,若在,返回书籍,不在返回信息
LinkList q;
q=head;
int n=0;
while(q->next!=NULL)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
if (q->next->book.bookCount>0) //图书馆中还有该书,返回该书信息,修改书籍信息
{
q->next->book.bookCount--;
cout<<"您要借的书:"<<q->next->book.bookId<<" "<<q->next->book.bookName<<" "<<q->next->book.bookAuthor<<endl;
}
else
{
cout<<"不好意思,您要的书已被别人借走。。。。。。"<<endl;
}
}
// else{
// cout<<"不好意思,您要的书本馆暂时没有。。。。"<<endl;
// }
q=q->next;
}
if (n==0)
{
cout<<"不好意思,您要的书本馆暂时没有。。。。"<<endl;
}
return OK;
}
//还书
status LinkList_Return(LinkList head,ElemType e)
{
//遍历链表找到该书籍的信息,然后修改
LinkList q;
q=head;
int n=0; //记录本本馆中是否有该书籍
while(NULL!=q->next)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
q->next->book.bookCount++;
}
q=q->next;
}
if (n==0)
{
cout<<"对不起,您借的书不是本馆的书........"<<endl;
}
return OK;
}
//输出单链表的元素
status LinkList_Cout(LinkList head)
{
if (NULL==head->next)
{
cout<<"单链表中没有元素!"<<endl;
return NULLHEAD;
}
LinkList p;
p=head->next;
int i=0;
while(NULL!=p)
{
cout<<i++<<"..."<<p->book.bookId<<" "<<p->book.bookName<<" "<<p->book.bookAuthor<<" "<<p->book.bookCount<<endl;
p=p->next;
}
}
status AddFile(LinkList head) //将修改后的图书馆信息重新写回文件
{
ofstream outf("book.dat",ios::out|ios::binary); //打开文件输出流
LinkList p;
p=head;
if (!outf)
{
cout<<"Open Flie error......"<<endl;
}
else
{
outf.seekp(0); //将文件指针定位到文件头部
Book *b=new Book;
while(NULL!=p->next)
{
(*b)=p->next->book; //将链表中的Book信息赋给(*b)
outf.write((char *)b,sizeof(Book));
b=new Book;
p=p->next;
}
outf.close();
}
return OK;
}
status select_L(LinkList head) //系统选择函数
{
//InitList_sq(L,5); //线性表完成加载
//ListInput_Sq(L);
cout<<" Welcome to Library"<<endl;
cout<<"*********************************************************"<<endl;
cout<<"查看图书里的书.......请输入1:"<<endl;
cout<<"查询图书里的书.......请输入2:"<<endl;
cout<<"借书.................请输入3:"<<endl;
cout<<"还书.................请输入4:"<<endl;
cout<<"新书采编入库.........请输入5:"<<endl;
cout<<"退出.................请输入6:" <<endl;
cout<<"*********************************************************"<<endl;
int n; //输入n ,通过n的值来判断执行什么操作
cin>>n;
cout<<endl;
switch(n)
{
case 1:
//查看图书馆里的书。。。。。。。。
LinkList_Cout(head);
select_L(head);
break;
case 2:
//查询书籍。。。。。。。。
ElemType book;
cout<<"请输入您要查询的书的名字:"<<endl;
cin>>book.bookName;
search(head ,book);
select_L(head);
break;
case 3:
//借书 。。。。。。。。。
cout<<"请输入您要借的书的名字:"<<endl;
cin>>book.bookName;
LinkList_Lend(head,book);
AddFile(head); //将借书后的书籍信息重新再写到文件中
select_L(head);
break;
case 4:
//还书 。。。。。。。。。。。
cout<<"请输入您想还的书的名字:"<<endl;
//ElemType book1;
cin>>book.bookName;
LinkList_Return(head,book);
AddFile(head);
select_L(head);
break;
case 5:
//新书采编入库...............
cout<<"输入新书的名字和书号:"<<endl;
cout<<"bookId:"<<endl;
cin>>book.bookId;
cout<<"bookName:"<<endl;
cin>>book.bookName;
cout<<"bookAuthor:"<<endl;
cin>>book.bookAuthor;
LinkList_Add(head,book);
AddFile(head);
select_L(head);
break;
case 6:
exit(0);
break;
default:
break;
}
return OK;
}
int main()
{
// bookFirst();
// bookOut();
LinkList head=LinkListInit(); //初始化一个带头结点的链表
ElemType e;
strcpy(e.bookName,"J+++");
strcpy(e.bookAuthor,"boo");
e.bookId=1001;
e.bookCount=1;
LinkList_Creat(head,e);
cout<<"图书馆信息已加载........."<<endl;
select_L(head); //对图书馆执行操作函数
return 0;
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。