C++图书管理系统(基于结构体数组)

本文详细介绍了如何设计并实现一个图书馆管理信息系统,包括学生和管理员模块,涉及登录验证、图书查询、借阅管理等功能,通过C++代码实例展示了关键功能的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

问题描述
要求以图书馆管理业务为背景,设计并实现一个“图书馆管理信息系统”软件,使用该系统可以方便查询图书的信息、借阅者的个人等信息,实现借书与还书等功能。


一、需求分析

1.介绍

本系统的用户分为学生和管理员两类,其中学生为普通用户。普通用户只能对自己的信息进行查询以及借书、还书的权限,管理员用户则拥有系统的所有功能权限。

2.模块划分

(1)登录模块       登录模块用于用户登录,完成基本的验证。根据所填信息进行判断,提示“登录成功”  、     “账号或密码错误”等信息。 (2)学生模块     学生登录之后,可以浏览图书,借书,还书,以及查看个人借书清单等功能。 (3)管理员模块   管理员登录之后,可以对系统进行管理,原则上拥有所有用户的全部权限。主要功能有对学生的增删查, 对图书的增删查,以及查看全部借书清单等功能。

3.系统设计

 

二、代码实现

1.完整代码

代码如下(示例):


#include <iostream>
#include <string>
#include<fstream>
using namespace std;
//学生结构体数组最大容量
#define student_max 100
//图书结构体数组最大容量
#define book_max 200
//借书记录结构体数组最大容量
#define record_max 200

//学生结构体
struct Student {
    string id;       //学号
    string name;     //姓名
    string password; //密码
};

//学生结构体数组
struct StudentArray {
    StudentArray() : count(0) {

    }

    Student data[student_max];      //学生结构体数组
    int count;                      //学生数量
};
int find_StudentArray(StudentArray& sa, const string id);        //查找学生
bool add_StudentArray(StudentArray& sa, const Student& student);  //添加学生
bool del_StudentArray(StudentArray& sa, const string id);         //删除学生
//查找学生
int find_StudentArray(StudentArray& sa, const string id) {
    for (int index = 0; index < sa.count; index++) {
        if (sa.data[index].id == id) {
            return index;
        }
    }
    return -1;
}

//添加学生
bool add_StudentArray(StudentArray& sa, const Student& student) {
    int position = find_StudentArray(sa, student.id);
    if (position == -1) {
        sa.data[sa.count] = student;
        sa.count++;
        return true;
    }

    cout << "该学生已存在,无法添加!" << endl;
    return false;

}

//删除学生
bool del_StudentArray(StudentArray& sa, const string id) {
    int position = find_StudentArray(sa, id);
    if (position != -1) {
        for (int index = position; index < sa.count - 1; index++) {
            sa.data[index] = sa.data[index + 1];
        }
        sa.count--;
        return true;
    }
    cout << "未找到该学生,删除失败!" << endl;
    return false;
}

//图书结构体
struct Book {
    Book() :borrow(false) {

    }
    string id;   //编号
    string name; //书名
    bool borrow;        //借出 | 可借
};

//图书结构体数组
struct BookArray {
    BookArray() : count(0) {

    }

    Book data[book_max];        //图书结构体数组
    int count;                  //图书数量
};
int find_BookArray(BookArray& ba, const string id);    //查找图书
bool add_BookArray(BookArray& ba, const Book& book);   //添加图书
bool del_BookArray(BookArray& ba, const string id);    //删除图书
//查找图书
int find_BookArray(BookArray& ba, const string id) {
    for (int index = 0; index < ba.count; index++) {
        if (ba.data[index].id == id) {
            return index;
        }
    }
    return -1;
}

//添加图书
bool add_BookArray(BookArray& ba, const Book& book) {
    int position = find_BookArray(ba, book.id);
    if (position == -1) {
        ba.data[ba.count] = book;
        ba.count++;
        return true;
    }
    cout << "该图书已存在,添加失败!" << endl;
    return false;
}

//删除图书
bool del_BookArray(BookArray& ba, const string id) {
    int position = find_BookArray(ba, id);
    if (position != -1) {
        for (int index = position; index < ba.count - 1; index++) {
            ba.data[index] = ba.data[index + 1];
        }
        ba.count--;
        return true;
    }
    cout << "未找到该图书,删除失败!" << endl;
    return false;
}

//借书记录结构体
struct Record {
    string student_id;   //学生学号
    string student_name; //学生姓名
    string book_id;      //图书编号
    string book_name;    //图书书名
};

//借书记录结构体数组
struct RecordArray {
    RecordArray() : count(0) {

    }

    Record data[record_max];    //借书记录结构体数组
    int count;                  //借书记录数量
};
int find_RecordArray(RecordArray& ra, const string book_id);    //通过图书编号查找借书记录
bool add_RecordArray(RecordArray& ra, const Record& record);    //添加借书记录
bool del_RecordArray(RecordArray& ra, const string book_id);    //删除借书记录
//通过图书编号查找借书记录
int find_RecordArray(RecordArray& ra, const string book_id) {
    for (int index = 0; index < ra.count; ++index) {
        if (ra.data[index].book_id == book_id) {
            return index;
        }
    }
    return -1;
}

//添加借书记录
bool add_RecordArray(RecordArray& ra, const Record& record) {
    int position = find_RecordArray(ra, record.book_id);
    if (position == -1) {
        ra.data[ra.count++] = record;
        return true;
    }
    cout << "该借书记录已存在,添加失败!" << endl;
    return false;
}

//删除借书记录
bool del_RecordArray(RecordArray& ra, const string book_id) {
    int position = find_RecordArray(ra, book_id);
    if (position != -1) {
        for (int index = position; index < ra.count - 1; index++) {
            ra.data[index] = ra.data[index + 1];
        }
        ra.count--;
        cout << "删除成功!" << endl;
        return true;
    }
    cout << "未找到该借书记录,删除失败!" << endl;
    return false;
}

//管理员登录
void loginAdmin() {
    do {
        cout << "# 管理员登录 #" << endl;
        string id;
        string password;
        cout << "账号:";
        cin >> id;
        cout << "密码:";
        cin >> password;
        if (id == "admin" && password == "123456") {
            break;
        }
        cout << "账号或者密码错误,请重新输入!" << endl;
    } while (true);
}

//学生登录
Student& loginStudent(StudentArray& sa) {
    do {
        cout << "# 学生登录 #" << endl;
        string id;
        string password;
        cout << "账号:";
        cin >> id;
        cout << "密码:";
        cin >> password;
        int position = find_StudentArray(sa, id);
        if (position != -1) {
            if (sa.data[position].password == password) {
                return sa.data[position];
            }
        }
        cout << "账号或者密码错误,请重新输入!" << endl;
    } while (true);
}

void printBook(BookArray& ba);                                //浏览图书
void borrowBook(Student& my, BookArray& ba, RecordArray& ra);  //借书
void returnBook(Student& my, BookArray& ba, RecordArray& ra); //还书
void printRecord(Student& my, RecordArray& ra);               //学生借书清单
//学生操作菜单
void menuStudent(StudentArray& sa, BookArray& ba, RecordArray& ra) {
    Student& my = loginStudent(sa);
    int option = 0;
    do {
        cout << "# 图书管理系统 - 学生 #" << endl;
        cout << "1 > 浏览图书" << endl;
        cout << "2 > 借书 #" << endl;
        cout << "3 > 还书 #" << endl;
        cout << "4 > 借书清单 #" << endl;
        cout << "0 > 注销 #" << endl;
        cin >> option;
        switch (option) {
        case 1:
            printBook(ba);
            break;
        case 2:
            borrowBook(my, ba, ra);
            break;
        case 3:
            returnBook(my, ba, ra);
            break;
        case 4:
            printRecord(my, ra);
            break;
        }
    } while (option != 0);
}


//浏览图书
void printBook(BookArray& ba) {
    cout << "# 浏览图书 #" << endl;
    for (int index = 0; index < ba.count; index++) {
        Book& book = ba.data[index];
        cout << book.id << " " << book.name << " ";
        if (book.borrow) {
            cout << "借出";
        }
        else {
            cout << "可借";
        }
        cout << endl;
    }
}

//借书
void borrowBook(Student& my, BookArray& ba, RecordArray& ra) {
    cout << "# 借书 #" << endl;
    cout << "输入图书编号:";
    string book_id;
    cin >> book_id;
    int position = find_BookArray(ba, book_id);
    if (position != -1) {
        Book& book = ba.data[position];
        if (!book.borrow) {
            book.borrow = true;
            Record record;
            record.student_id = my.id;
            record.student_name = my.name;
            record.book_id = book.id;
            record.book_name = book.name;
            add_RecordArray(ra, record);
            cout << "恭喜,借书成功!" << endl;
        }
        else {
            cout << "抱歉,该书已经出借!" << endl;
        }
    }
    else {
        cout << "抱歉,未找到图书信息!" << endl;
    }
}

//还书
void returnBook(Student& my, BookArray& ba, RecordArray& ra) {
    cout << "# 还书 #" << endl;
    cout << "输入图书编号:";
    string book_id;
    cin >> book_id;
    int position = find_BookArray(ba, book_id);
    if (position != -1) {
        Book& book = ba.data[position];
        if (book.borrow) {
            book.borrow = false;
            del_RecordArray(ra, book_id);
            cout << "恭喜,还书成功!" << endl;
        }
        else {
            cout << "抱歉,该书尚未出借!" << endl;
        }
    }
    else {
        cout << "抱歉,未找到图书信息!" << endl;
    }
}

//学生借书清单
void printRecord(Student& my, RecordArray& ra) {
    cout << "# 借书清单 #" << endl;
    for (int index = 0; index < ra.count; ++index) {
        Record& record = ra.data[index];
        if (record.student_id == my.id) {
            cout << record.book_id << " " << record.book_name << endl;
        }
    }
}


void printStudent(StudentArray& sa);   //浏览学生
void addBook(BookArray& ba);           //添加图书
void delBook(BookArray& ba);           //删除图书
void addStudent(StudentArray& sa);     //添加学生
void delStudent(StudentArray& sa);     //删除学生
void printRecord(RecordArray& ra);     //借书清单(全部)
//管理员操作菜单
void menuAdmin(StudentArray& sa, BookArray& ba, RecordArray& ra) {
    loginAdmin();
    int option = 0;
    do {
        cout << "# 图书管理系统 - 管理员 #" << endl;
        cout << "1 > 浏览图书 #" << endl;
        cout << "2 > 添加图书 #" << endl;
        cout << "3 > 删除图书 #" << endl;
        cout << "4 > 浏览学生 #" << endl;
        cout << "5 > 添加学生 #" << endl;
        cout << "6 > 删除学生 #" << endl;
        cout << "7 > 借书清单(全部) #" << endl;
        cout << "0 > 注销 #" << endl;
        cin >> option;
        switch (option) {
        case 1:
            printBook(ba);
            break;
        case 2:
            addBook(ba);
            break;
        case 3:
            delBook(ba);
            break;
        case 4:
            printStudent(sa);
            break;
        case 5:
            addStudent(sa);
            break;
        case 6:
            delStudent(sa);
            break;
        case 7:
            printRecord(ra);
            break;
        }
    } while (option != 0);
}

//浏览学生
void printStudent(StudentArray& sa) {
    cout << "# 浏览学生 #" << endl;
    for (int index = 0; index < sa.count; ++index) {
        Student& student = sa.data[index];
        cout << student.id << " " << student.name << " ";
        cout << student.password << endl;
    }
}

//添加图书
void addBook(BookArray& ba) {
    cout << "# 添加图书 #" << endl;
    Book book;
    book.borrow = false;
    cout << "输入图书编号:";
    cin >> book.id;
    cout << "输入图书名称:";
    cin >> book.name;
    if (add_BookArray(ba, book)) {
        cout << "恭喜,添加成功!" << endl;
    }
    else {
        cout << "抱歉,添加失败!" << endl;
    }
}

//删除图书
void delBook(BookArray& ba) {
    cout << "# 删除图书 #" << endl;
    string book_id;
    cout << "输入图书编号:";
    cin >> book_id;
    if (del_BookArray(ba, book_id)) {
        cout << "恭喜,删除成功!" << endl;
    }
    else {
        cout << "抱歉,删除失败!" << endl;
    }
}

//添加学生
void addStudent(StudentArray& sa) {
    cout << "# 添加学生 #" << endl;
    Student student;
    cout << "输入学生编号:";
    cin >> student.id;
    cout << "输入学生姓名:";
    cin >> student.name;
    cout << "输入学生密码:";
    cin >> student.password;
    if (add_StudentArray(sa, student)) {
        cout << "恭喜,添加成功!" << endl;
    }
    else {
        cout << "抱歉,添加失败!" << endl;
    }
}

//删除学生
void delStudent(StudentArray& sa) {
    cout << "# 删除学生 #" << endl;
    string student_id;
    cout << "输入学生编号:";
    cin >> student_id;
    if (del_StudentArray(sa, student_id)) {
        cout << "恭喜,删除成功!" << endl;
    }
    else {
        cout << "抱歉,删除失败!" << endl;
    }
}
//借书清单(全部)
void printRecord(RecordArray& ra) {
    cout << "# 借书清单(全部) #" << endl;
    for (int index = 0; index < ra.count; ++index) {
        Record& record = ra.data[index];
        cout << record.book_id << " " << record.book_name << " ";
        cout << record.student_id << " " << record.student_name << endl;
    }
}
//数据读入
//学生数据读入
void read_student(StudentArray& sa) {
    Student s;
    ifstream stfile;
    stfile.open("student.txt");
    if (!stfile.is_open()) {
        cout << "文件打开失败!" << endl;
    }

    while (stfile >> s.id >> s.name >> s.password) {
        add_StudentArray(sa, s);
    }
    stfile.close();
}
//图书数据读入
void read_book(BookArray& ba) {
    Book b;
    ifstream bkfile;
    bkfile.open("book.txt");
    if (!bkfile.is_open()) {
        cout << "文件打开失败!" << endl;
    }
    while (bkfile >> b.id >> b.name) {
        add_BookArray(ba, b);
    }

    bkfile.close();
}
//数据读出
//学生数据读出
void write_student(StudentArray& sa) {
    Student s;
    ofstream stfile;
    stfile.open("student.txt");
    if (!stfile.is_open()) {
        cout << "文件打开失败!" << endl;
    }

    for (int i = 0; i < sa.count; i++) {
        stfile << sa.data[i].id << " " << sa.data[i].name << " " << sa.data[i].password << endl;
    }
    stfile.close();
}
//图书数据读出
void write_book(BookArray& ba) {
    Book b;
    ofstream bkfile;
    bkfile.open("book.txt");
    if (!bkfile.is_open()) {
        cout << "文件打开失败!" << endl;
    }
    for (int i = 0; i < ba.count; i++) {
        bkfile << ba.data[i].id << " " << ba.data[i].name << endl;
    }
    bkfile.close();
}
//主菜单
void menuMain() {

    StudentArray sa;
    BookArray ba;
    RecordArray ra;
    read_student(sa);
    read_book(ba);
    int option = 0;
    do {
        cout << "# 图书管理系统 #" << endl;
        cout << "1 > 学生登录" << endl;
        cout << "2 > 管理员登录 #" << endl;
        cout << "0 > 退出 #" << endl;
        cin >> option;
        switch (option) {
        case 1:
            menuStudent(sa, ba, ra);
            break;
        case 2:
            menuAdmin(sa, ba, ra);
            break;
        }
    } while (option != 0);
    write_student(sa);
    write_book(ba);
}

int main() {


    menuMain();

    return 0;
}


总结


以上就是今天要讲的内容,本文仅仅简单介绍了图书管理系统的实现,希望能给大家带来帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值