欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++实现图书管理系统最新版

程序员文章站 2022-06-25 23:13:46
图书管理系统设计,供大家参考,具体内容如下一、问题描述及功能要求(1)图书信息录入功能(图书信息用文件保存)(2)图书信息浏览功能(3)查询和排序功能:(至少一种查询方式).按书名查询.按作者名查询(...

图书管理系统设计,供大家参考,具体内容如下

一、问题描述及功能要求

(1)图书信息录入功能(图书信息用文件保存)

(2)图书信息浏览功能

(3)查询和排序功能:(至少一种查询方式)

.按书名查询
.按作者名查询

(4)图书信息的删除与修改

二、代码实现 带有注释

废话不说,直接代码,欢迎指正。
大家cv可能有不兼容的情况,可以滴滴,尽可能解决问题地回复。

#include<iostream>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include <cstring>
#include<windows.h>
#include<vector>
#define bookmaxrepertory 99
using namespace std;
class common{
public:
    int id;
    char title[66];
    char author[66];
    common(){
    }
    common(int _id,char _title[],char _author[]){
        id = _id;
        strcpy(title , _title);
        strcpy(author , _author);
    }
};

class book:public common{
public:
    char publisher[66];
    int pagenumber;
    book(){}
    book(int _id,char _title[],char _author[],char _publisher[],char _isbnnumber[],int _pagenumber){
        common(_id,_title,_author);
        strcpy(publisher,_publisher);
        pagenumber = _pagenumber;
    }
};

class medialibrarymanagesystem{
public:

    medialibrarymanagesystem(){
        nowbooktotal = 0;
    }

    void _run(){
        readdatabyfile();
        printf("程序加载中");
        for (int i = 0; i < 10; i++) {
            sleep(100);    /* windows 使用sleep,参数为毫秒 */
            printf(".");
            fflush(stdout);//强制刷新缓存,输出显示
        }
        printf("\n");
        system("cls");
        int cmd;
        while(true){
            cout<<home_menu<<endl;
            cout<<"请输入你需要执行的命令序号:";cin>>cmd;
            if(cmd == 0){
                _exit();
            }
            system("cls");
            switch(cmd){
                case 1:
                    addbook();
                    break;
                case 2:
                    querygoods();
                    break;
                case 3:
                    showgoods();
                    break;
                case 4:
                    updategoods();
                    break;
                case 5:
                    deletegoods();
                    break;
                default:
                    cout<<"输入的指令有误!请重新输入!"<<endl;
            }
            system("pause");
            system("cls");

        }
    }





private:

    const string home_menu =
    "\n********************************** 图书管理系统 **********************************\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "*                                     1.添加                                       *\n"
    "*                                                                                  *\n"
    "*                                     2.查询                                       *\n"
    "*                                                                                  *\n"
    "*                                     3.显示图书库                                 *\n"
    "*                                                                                  *\n"
    "*                                     4.修改图书库                                 *\n"
    "*                                                                                  *\n"
    "*                                     5.删除                                       *\n"
    "*                                                                                  *\n"
    "*                                     0.退出                                       *\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "************************************************************************************\n";

    const string query_goods_menu =
    "\n************************************ 图书查询 ************************************\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "*                                    1.按标题查询                                  *\n"
    "*                                                                                  *\n"
    "*                                    2.按编号查询                                  *\n"
    "*                                                                                  *\n"
    "*                                    0.退出                                        *\n"
    "*                                                                                  *\n"
    "*                                                                                  *\n"
    "************************************************************************************\n";

    book booklist[bookmaxrepertory];

    int nowbooktotal;

    ///添加图书
    void addbook(){
        book book;
        cout<<"请输入编号:";cin>>book.id;
        cout<<"请输入标题:";cin>>book.title;
        cout<<"请输入作者:";cin>>book.author;
        cout<<"请输入出版社:";cin>>book.publisher;
        cout<<"请输入页数:";cin>>book.pagenumber;
        if(getbookbyid(book.id) != -1){
            cout<<"添加失败! 添加编号重复! 请重新添加!"<<endl;
        }else if(nowbooktotal == bookmaxrepertory){
            cout<<"添加失败! 图书库已满!"<<endl;
        }else{
            booklist[nowbooktotal ++] = book;
        }
    }

    /*  查询图书  */
    void querygoods(){
        int cmd;
        while(true){
            cout<<query_goods_menu<<endl;
            cout<<"请输入你需要执行的命令序号:";cin>>cmd;
            if(cmd == 0){
                break;
            }
            system("cls");
            switch(cmd){
                case 1:
                    querybytitle();
                    break;
                case 2:
                    querybyid();
                    break;
                default:
                    cout<<"输入的指令有误!请重新输入!"<<endl;
            }
            system("pause");
            system("cls");

        }
    }


    ///按标题查询
    void querybytitle(){
        int cmd;
        char title[66];
        cout<<"请输入标题:";cin>>title;
        getbookbytitle(title);
    }

    ///book
    void getbookbytitle(char title[]){
        bool flag = true;
        printf("%-6s%-10s%-10s%-10s%-10s%\n","编号","标题","作者","出版社","页数");
        for(int i = 0;i < nowbooktotal;i ++){
            if(strcmp(booklist[i].title,title) == 0){
                flag = false;
                printf("%-6d%-10s%-10s%-10s%-10d\n",booklist[i].id,booklist[i].title,booklist[i].author,booklist[i].publisher,booklist[i].pagenumber);
            }
        }
        if(flag){
            printf("\n\n空的!\n\n");
        }
    }


    ///按编号查询
    void querybyid(){
        int cmd,i,id;
        cout<<"请输入id:";cin>>id;
        i = getbookbyid(id);
        if(i == -1){
            printf("查找不到!\n");
        }else{
            printf("%-6s%-10s%-10s%-10s%-10s\n","编号","标题","作者","评级","出版社","页数");
            printf("%-6d%-10s%-10s%-10s%-10d\n",booklist[i].id,booklist[i].title,booklist[i].author,booklist[i].publisher,booklist[i].pagenumber);
        }
    }

    ///book
    int getbookbyid(int id){
        int index = -1;
        for(int i = 0;i < nowbooktotal;i ++){
            if(booklist[i].id == id){
                index = i;
                break;
            }
        }
        return index;
    }

    /*  显示图书库  */
    void showgoods(){
        if(nowbooktotal == 0){
            printf("空的!\n");
        }else{
            printf("%-6s%-10s%-10s%-10s%-10s\n","编号","标题","作者","出版社","页数");
            for(int i = 0;i < nowbooktotal;i ++){
                printf("%-6d%-10s%-10s%-10s%-10d\n",booklist[i].id,booklist[i].title,booklist[i].author,booklist[i].publisher,booklist[i].pagenumber);
            }
        }
    }

    /*  修改图书  */
    void updategoods(){
        int cmd,id,i;
        cout<<"请输入你需要修改的图书编号:";cin>>id;
        i = getbookbyid(id);
        if(i == -1){
                cout<<"图书不存在!"<<endl;
        }else{
            cout<<"原图书信息为:"<<endl;
            printf("%-6s%-10s%-10s%-10s%-10s\n","编号","标题","作者","出版社","页数");
            printf("%-6d%-10s%-10s%-10s%-10d\n",booklist[i].id,booklist[i].title,booklist[i].author,booklist[i].publisher,booklist[i].pagenumber);
            book book;
            book.id = id;
            cout<<"请输入修改后的标题:";cin>>book.title;
            cout<<"请输入修改后的作者:";cin>>book.author;
            cout<<"请输入修改后的出版社:";cin>>book.publisher;
            cout<<"请输入修改后的页数:";cin>>book.pagenumber;
            booklist[i] = book;
        }
    }

    /*  删除图书  */
    void deletegoods(){
        int cmd,id,i;
        if(nowbooktotal == 0){
            cout<<"空的!"<<endl;
   return;
        }
        cout<<"请输入你需要删除的图书编号:";cin>>id;
        deletebook(id);
    }

    ///book
    void deletebook(int id){
        int index = getbookbyid(id);
        if(index == -1){
            cout<<"没有该图书!"<<endl;
        }else{
            for(int i = index;i < nowbooktotal - 1;i ++){
                booklist[i] = booklist[i + 1];
            }
            nowbooktotal --;
            cout<<"删除成功!"<<endl;
        }
    }

    /*  保存图书  */
    void savedatatofile(){
        ///book
        file *bookdb = fopen("booklist.txt", "wb");
        for (int i = 0; i < nowbooktotal; i++) {
            fwrite(&booklist[i], sizeof(book), 1, bookdb);
        }
        fclose(bookdb);
    }

    /*  读取图书  */
    void readdatabyfile(){
        ///book
        file *bookdb = fopen("booklist.txt", "rb");
        nowbooktotal = 0;
        book book;
        while (fread(&book, sizeof(book), 1, bookdb) == 1) {
            booklist[nowbooktotal++] = book;
        }
        fclose(bookdb);
    }

    void _exit(){
        savedatatofile();
        system("cls");
        printf("正在退出");
        for (int i = 0; i < 10; i++) {
            sleep(100); 
            printf(".");
            fflush(stdout);//强制刷新缓存,输出显示
        }
        system("cls");
        printf("已退出!");
        exit(1);
    }

};

int main(){
    medialibrarymanagesystem medialibrarymanagesystem;
    medialibrarymanagesystem._run();
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。