| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 373 人关注过本帖
标题:图书馆管理系统
只看楼主 加入收藏
王辉428
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-18
结帖率:0
收藏
已结贴  问题点数:2 回复次数:1 
图书馆管理系统
            
[问题描述]
    设计一个图书管理系统,该图书管理系统分为借书,还书,图书维护和读者维护4个部分,如下图所示.
                    ┏━━━━━━━┓
                    ┃ 图书管理系统 ┃
                    ┗━━━┳━━━┛
      ┏━━━━━━┳━━━┻━━━┳━━━━━━┓
┏━━┻━━┓┏━━┻━━┓  ┏━━┻━━┓┏━━┻━━┓
   借书   ┃┃   还书     ┃ 图书维护 ┃┃ 读者维护 ┃
┗━━━━━┛┗━━━━━┛  ┗━━━━━┛┗━━━━━┛
                     图书管理系统示意图

[算法分析]
    设计一个读者类Reader,包括一个读者的基本管理功能,具有以下私有数据:
        int  tag;             //删除标记(0:未删,1:已删)
        int  no;              //读者编号
        char name[10];        //读者姓名
        int  borbook[Maxbor]; //所借图书
    设计一个读者类库RDatabase,具有以下私有数据:
        int    top;           //读者记录指针
        Reader read[Maxr];    //读者记录
    在其构造函数中,将reader.txt文件的所有记录读到read[]中.处理完毕,在析构函数中将read[]中的
所有未删记录写入到read.txt文件中,中间的处理都是对read[]进行的.
    设计一个图书类Book,包括一本图书的基本管理功能,私有数据包括:
        int  tag;             //删除标记(0:未删,1:已删)
        int  no;              //读者编号
        char name[20];        //读者姓名
        int  onshelf[Maxr];   //所借图书
    设计一个图书类库BDatabase,私有数据包括:
        int  top;             //读者记录指针
        Book book[Maxb];      //读者记录
    在其构造函数中,将book.txt文件的所有记录读到book[]中.处理完毕,在析构函数中将book[]中的所
有未删记录写入到book.txt文件中,中间的处理都是对book[]进行的.
*/
#include "iostream.h"
#include "iomanip.h"
#include "string.h"
#include "fstream.h"

const int Maxr   = 100;                 //最多读者数
const int Maxb   = 100;                 //最多图书数
const int Maxbor = 5;                   //每位读者最多借5本书

//读者类
class Reader{
private:                                //私有部分
    int  tag;                           //删除标记(0:未删,1:已删)
    int  no;                            //读者编号
    char name[10];                      //读者姓名
    int  borbook[Maxbor];               //所借图书
public:                                 //公有部分
    Reader();                           //构造函数
    char *getname();                    //获取姓名
    int  gettag();                      //获取删除标记
    int  getno();                       //获取读者编号
    void setname(char na[]);            //设置姓名
    void delbook();                     //设置删除标记
    void addreader(int n,char *na);     //增加读者
    void borrowbook(int bookid);        //借书操作
    int  retbook(int bookid);           //还书操作
    void display();                     //输出读者信息
    ~Reader();                          //析构函数
};

//读者库类
class RDatabase{
private:                                //私有部分
    int    top;                         //读者记录指针
    Reader read[Maxr];                  //读者记录
public:                                 //公有部分
    RDatabase();                        //构造函数,将reader.txt读到read[]中
    void   clear();                     //删除所有读者信息
    int    addreader(int n,char *na);   //添加读者时先查找是否存在
    Reader *query(int readerid);        //按编号查找
    void   display();                   //输出所有读者信息
    void   readerdata();                //读者库维护
    ~RDatabase();                       //析构函数,将read[]写入reader.txt文件中
};

//图书类
class Book{
private:                                //私有部分
    int  tag;                           //删除标记(0:未删,1:已删)
    int  no;                            //图书编号
    char name[20];                      //书名
    int  onshelf;                       //是否在架(0:已借,1:在架)
public:                                 //公有部分
    Book();                             //构造函数
    char *getname();                    //获取书名
    int  getno();                       //获取图书编号
    int  gettag();                      //获取删除标记
    void setname(char na[]);            //设置书名
    void delbook();                     //删除图书
    void addbook(int n,char *na);       //增加图书
    int  borrowbook();                  //借书操作
    void retbook();                     //还书操作
    void display();                     //输出图书信息
    ~Book();                            //析构函数
};

//图书库类
class BDatabase{
private:                                //私有部分
    int  top;                           //图书记录指针
    Book book[Maxb];                    //图书记录
public:                                 //公有部分
    BDatabase();                        //构造函数,将book.txt读到book[]中
    void clear();                       //删除所有图书信息
    int  addbook(int n,char *na);       //添加图书时先查找是否存在
    Book *query(int bookid);            //按编号查找
    void display();                     //输出所有可借的图书信息
    void bookdata();                    //图书库维护
    ~BDatabase();                       //析构函数,将book[]写入book.txt文件中
};
/***********************************************************************************/
//定义读者类Reader成员
Reader::Reader(){                       //构造函数
};
char *Reader::getname(){                //获取姓名
    return name;
};
int Reader::gettag(){                   //获取删除标记
    return tag;
};
int Reader::getno(){                    //获取读者编号
    return no;
};
void Reader::setname(char na[]){        //设置姓名
    strcpy(name,na);
};
void Reader::delbook(){                 //设置删除标记
    tag = 1;
};
void Reader::addreader(int n,char *na){ //增加读者
    tag = 0;
    no = n;
    strcpy(name,na);
    for(int i=0;i<Maxbor;i++){
        borbook[i] = 0;
    }
};
void Reader::borrowbook(int bookid){    //借书操作
    for(int i=0;i<Maxbor;i++){
        if(borbook[i]==0){
            borbook[i] = bookid;
            return;
        }
    }
};
int Reader::retbook(int bookid){        //还书操作
    for(int i=0;i<Maxbor;i++){
        if(borbook[i]==bookid){
            borbook[i] = 0;
            return 1;
        }
    }
    return 0;
};
void Reader::display(){                 //输出读者信息
    cout << setw(5) << no << setw(10) << name << setw(20) << "借书编号:[";
    for(int i=0;i<Maxbor;i++){
        if(borbook[i]!=0){
            cout << borbook[i];
            if(borbook[i+1]!=0){ cout << "|"; }
        }
    }
    cout << "]" << endl;
};
Reader::~Reader(){                      //析构函数
};

/////////////////////////////////////////////////////////////////////////////////////
//定义读者库类RDatabase成员
RDatabase::RDatabase(){                 //构造函数,将reader.txt读到read[]中
    Reader r;
    top = -1;
    fstream file("reader.txt",ios::in);
    while(1){
        file.read((char *)&r,sizeof(r));
        if(!file){ break; }
        top++;
        read[top] = r;
    }
    file.close();
};
void RDatabase::clear(){                //删除所有读者信息
    top = -1;
};
int RDatabase::addreader(int n,char *na){ //添加读者时先查找是否存在
    Reader *p = query(n);
    if(p==NULL){
        top++;
        read[top].addreader(n,na);
        return 1;
    }
    return 0;
};
Reader *RDatabase::query(int readerid){ //按编号查找
    for(int i=0;i<=top;i++){
        if(read[i].getno()==readerid && read[i].gettag()==0){
            return &read[i];
        }
    }
    return NULL;
};
void RDatabase::display(){              //输出所有读者信息
    if(top==-1){
        cout << "Reader database is empty!" << endl << endl;
        return;
    }
    for(int i=0;i<=top;i++){
        if(read[i].gettag()==0){
            read[i].display();
        }
    }
};
void RDatabase::readerdata(){           //读者库维护
    int    choice = 1;
    char   rname[20];
    int    readerid;
    Reader *r;

    while(choice!=0){
        cout << "读者维护:" << "  "
             << "1:新增"    << "  "
             << "2:更改"    << "  "
             << "3:删除"    << "  "
             << "4:查找"    << "  "
             << "5:显示"    << "  "
             << "6:全删"    << "  "
             << "0:退出"    << "  请选择:";
        cin >> choice;
        switch(choice){
        case 0:
            {
                break;
            }
        case 1:
            {
                cout << "输入读者编号:";   cin >> readerid;
                cout << "输入读者姓名:";   cin >> rname;
                addreader(readerid,rname);
                break;
            }
        case 2:
            {
                cout << "输入读者编号:";   cin >> readerid;
                r = query(readerid);
                if(r==NULL){
                    cout << "该读者不存在" << endl;
                    break;
                }
                cout << "输入新的姓名:";   cin >> rname;
                r->setname(rname);
                break;
            }
        case 3:
            {
                cout << "输入读者编号:";   cin >> readerid;
                r = query(readerid);
                if(r==NULL){
                    cout << "该读者不存在" << endl;
                    break;
                }
                r->delbook();
                break;
            }
        case 4:
            {
                cout << "输入读者编号:";   cin >> readerid;
                r = query(readerid);
                if(r==NULL){
                    cout << "该读者不存在" << endl;
                    break;
                }
                r->display();
                break;
            }
        case 5:
            {
                display();
                break;
            }
        case 6:
            {
                clear();
                break;
            }
        default:
            {
                cout << "输入错误,请输入正确的选项" << endl;
                break;
            }
        }
    }
};
RDatabase::~RDatabase(){                //析构函数,将read[]写入reader.txt文件中
    fstream file("reader.txt",ios::out);
    for(int i=0;i<=top;i++){
        if(read[i].gettag()==0){
            file.write((char *)&read[i],sizeof(read[i]));
        }
    }
    file.close();
};

/////////////////////////////////////////////////////////////////////////////////////
//定义图书类Book成员
Book::Book(){                           //构造函数
};
char *Book::getname(){                  //获取书名
    return name;
};
int Book::getno(){                      //获取图书编号
    return no;
};
int Book::gettag(){                     //获取删除标记
    return tag;
};
void Book::setname(char na[]){          //设置书名
    strcpy(name,na);
};
void Book::delbook(){                   //删除图书
    tag = 1;
};
void Book::addbook(int n,char *na){     //增加图书
    tag = 0;
    no = n;
    strcpy(name,na);
    onshelf = 1;
};
int Book::borrowbook(){                 //借书操作
    if(onshelf==1){
        onshelf = 0;
        return 1;
    }
    return 0;
};
void Book::retbook(){                   //还书操作
    onshelf = 1;
};
void Book::display(){                   //输出图书信息
    cout << setw(6) << no << setw(18) << name << setw(10) << ((onshelf==1)?"在架":"已借") << endl;
};
Book::~Book(){                          //析构函数
};

/////////////////////////////////////////////////////////////////////////////////////
//定义图书库类BDatabase成员
BDatabase::BDatabase(){                 //构造函数,将book.txt读到book[]中
    Book b;
    top = -1;
    fstream file("book.txt",ios::in);
    while(1){
        file.read((char *)&b,sizeof(b));
        if(!file){ break; }
        top++;
        book[top] = b;
    }
    file.close();
};
void BDatabase::clear(){                //删除所有图书信息
    top = -1;
};
int BDatabase::addbook(int n,char *na){ //添加图书时先查找是否存在
    Book *p = query(n);
    if(p==NULL){
        top++;
        book[top].addbook(n,na);
        return 1;
    }
    return 0;
};
Book *BDatabase::query(int bookid){     //按编号查找
    for(int i=0;i<=top;i++){
        if(book[i].getno()==bookid && book[i].gettag()==0){
            return &book[i];
        }
    }
    return NULL;
};
void BDatabase::display(){              //输出所有可借的图书信息
    if(top==-1){
        cout << "Book database is empty!" << endl << endl;
        return;
    }
    for(int i=0;i<=top;i++){
        if(book[i].gettag()==0){
            book[i].display();
        }
    }
};
void BDatabase::bookdata(){             //图书库维护
    int  choice = 1;
    char bname[40];
    int  bookid;
    Book *b;
    while(choice!=0){
        cout << "图书维护:" << "  "
             << "1:新增"    << "  "
             << "2:更改"    << "  "
             << "3:删除"    << "  "
             << "4:查找"    << "  "
             << "5:显示"    << "  "
             << "6:全删"    << "  "
             << "0:退出"    << "  请选择:";
        cin >> choice;
        switch(choice){
        case 0:
            {
                break;
            }
        case 1:
            {
                cout << "输入图书编号:";   cin >> bookid;
                cout << "输入图书书名:";   cin >> bname;
                addbook(bookid,bname);
                break;
            }
        case 2:
            {
                cout << "输入图书编号:";   cin >> bookid;
                b = query(bookid);
                if(b==NULL){
                    cout << "该图书不存在" << endl;
                    break;
                }
                cout << "输入新的书名:";   cin >> bname;
                b->setname(bname);
                break;
            }
        case 3:
            {
                cout << "输入图书编号:";   cin >> bookid;
                b = query(bookid);
                if(b==NULL){
                    cout << "该图书不存在" << endl;
                    break;
                }
                b->delbook();
                break;
            }
        case 4:
            {
                cout << "输入图书编号:";   cin >> bookid;
                b = query(bookid);
                if(b==NULL){
                    cout << "该图书不存在" << endl;
                    break;
                }
                b->display();
                break;
            }
        case 5:
            {
                display();
                break;
            }
        case 6:
            {
                clear();
                break;
            }
        default:
            {
                cout << "输入错误,请输入正确的选项" << endl;
                break;
            }
        }
    }
};
BDatabase::~BDatabase(){                //析构函数,将book[]写入book.txt文件中
    fstream file("book.txt",ios::out);
    for(int i=0;i<=top;i++){
        if(book[i].gettag()==0){
            file.write((char *)&book[i],sizeof(book[i]));
        }
    }
    file.close();
};

void main(){
    int choice=1;

    RDatabase ReaderDB;   int readerid;   Reader *r;
    BDatabase BookDB;     int bookid;     Book   *b;

    while(choice!=0){
        cout << "操作:"      << "  "
             << "1:借书"     << "  "
             << "2:还书"     << "  "
             << "3:图书维护" << "  "
             << "4:读者维护" << "  "
             << "0:离开"     << "  请选择:";
        cin >> choice;
        switch(choice){
        case 0:
            {
                break;
            }
        case 1:
            {
                cout << "借书" << endl;
                cout << "读者编号:";   cin >> readerid;
                cout << "图书编号:";   cin >> bookid;
                r = ReaderDB.query(readerid);
                if(r==NULL){
                    cout << "不存在该读者,不能借书" << endl;
                    break;
                }
                b = BookDB.query(bookid);
                if(b==NULL){
                    cout << "不存在该图书,不能借书" << endl;
                    break;
                }
                if(b->borrowbook()==0){
                    cout << "该图书已借出,不能借书" << endl;
                    break;
                }
                r->borrowbook(b->getno());
                break;
            }
        case 2:
            {
                cout << "还书" << endl;
                cout << "读者编号:";   cin >> readerid;
                cout << "图书编号:";   cin >> bookid;
                r = ReaderDB.query(readerid);
                if(r==NULL){
                    cout << "不存在该读者,不能还书" << endl;
                    break;
                }
                b = BookDB.query(bookid);
                if(b==NULL){
                    cout << "不存在该图书,不能还书" << endl;
                    break;
                }
                b->retbook();
                r->retbook(b->getno());
                break;
            }
        case 3:
            {
                BookDB.bookdata();
                break;
            }
        case 4:
            {
                ReaderDB.readerdata();
                break;
            }
        default:
            {
                cout << "输入错误,请输入正确的选项" << endl;
                break;
            }
        }
        cout << endl;
    }
}
搜索更多相关主题的帖子: 系统 
2010-12-21 16:12
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:2 
链表类.rar (1.9 KB)
   看看和你胃口吧  最好再改改

                                         
===========深入<----------------->浅出============
2010-12-21 17:00
快速回复:图书馆管理系统
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061032 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved