用C++编写图书管理系统?要求头文件和.cpp文件分开

  • 1. * 1、问题描述 定义图书类,属性有:书名、出版社、ISBN号、作者、库存量、价格等信息和相关的对属性做操作的行为。 主要完成对图书的销售、统计和图书的简单管理。 2、功能要求 (1)销售功能。购买书籍时,输入相应的ISBN号,并在书库中查找该书的相关信息。如果有库存量,输入购买的册数,进行相应计算。如果库存量不够,给出提示信息,结束购买。 (2)图书简单管理功能。 添加功能:主要完成图书信息的添加,要求ISBN号唯一。当添加了重复的编号时,则提示数据添加重复并取消添加。 查询功能:可按书名、ISBN号、作者、出版社进行查询。若存在相应信息,输出所查询的信息,若不存在该记录,则提示“该标题不存在!”。 修改功能:可根据查询结果对相应的记录进行修改,修改时注意ISBN号的唯一性。 删除功能:主要完成图书信息的删除。输入要删除的ISBN号,根据编号删除该物品的记录,如果该编号不在物品库中,则提示“该编号不存在”。 (3)统计功能。 输出当前书库中所有图书的总数及详细信息;可按书的价格、库存量、作者、出版社进行统计,输出统计信息时,要按从大到小进行排序。 (7)图书存盘:将当前程序中的图书信息存入文件中。 (8)读出信息:从文件中将图书信息读入程序。

代码先贴这,你测试一下:

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

#define N 300//最多保存数

class book//图书类
{
public:
    string name;//书名
    string pub;//出版社
    string ISBN;
    string author;//作者
    int count;
    float price;


    void input(string n, string p, string i, string a, int c, float pr )//输入信息到图书类
    {
        name = n;
        pub = p;
        ISBN = i;
        author = a;
        count = c;
        price = pr;
    }

    void show()//显示信息
    {
        cout<< "书名:" << name 
            << " 出版社:" << pub 
            << " ISBN号:" << ISBN 
            << " 作者:" << author 
            << " 库存:" << count 
            << " 价格:" << price << endl;
    }

    void input(ifstream & is)//从文件读取类成员变量
    {
        is >> name >> pub >> ISBN >> author >> count >> price;
    }

    void output(ofstream & os)//存书籍类成员变量到文件
    {
        os << name
            << " " << pub 
            << " " << ISBN 
            << " " << author 
            << " " << count 
            << " " << price << endl;
    }

};

class bookmanage//图书管理类
{
    book m_book[N];
    int m_count;
public:
    bookmanage(){ m_count = 0; }//构造函数
    int bookexist(string ISBN)
    {
        int ret = -1;
        for (int i = 0; i < m_count; i++)
        {
            if (ISBN == m_book[i].ISBN)//找到相同号
            {
                ret = i;
                break;
            }
        }

        return ret;
    }

    void insert(string n, string p, string i, string a, int c, float pr)//添加
    {
        m_book[m_count].input(n, p,  i, a, c, pr);
        m_count++;
    }

    void del(int index)
    {
        for (int i = index; i < m_count; i++)
        {
            m_book[i] = m_book[i+1];
        }

        m_count--;
    }

    void modify(int index, string n, string p, string i, string a, int c, float pr)//修改
    {
        m_book[index].input(n, p,  i, a, c, pr);
    }


    bool searchname(string name)//根据书名查找
    {
        bool ret = false;
        for (int i = 0; i < m_count; i++)
        {
            if (name == m_book[i].name)
            {
                m_book[i].show();
                ret = true;
            }
        }

        return ret;
    }

    bool searchpub(string pub)//根据作==出版社查找
    {
        bool ret = false;
        for (int i = 0; i < m_count; i++)
        {
            if (pub == m_book[i].pub)
            {
                m_book[i].show();
                ret = true;
            }
        }

        return ret;
    }

    bool searchISBN(string ISBN)//根据ISBN查找
    {
        bool ret = false;
        for (int i = 0; i < m_count; i++)
        {
            if (ISBN == m_book[i].ISBN)
            {
                m_book[i].show();
                ret = true;
            }
        }

        return ret;
    }

    bool searchauthor(string author)//根据作者查找
    {
        bool ret = false;
        for (int i = 0; i < m_count; i++)
        {
            if (author == m_book[i].author)
            {
                m_book[i].show();
                ret = true;
            }
        }

        return ret;
    }

    bool sortprice()
    {
        if(m_count==0) return false;
        int i,j;
        book t;
        for (i=0;i<m_count-1;i++)
        {
            for (j=0;j<m_count-i-1;j++)
            {
                if (m_book[j].price<m_book[j+1].price)
                {
                    t=m_book[j];
                    m_book[j] = m_book[j+1];
                    m_book[j+1] = t;
                }
            }
        }

        for (int i = 0; i < m_count; i++)
        {
            m_book[i].show();
        }

        return true;
    }

    bool sortcount()
    {
        if(m_count==0) return false;
        int i,j;
        book t;
        for (i=0;i<m_count-1;i++)
        {
            for (j=0;j<m_count-i-1;j++)
            {
                if (m_book[j].count<m_book[j+1].count)
                {
                    t=m_book[j];
                    m_book[j] = m_book[j+1];
                    m_book[j+1] = t;
                }
            }
        }

        for (int i = 0; i < m_count; i++)
        {
            m_book[i].show();
        }

        return true;
    }

    bool sortauthor()
    {
        if(m_count==0) return false;
        int i,j;
        book t;
        for (i=0;i<m_count-1;i++)
        {
            for (j=0;j<m_count-i-1;j++)
            {
                if (m_book[j].author<m_book[j+1].author)
                {
                    t=m_book[j];
                    m_book[j] = m_book[j+1];
                    m_book[j+1] = t;
                }
            }
        }

        for (int i = 0; i < m_count; i++)
        {
            m_book[i].show();
        }

        return true;
    }

    bool sortpub()
    {
        if(m_count==0) return false;
        int i,j;
        book t;
        for (i=0;i<m_count-1;i++)
        {
            for (j=0;j<m_count-i-1;j++)
            {
                if (m_book[j].pub<m_book[j+1].pub)
                {
                    t=m_book[j];
                    m_book[j] = m_book[j+1];
                    m_book[j+1] = t;
                }
            }
        }

        for (int i = 0; i < m_count; i++)
        {
            m_book[i].show();
        }

        return true;
    }

    bool show()//显示全部图书
    {
        if(m_count==0) return false;
        for (int i = 0; i < m_count; i++)
        {
            m_book[i].show();
        }

        return true;
    }

    bool buybook(int index, int c)//购书
    {
        if(m_book[index].count<c) return false;
        m_book[index].count -= c;
        return true;
    }

    void save(string filename)//存全部图书到文件
    {
        ofstream write(filename.c_str(), ios::out);
        write << m_count << endl;
        for (int i = 0; i < m_count; i++)
        {
            m_book[i].output(write);
        }

        write.close();
    }

    void load(string filename)//从文件读取全部图书信息
    {
        ifstream read(filename.c_str(), ios::in);
        if (read.peek() == EOF)
        {
            read.close();
            return;
        }

        read >> m_count;
        for (int i = 0; i < m_count; i++)
        {
            m_book[i].input(read);
        }

        read.close();
    }
};

void searcbook(bookmanage manage)
{
    string name;//书名
    string pub;//出版社
    string ISBN;
    string author;//作者
    char n;
    int k = 1;

    while (k == 1)
    {
        system("cls");
        cout << "图书管理" << endl;
        cout<< "1   根据书名查找" << endl
            << "2   根据ISBN查找" << endl
            << "3   根据作者查找" << endl
            << "4   根据出版社查找" << endl
            << "0   返回" << endl
            << "选择:";
        cin >> n;
        switch (n)
        {
        case 1:
            cout << "输入书名:";
            cin >> name;
            if(!manage.searchname(name)) cout << "该标题不存在!"<<endl;
            system("pause");
            break;
        case 2:
            cout << "输入出版社:";
            cin >> pub;
            if(!manage.searchname(pub)) cout << "该标题不存在!"<<endl;
            system("pause");    
            break;;
        case 3:
            cout << "输入ISBN:";
            cin >> ISBN;
            if(!manage.searchname(ISBN)) cout << "该标题不存在!"<<endl;
            system("pause");
            break;
        case 4:
            cout << "输入作者:";
            cin >> author;
            if(!manage.searchname(author)) cout << "该标题不存在!"<<endl;
            system("pause");
            break;
        case 0:
            k = 0;
            break;
        default:
            cout<<"选择错误,请重新输入"<<endl;
            break;
        }
    }
}

void bookmemu(bookmanage &manage)
{
    string name;//书名
    string pub;//出版社
    string ISBN;
    string author;//作者
    int count;
    float price;
    char n;
    int index;
    int k = 1;

    while (k == 1)
    {
        system("cls");
        cout << "图书管理" << endl;
        cout<< "1   图书基本信息录入" << endl
            << "2   图书基本信息显示" << endl
            << "3   图书基本信息删除" << endl
            << "4   图书基本信息修改" << endl
            << "5   图书基本信息查询" << endl
            << "0   返回" << endl
            << "选择:";
        cin >> n;
        switch (n)
        {
        case 1:
            cout << "请输入ISBN:";
            cin >> ISBN;
            index = manage.bookexist(ISBN);
            if (index == -1)
            {
                cout << "请输入出版社:";
                cin >> pub;
                cout << "请输入ISBN:";
                cin >> ISBN;
                cout << "请输入作者:";
                cin >> author;
                cout << "请输入库存量:";
                cin >> count;
                cout << "请输入买入价格:";
                cin >> price;
                manage.insert(name, pub,  ISBN, author,  count, price);
            }
            else cout << "该编号已存在" << endl;

            system("pause");
            break;
        case 2:
            manage.show();
            system("pause");    
            break;;
        case 3:
            cout << "请输入ISBN:";
            cin >> ISBN;
            index = manage.bookexist(ISBN);
            if (index != -1) manage.del(index);
            else cout << "该编号不存在" << endl;
            system("pause");
            break;
        case 4:
            cout << "请输入ISBN:";
            cin >> ISBN;
            index = manage.bookexist(ISBN);
            if (index != -1)
            {
                cout << "请输入出版社:";
                cin >> pub;
                cout << "请输入ISBN:";
                cin >> ISBN;
                cout << "请输入作者:";
                cin >> author;
                cout << "请输入库存量:";
                cin >> count;
                cout << "请输入买入价格:";
                cin >> price;
                manage.modify(index, name, pub,  ISBN, author,  count, price);
            }
            else cout << "该编号不存在" << endl;
            system("pause");
            break;
        case 5:
            searcbook(manage);
            system("pause");
            break;
        case 0:
            k = 0;
            break;
        default:
            cout<<"选择错误,请重新输入"<<endl;
            break;
        }
    }
}

void statistics(bookmanage manage)
{
    char n;
    int k = 1;

    while (k == 1)
    {
        system("cls");
        cout << "图书统计" << endl;
        cout<< "1   根据价格统计" << endl
            << "2   根据库存统计" << endl
            << "3   根据作者统计" << endl
            << "4   根据出版社统计" << endl
            << "0   返回" << endl
            << "选择:";
        cin >> n;
        switch (n)
        {
        case 1:
            if(!manage.sortprice()) cout << "未录入图书!"<<endl;
            system("pause");
            break;
        case 2:
            if(!manage.sortcount()) cout << "未录入图书!"<<endl;
            system("pause");    
            break;;
        case 3:
            if(!manage.sortauthor()) cout << "未录入图书!"<<endl;
            system("pause");
            break;
        case 4:
            if(!manage.sortpub()) cout << "未录入图书!"<<endl;
            system("pause");
            break;
        case 0:
            k = 0;
            break;
        default:
            cout<<"选择错误,请重新输入"<<endl;
            break;
        }
    }
}

int main()
{
    bookmanage manage;
    manage.load("book.txt");
    string name;//书名
    string pub;//出版社
    string ISBN;
    string author;//作者
    int count;
    int n;
    int index;
    int k = 1;

    while (k == 1)
    {
        system("cls");
        cout << "图书管理系统" << endl;
        cout<< "1   管理图书" << endl
            << "2   销售图书" << endl
            << "3   统计图书" << endl
            << "0   退出系统" << endl
            << "选择:";
        cin >> n;
        switch (n)
        {
        case 1:
            bookmemu(manage);
            system("pause");
            break;
        case 2:
            cout << "请输入ISBN:";
            cin >> ISBN;
            index = manage.bookexist(ISBN);
            if (index != -1)
            {
                cout << "请输入购买数量:";
                cin >> count;
                if(manage.buybook(index, count)) cout << "购买成功" << endl;
                else cout << "库存不足" << endl;
            }
            else cout << "该编号不存在" << endl;
            system("pause");
            break;
        case 3:
            statistics(manage);
            break;
        case 0:
            k = 0;
            break;
        default:
            cout<<"选择错误,请重新输入"<<endl;
            break;
        }
    }

    manage.save("book.txt");
    return 0;
}

https://download.csdn.net/download/qq_33017925/9544966