溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

簡單圖書管理系統(tǒng)

發(fā)布時間:2020-07-01 12:38:48 來源:網(wǎng)絡 閱讀:328 作者:botaorain 欄目:移動開發(fā)
// 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();    //輸出
//定義一個結(jié)構(gòu)體,表示書籍的信息
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};
//定義一個枚舉類型,讓所有的函數(shù)返回一個枚舉類型的結(jié)果
enum status
    {
    OK,FAILED,EMPTY,NOTINIT,OVERFLOW1,NULLHEAD//OVERFLOW在visual studio無法使用,改為OVERFLOW1
    };
void bookFirst()
    {
          
        ofstream of("book.dat",ios::out|ios::binary);  //定義文件輸出流,文件不存在時創(chuàng)建文件
        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,"");   //初始化頭結(jié)點
        (head->book).bookCount=0;
        (head->book).bookId=0;
    //  cout<<"LinkList已初始化......."<<endl;
        return head;
    }
 //創(chuàng)建單鏈表 ,增加元素    //將文件中的書籍信息初始化到鏈表中 。。。。然后再進行操作
status LinkList_Creat(LinkList head,ElemType e)
    {
          
         LinkList q;
         q=head;
         while(q->next!=NULL)    //讓指針指到最后
             q=q->next;
         //ElemType e;   //從文件中讀取     for循環(huán)
         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)         //系統(tǒng)選擇函數(shù)
    {
    //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的值來判斷執(zhí)行什么操作
    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();   //初始化一個帶頭結(jié)點的鏈表
        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);  //對圖書館執(zhí)行操作函數(shù)
        return 0;
    }


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI