// Library_botao.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#define ElemType Book //宏定義
#define LIST_INT_SIZE 1 //改為1,改用ListInsert增加空間
#include
#include
#include
#include "malloc.h"
#include
using namespace std;
void bookFirst(); //先初始化書(shū)籍
void bookOut(); //輸出
//定義一個(gè)結(jié)構(gòu)體,表示書(shū)籍的信息
struct Book
{
int bookId;
char bookName[20];
char bookAuthor[20]; //作者
int bookCount;
//char bookLender[20]; //借閱者
};
//每次初始化書(shū)籍時(shí),給定一本書(shū)固定的bookId號(hào),不會(huì)改變 //同一種書(shū)用同一個(gè)書(shū)號(hào)
/************************************************************************/
/* 定義線性表,用于對(duì)圖書(shū)號(hào)建索引,加快查詢速度 */
/************************************************************************/
//定義一個(gè)順序表
struct Sqlist{
ElemType *elem;
int length;
int listsize;
}L={NULL,0,0};
//定義一個(gè)枚舉類(lèi)型,讓所有的函數(shù)返回一個(gè)枚舉類(lèi)型的結(jié)果
enum status
{
OK,FAILED,EMPTY,NOTINIT,OVERFLOW1,NULLHEAD//OVERFLOW在visual studio無(wú)法使用,改為OVERFLOW1
};
void bookFirst()
{
ofstream of("book.dat",ios::out|ios::binary); //定義文件輸出流,文件不存在時(shí)創(chuàng)建文件
if (!of)
{
cout<<"The file open error!"<>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"<bookId;
cout<<" "<<"bookNname:"<bookName;
cout<<" "<<"bookAuthor:"<bookAuthor;
cout<<" "<<"bookCunt:"<bookCount;
cout<next=NULL;
strcpy((head->book).bookName,""); //初始化頭結(jié)點(diǎn)
(head->book).bookCount=0;
(head->book).bookId=0;
// cout<<"LinkList已初始化......."<next!=NULL) //讓指針指到最后
q=q->next;
//ElemType e; //從文件中讀取 for循環(huán)
ifstream inFile("book.dat",ios::in|ios::binary);
if (!inFile)
{
cout<<"Open File error!"<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) //查詢書(shū)籍
{
LinkList q;
q=head;
int n=0;
while(NULL!=q->next)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
cout<<"您要查詢的書(shū)籍為:"<next->book.bookId<<" "<next->book.bookId<<" "<next->book.bookAuthor<<" "<next->book.bookCount<next;
}
if (n==0)
{
cout<<"不好意思,本館暫時(shí)沒(méi)有你們要借的書(shū)。。。。。。。。。。。。"<next!=NULL)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
q->next->book.bookCount++;
}
q=q->next;
}
if (n==0) //說(shuō)明以前圖書(shū)館不存在該書(shū)
{
q->next=(Lnode *)malloc(sizeof(Lnode));
q->next->book=e;
q->next->book.bookCount=1; //圖書(shū)館有了這樣的書(shū) 1本
}
q->next->next=NULL;
return OK;
}
//借書(shū)
status LinkList_Lend(LinkList head, ElemType e)
{
//遍歷鏈表看要借的書(shū)是否在管中,若在,返回書(shū)籍,不在返回信息
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) //圖書(shū)館中還有該書(shū),返回該書(shū)信息,修改書(shū)籍信息
{
q->next->book.bookCount--;
cout<<"您要借的書(shū):"<next->book.bookId<<" "<next->book.bookName<<" "<next->book.bookAuthor<next;
}
if (n==0)
{
cout<<"不好意思,您要的書(shū)本館暫時(shí)沒(méi)有。。。。"<next)
{
if (strcmp(q->next->book.bookName,e.bookName)==0)
{
n++;
q->next->book.bookCount++;
}
q=q->next;
}
if (n==0)
{
cout<<"對(duì)不起,您借的書(shū)不是本館的書(shū)........"<next)
{
cout<<"單鏈表中沒(méi)有元素!"<next;
int i=0;
while(NULL!=p)
{
cout<book.bookId<<" "<book.bookName<<" "<book.bookAuthor<<" "<book.bookCount<next;
}
}
status AddFile(LinkList head) //將修改后的圖書(shū)館信息重新寫(xiě)回文件
{
ofstream outf("book.dat",ios::out|ios::binary); //打開(kāi)文件輸出流
LinkList p;
p=head;
if (!outf)
{
cout<<"Open Flie error......"<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) //系統(tǒng)選擇函數(shù)
{
//InitList_sq(L,5); //線性表完成加載
//ListInput_Sq(L);
cout<<" Welcome to Library"<>n;
cout<>book.bookName;
search(head ,book);
select_L(head);
break;
case 3:
//借書(shū) 。。。。。。。。。
cout<<"請(qǐng)輸入您要借的書(shū)的名字:"<>book.bookName;
LinkList_Lend(head,book);
AddFile(head); //將借書(shū)后的書(shū)籍信息重新再寫(xiě)到文件中
select_L(head);
break;
case 4:
//還書(shū) 。。。。。。。。。。。
cout<<"請(qǐng)輸入您想還的書(shū)的名字:"<>book.bookName;
LinkList_Return(head,book);
AddFile(head);
select_L(head);
break;
case 5:
//新書(shū)采編入庫(kù)...............
cout<<"輸入新書(shū)的名字和書(shū)號(hào):"<>book.bookId;
cout<<"bookName:"<>book.bookName;
cout<<"bookAuthor:"<>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(); //初始化一個(gè)帶頭結(jié)點(diǎn)的鏈表
ElemType e;
strcpy(e.bookName,"J+++");
strcpy(e.bookAuthor,"boo");
e.bookId=1001;
e.bookCount=1;
LinkList_Creat(head,e);
cout<<"圖書(shū)館信息已加載........."<
分享題目:簡(jiǎn)單圖書(shū)管理系統(tǒng)
轉(zhuǎn)載來(lái)源:http://weahome.cn/article/psgojd.html