图书管理系统 青春版

图书管理系统 青春版

实现了添加修改查找删除 用的vs2019
程序分为四个部分
1.main.cpp
2.book.h
3.library.h
4.library.cpp
[main.cpp]

#include <iostream>
#include "Library.h"

using namespace std;

int main()
{
	Library L;
	int choice;

	while (true) {

		//for (map<string, book>::iterator i = L.BookMAP.begin(); i != L.BookMAP.end(); i++) {
		//	cout << (*i).second.Isbn << ' ' << (*i).second.Name << ' ' << (*i).second.Writer << ' ' << (*i).second.PublishingHouse
		//		<< ' ' << (*i).second.Price << ' ' << (*i).second.Stock << endl;
  //   	}
        
		L.ShowMenu();
		
		cout << "请输入您的选择:";
		cin >> choice;

		switch(choice) {
		//整理图书
		case 1:
			system("cls");  //清屏
			L.SortBook();
			break;

		//显示书籍信息
		case 2:;
			system("cls");  //清屏
			cout << "图书的信息是(书名 Isbn 作者 出版社 价格 库存):" << endl;
			L.ShowBookInformation();
			system("pause");
			system("cls");  //清屏
			break;

		//保存图书信息
		case 3:
			L.SaveBook();
			cout << "保存成功" << endl;
			system("pause");
			system("cls");  //清屏
			break;

		//退出图书系统
		case 0:
			cout << "是否保存此次修改信息(1 是 0 否):";
			cin >> choice;
			if (choice) L.SaveBook();
			else;
			L.exitSystem();
			break;

		default:
			system("cls");  //清屏
			break;
		}
	}
	
	L.SaveBook();
	return 0;
}

[book.h]

#pragma once
#include <string>
using namespace std;

class book
{
public:
	book() {};
	//book(string N, string I, string W, string P, float Pr, int S) :Name(N),Isbn(I),Writer(W),PublishingHouse(P),Price(Pr),Stock(S){}
	string Name;
	string Isbn;
	string Writer;
	string PublishingHouse; //出版社
	float Price;
	int Stock; //储存
};

[library.h]

#pragma once

#include "Book.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>



class Library
{
public:
	Library();

	//生成界面
	void ShowMenu();

	//读取数据库信息
	void ReadInformation();

	//整理图书
	void SortBook();

	//整理图书选择界面
	void showMenuS();

	//查找选择界面
	void showMenuSearch();

	//修改书籍信息  不修改 Isbn
	void changeBookinfor(string Isbn);

	//判断图书是否存在
	bool IsExist(string Isbn);

	//通过Isbn查找图书信息
	void SearchByIsbn(string Isbn);

    //通过书名查找图书信息
	void SearchByName(string Name);

	//通过作者查找图书信息
	void SearchByWriter(string Writer);

	//通过出版社查找图书信息
	void SearchByPublish(string Publish);

	//加入图书
	void AddBook();

	//删除图书
	void Delete(string Name);

	//显示图书信息 按ISBN号排序
	void ShowBookInformation();

	//保存图书信息
	void SaveBook();

	//退出系统
	void exitSystem();

	//判断文件是否为空
	bool fileIsEmpty;
	
	//保存读入图书信息
	map<string, book> BookMAP;
};

[library.cpp]

include "Library.h"


//把 string 转 int 的函数
int int1str(string num) {
    int res;
    stringstream stream(num);
    stream >> res;
    return res;
}

//把 string 转 float 的函数
float int2str(string num) {
    float res;
    stringstream stream(num);
    stream >> res;
    return res;
}

bool cmp(book b1, book b2)
{
    return b1.Isbn < b2.Isbn;
}

Library::Library()
{
    //读取数据库记录
    this->ReadInformation();

   // cout << "Library" << endl;
}

void Library::ShowMenu()
{
        cout << "*************************************" << endl;
        cout << "********  书是人类进步的阶梯  *********" << endl;
        cout << "********    整理书籍请按1    *********" << endl;
        cout << "********  显示书籍信息请按2  *********" << endl;
        cout << "********  保存图书信息请按3  *********" << endl;
        cout << "********  退出图书系统请按0  *********" << endl;
        cout << "*************************************" << endl;
}

void Library::showMenuS()
{
    cout << "*************************************" << endl;
    cout << "********    书籍整理系统    *********" << endl;
    cout << "********    添加书籍请按1    *********" << endl;
    cout << "********  修改书籍信息请按2  *********" << endl;
    cout << "********  查找图书信息请按3  *********" << endl;
    cout << "********  删除图书信息请按4  *********" << endl;
    cout << "********  退出整理系统请按0  *********" << endl;
    cout << "*************************************" << endl;
}

void Library::showMenuSearch()
{
    cout << "***************************************" << endl;
    cout << "********     书籍查找系统     *********" << endl;
    cout << "********   按ISBN查找请按1    *********" << endl;
    cout << "********   按作者查找请按2    *********" << endl;
    cout << "********   按出版社查找请按3  *********" << endl;
    cout << "********   按书名查找请按4    *********" << endl;
    cout << "********   退出查找系统请按0  *********" << endl;
    cout << "***************************************" << endl;
}

void Library::SortBook() 
{
    int choice,ChoiceSearch;
    string SS;
    string Isbn;
    while(true) {
        showMenuS();
        cout << "请输入您的选择:";
        cin >> choice;

        switch (choice) {
        //添加图书
        case 1:
            AddBook();
            system("cls");
            break;
        
        //修改图书信息
        case 2:
            cout << "请输入想修改的书的Isbn:";
            cin >> Isbn;
            changeBookinfor(Isbn);
            system("cls");
            break;

        //查找图书信息
        case 3:
            system("cls");
            while (true)
            {
                showMenuSearch();
                cout << "请输入您的选择:";
                cin >> ChoiceSearch;
                switch (ChoiceSearch) {
                    //按ISBN查找请按1
                case 1:
                    system("cls");
                    cout << "请输入想要查询的ISBN:";
                    cin >> SS;
                    SearchByIsbn(SS);
                    system("pause");
                    system("cls");
                    break;

                    //按作者查找请按2
                case 2:
                    system("cls");
                    cout << "请输入想要查询的作者:";
                    cin >> SS;
                    SearchByWriter(SS);
                    system("pause");
                    system("cls");
                    break;
                    //按出版社查找请按3
                case 3:
                    system("cls");
                    cout << "请输入想要查询的ISBN:";
                    cin >> SS;
                    SearchByPublish(SS);
                    system("pause");
                    system("cls");
                    break;

                    //按书名查找请按4
                case 4:
                    system("cls");
                    cout << "请输入想要查询的书名:";
                    cin >> SS;
                    SearchByName(SS);
                    system("pause");
                    system("cls");
                    break;

                    //退出查找系统请按0
                case 0:
                    system("cls");
                    break;
                default:
                    system("cls");  //清屏
                    break;
                }
                if (ChoiceSearch == 0) break;
            }
            break;
            
        //删除图书信息
        case 4:
            cout << "请输入想删除的书的Isbn:";
            cin >> Isbn;
            Delete(Isbn);
            system("cls");
            break;

        //退出整理系统
        case 0:
            system("cls");
            break;
        default:
            system("cls");  //清屏
            break;
        }
        if (choice == 0) break;
    }
};

//添加图书
void Library::AddBook()
{
    book tmp;
    cout << "请输入要添加的图书的信息 Name Isbn Writer PublishingHouse Price Stock:" << endl;
    cin >> tmp.Isbn >> tmp.Name >> tmp.Writer >> tmp.PublishingHouse >> tmp.Price >> tmp.Stock;

    map<string, book>::iterator it = BookMAP.find(tmp.Isbn);
    
    //这本书 本来就存在  
    if ( it != BookMAP.end() ) { 
        if (tmp.Name != (*it).second.Name || tmp.Writer != (*it).second.Writer || tmp.PublishingHouse != (*it).second.PublishingHouse || tmp.Price != (*it).second.Price) {
            cout << "输入信息有误,添加失败" << endl;
        }
        else {
            (*it).second.Stock += tmp.Stock;
        }
    } else { 
        //添加新书
        BookMAP.insert({ tmp.Isbn, tmp });
        ofstream ofs;
        ofs.open("Library.csv", ios::out | ios::app); // 用输出的方式打开文件  -- 写文件

        ofs  << tmp.Isbn << ',' << tmp.Name << ','<< tmp.Writer << ',' << tmp.PublishingHouse << ',' << tmp.Price << ',' << tmp.Stock << ',';

        ofs << endl;
        //关闭文件
        ofs.close();

        cout << "已经添加该图书" << endl;
    
    //有记录了,文件不为空
    this->fileIsEmpty = false;
    }
    system("pause");
}

//修改书籍信息  不修改 Isbn
void Library::changeBookinfor(string Isbn)
{
    book tmp;
    map<string, book>::iterator it = BookMAP.find(Isbn);
    if (it == BookMAP.end()) {
        cout << "输入Isbn不存在" << endl;
        system("pause");
    }
    else {
        cout << "原书籍的信息是(Isbn 书名 作者 出版社 价格 库存):" << endl;
        cout << (*it).second.Isbn << ' ' << (*it).second.Name << ' ' << (*it).second.Writer << ' ' << (*it).second.PublishingHouse
            << ' ' << (*it).second.Price << ' ' << (*it).second.Stock << endl;
        cout << "请输入新的信息(书名 作者 出版社 价格 库存):" << endl;
        cin >> tmp.Name >>  tmp.Writer >> tmp.PublishingHouse >> tmp.Price >> tmp.Stock;
        (*it).second.Name = tmp.Name;
        (*it).second.Writer = tmp.Writer;
        (*it).second.PublishingHouse = tmp.PublishingHouse;
        (*it).second.Price = tmp.Price;
        (*it).second.Stock = tmp.Stock;
        cout << "修改成功!" << endl;
        system("pause");
    }

}

//判断书是否存在
bool Library::IsExist(string Isbn)
{
    map<string, book>::iterator it = BookMAP.find(Isbn);
    if (it != BookMAP.end())
        return true;
    else
        return false;
}

//通过 ISBN 查找图书信息 并显示
void Library::SearchByIsbn(string Isbn)
{
    bool exist = false;
    for (map<string, book>::iterator it = BookMAP.begin(); it != BookMAP.end(); it++)
    {
        if (exist == false && (*it).second.Isbn == Isbn) {
            cout << "图书的信息是(Isbn 书名 作者 出版社 价格 库存):" << endl;
            exist = true;
        }
        if ((*it).second.Isbn == Isbn) {
            cout << (*it).second.Isbn << ' ' << (*it).second.Name << ' ' << (*it).second.Writer << ' ' << (*it).second.PublishingHouse
                		<< ' ' << (*it).second.Price << ' ' << (*it).second.Stock << endl;
        }
    }
    if (exist == false) cout << "没有这本书,sry." << endl;
}

//通过 书名 查找图书信息 并显示
void Library::SearchByName(string Name)
{
    bool exist = false;
    for (map<string, book>::iterator it = BookMAP.begin(); it != BookMAP.end(); it++)
    {
        if (exist == false && (*it).second.Name == Name) {
            cout << "图书的信息是(Isbn 书名 作者 出版社 价格 库存):" << endl;
            exist = true;
        }
        if ((*it).second.Name == Name) {
            cout << (*it).second.Isbn << ' ' << (*it).second.Name << ' ' << (*it).second.Writer << ' ' << (*it).second.PublishingHouse
                << ' ' << (*it).second.Price << ' ' << (*it).second.Stock << endl;
        }
    }
    if (exist == false) cout << "没有这本书,sry." << endl;
}

//通过 作者 查找图书信息 并显示
void Library::SearchByWriter(string Writer)
{
    bool exist = false;
    for (map<string, book>::iterator it = BookMAP.begin(); it != BookMAP.end(); it++)
    {
        if (exist == false && (*it).second.Writer == Writer) {
            cout << "图书的信息是(Isbn 书名 作者 出版社 价格 库存):" << endl;
            exist = true;
        }
        if ((*it).second.Writer == Writer) {
            cout << (*it).second.Isbn << ' ' << (*it).second.Name << ' ' << (*it).second.Writer << ' ' << (*it).second.PublishingHouse
                << ' ' << (*it).second.Price << ' ' << (*it).second.Stock << endl;
        }
    }
    if (exist == false) cout << "没有这本书,sry." << endl;
}

//通过 出版社 查找图书信息 并显示
void Library::SearchByPublish(string Publish)
{
    bool exist = false;
    for (map<string, book>::iterator it = BookMAP.begin(); it != BookMAP.end(); it++)
    {
        if (exist == false && (*it).second.PublishingHouse == Publish) {
            cout << "图书的信息是(Isbn 书名 作者 出版社 价格 库存):" << endl;
            exist = true;
        }
        if ((*it).second.PublishingHouse == Publish) {
            cout << (*it).second.Isbn << ' ' << (*it).second.Name << ' ' << (*it).second.Writer << ' ' << (*it).second.PublishingHouse
                << ' ' << (*it).second.Price << ' ' << (*it).second.Stock << endl;
        }
    }
    if (exist == false) cout << "没有这本书,sry." << endl;
}

//显示图书信息 按ISBN号排序
void Library::ShowBookInformation()
{
    //map  不能 按照 value 排序 我真的服了
    //sort(BookMAP.begin(), BookMAP.end(),[](map<string, book>::value_type b1, map<string, book>::value_type b2) { return b1.second.Isbn < b2.second.Isbn; });// cmp 
    for (map<string, book>::iterator i = BookMAP.begin(); i != BookMAP.end(); i++) {
        	cout << (*i).second.Isbn << ' ' << (*i).second.Name << ' ' << (*i).second.Writer << ' ' << (*i).second.PublishingHouse
        		<< ' ' << (*i).second.Price << ' ' << (*i).second.Stock << endl;
     	}
}

//删除图书
void Library::Delete(string Isbn)
{
    if (IsExist(Isbn)) {
        BookMAP.erase(Isbn);
        cout << "删除成功!" << endl;
        system("pause");
    }
    else {
        cout << "这本书本来就没有,不用删除。" << endl;
        system("pause");
    }
}

//保存图书信息
void Library::SaveBook()
{
    ofstream ofs;
    ofs.open("Library.csv", ios::out);  //输出流对象  输出文件

    string data = "ISBN,书名,作者,出版社,价格,库存,";
    ofs << data << endl;

    for (map<string, book>::iterator i = BookMAP.begin(); i != BookMAP.end(); i++) {
        	ofs << (*i).second.Isbn << ',' << (*i).second.Name << ',' << (*i).second.Writer << ',' << (*i).second.PublishingHouse
        		<< ',' << (*i).second.Price << ',' << (*i).second.Stock << endl;
     }

    //关闭文件
    ofs.close();
}

//读入数据库信息
void Library::ReadInformation()
{
    ifstream ifs("Library.csv", ios::in); //输入流对象 读取文件

    if (!ifs.is_open())
    {
        this->fileIsEmpty = true;
        cout << "文件不存在!" << endl;
        ifs.close();
        return;
    }

    //
    //char ch;
    //ifs >> ch;
    //if (ifs.eof())
    //{
    //    cout << "文件为空!" << endl;
    //    this->fileIsEmpty = true;
    //    ifs.close();
    //    return;
    //}

    文件不为空
    //this->fileIsEmpty = false;

    //ifs.putback(ch); //读取的单个字符放回去

    string data;

    while (ifs >> data)
    {
        int pos = -1;
        int start = 0;
        int i = 1;
        string tmp;
        book TMP;
        while (true)
        {
            pos = data.find(',', start); //从0开始查找 ','
            if (pos == -1)
            {
                break; //找不到break返回
            }
            tmp = data.substr(start, pos - start); //找到了,进行分割 参数1 起始位置,参数2 截取长度
            
            if (i == 1) { TMP.Isbn = tmp; }
            else if (i == 2) { if (tmp == "书名") break; TMP.Name = tmp; this->fileIsEmpty = false; }
            else if (i == 3) { TMP.Writer = tmp; }
            else if (i == 4) { TMP.PublishingHouse = tmp; }
            else if (i == 5) { TMP.Price = int2str(tmp); }
            start = pos + 1;
            i++;
        }

       //cout << data << endl;
        if (tmp == "书名") continue;
       TMP.Stock = int1str(data.substr(start,data.length()-start+1));
       BookMAP[TMP.Isbn] = TMP;

    }
   
    ifs.close();
}

void Library::exitSystem()
{
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
}

版权声明:本文为m0_52106197原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。