简单图书管理系统
// Library_botao.cpp : 定义控制台应用程序的入口点。 // #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(); //先初始化书籍 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!"< >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,""); //初始化头结点 (head->book).bookCount=0; (head->book).bookId=0; // cout<<"LinkList已初始化......."< next!=NULL) //让指针指到最后 q=q->next; //ElemType e; //从文件中读取 for循环 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) //查询书籍 { LinkList q; q=head; int n=0; while(NULL!=q->next) { if (strcmp(q->next->book.bookName,e.bookName)==0) { n++; cout<<"您要查询的书籍为:"< next->book.bookId<<" "< next->book.bookId<<" "< next->book.bookAuthor<<" "< next->book.bookCount< next; } if (n==0) { cout<<"不好意思,本馆暂时没有你们要借的书。。。。。。。。。。。。"< 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<<"您要借的书:"< next->book.bookId<<" "< next->book.bookName<<" "< next->book.bookAuthor< next; } if (n==0) { cout<<"不好意思,您要的书本馆暂时没有。。。。"< next) { if (strcmp(q->next->book.bookName,e.bookName)==0) { n++; q->next->book.bookCount++; } q=q->next; } if (n==0) { cout<<"对不起,您借的书不是本馆的书........"< next) { cout<<"单链表中没有元素!"< next; int i=0; while(NULL!=p) { cout<book.bookId<<" "< book.bookName<<" "< book.bookAuthor<<" "< book.bookCount< next; } } status AddFile(LinkList head) //将修改后的图书馆信息重新写回文件 { ofstream outf("book.dat",ios::out|ios::binary); //打开文件输出流 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) //系统选择函数 { //InitList_sq(L,5); //线性表完成加载 //ListInput_Sq(L); cout<<" Welcome to Library"< >n; cout< >book.bookName; search(head ,book); select_L(head); break; case 3: //借书 。。。。。。。。。 cout<<"请输入您要借的书的名字:"< >book.bookName; LinkList_Lend(head,book); AddFile(head); //将借书后的书籍信息重新再写到文件中 select_L(head); break; case 4: //还书 。。。。。。。。。。。 cout<<"请输入您想还的书的名字:"< >book.bookName; LinkList_Return(head,book); AddFile(head); select_L(head); break; case 5: //新书采编入库............... cout<<"输入新书的名字和书号:"< >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(); //初始化一个带头结点的链表 ElemType e; strcpy(e.bookName,"J+++"); strcpy(e.bookAuthor,"boo"); e.bookId=1001; e.bookCount=1; LinkList_Creat(head,e); cout<<"图书馆信息已加载........."<
本文名称:简单图书管理系统
网页地址:http://scgulin.cn/article/psgojd.html