图书馆系统(C++)

cpp 复制代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#define MAX_BOOKS 10000
#define MAX_USERS 10000

using namespace std;

struct Book {
    int book_id;
    char title[100];
    char author[100];
    int available;
    int total;
};

struct bb {
    int bookid;   //所借阅书籍的编号 
    bb* next;   // 指向下一本借阅的书的指针
};

struct User {
    int userid;
    char name[15];
    long long phone;
    int bbn; // 借阅书数
    bb* booklist;  // 借阅的书:指向链表头结点的指针
    User* next;
};

User* userslist = NULL;

int sum = 0;
struct Book books[MAX_BOOKS];
int num_books = 0;
int num_users = 0;

int choose();

int mainMenu();

int bookMenu();

void addbook();

void borrowbook();

void returnbook();

void searchbook();

void deletebook();

int book();

void readBook();

void writeBook();

int user();

void writeUser();

void readUser();

int main() {
    while (1) {
        int choice = mainMenu();
        switch (choice) {
        case 1:
            book();
            break;
        case 2:
            user();
            break;
        case 3:
            exit(0);
            break;

        default:
            printf("无效数字!\n");
            //system("pause");
            break;
        }
    }
}

int choose()
{
    int choice = -1;
    printf("输入选择:");
    scanf("%d", &choice);
    return choice;
}

//主系统 
int mainMenu()
{
    printf("\n****************************\n");
    printf("*        图书馆系统        *\n");
    printf("*      1.图书管理系统      *\n");
    printf("*      2.会员管理系统      *\n");
    printf("*      3. 退出系统         *\n");
    printf("****************************\n");
    return choose();
}

int bookMenu()
{
    printf("\n**********************************\n");
    printf("*           图书管理系统         *\n");
    printf("*          1. 添加图书           *\n");
    printf("*          2. 借阅图书           *\n");
    printf("*          3. 归还图书           *\n");
    printf("*          4. 查询图书           *\n");
    printf("*          5. 删除图书           *\n");
    printf("*          6. 保存修改           *\n");
    printf("*          7. 返回主系统         *\n");
    printf("*          8. 切换会员管理系统   *\n");
    printf("*          9. 退出系统           *\n");
    printf("**********************************\n");
    return choose();
}

//添加图书
void addBook() {
    // 检查是否已存在该书
    int book_id, count;
    char title[100];
    char author[100];
    printf("图书id:");
    scanf("%d", &book_id);
    printf("书名:");
    scanf("%s", title);
    printf("作者名:");
    scanf("%s", author);
    printf("添加数量:");
    scanf("%d", &count);
    if (count + sum > MAX_BOOKS)
    {
        printf("图书馆存储空间已满,无法添加!");
        return;
    }
    for (int i = 0; i < num_books; i++) {
        if (books[i].book_id == book_id) {
            books[i].total += count;
            books[i].available += count;
            sum = sum + count;
            printf("图书添加成功!");
            return;
        }
    }

    // 不存在则添加新书
    books[num_books].book_id = book_id;
    strcpy(books[num_books].title, title);
    strcpy(books[num_books].author, author);
    books[num_books].total = count;
    books[num_books].available = count;
    sum = sum + count;
    num_books++;
    printf("图书添加成功!");
    return;
}

void borrowBook()
{
    long long phone;
    int bookid;
    cout << "请输入借阅会员的手机号:";
    cin >> phone;
    User* c = userslist;
    while (c != NULL && c->phone != phone)
    {
        c = c->next; // 寻找要借书的会员
    }

    if (c == NULL)
    {
        cout << "未找到该手机号对应的会员信息!" << endl;
        return;
    }

    // 当会员借阅书籍数目达到上限时,提示借阅书籍已达上限
    if (c->bbn >= 3) {
        cout << "借阅书籍已达上限,无法再借阅更多书籍。" << endl;
        return;
    }
    cout << "请输入要借阅图书的编号:";
    cin >> bookid;
    for (int i = 0; i < num_books; i++)
    {
        if (books[i].book_id == bookid)
        {
            if (books[i].available > 0)
            {
                books[i].available--;
                printf("借阅成功\n");
                sum--;

                // 创建新的书籍节点
                bb* newbook = new bb();	//newBook
                newbook->bookid = bookid;
                newbook->next = NULL;

                if (c->bbn == 0)
                {
                    c->booklist = newbook; // 如果之前未借过书,直接把新书籍节点设为书单的头
                }
                else
                {
                    bb* cb = c->booklist;	//currentBook
                    while (cb->next != NULL)
                    {
                        cb = cb->next; // 找到书单的末尾节点
                    }
                    cb->next = newbook; // 将新书籍节点插入到末尾节点之后
                    cb = cb->next;
                    cb->next = NULL;
                }
                c->bbn++;
            }
            else
            {
                printf("该书已全部借出\n");
            }
            return;
        }
    }
    printf("未找到该书\n");
    return;
}

//归还图书 
void returnBook()
{
    long long phone;
    int book_id;
    char title[100];
    char author[100];
    printf("请输入归还者的手机号:");
    scanf("%lld", &phone);
    User* c = userslist;
    while (c != NULL && c->phone != phone)
    {
        c = c->next; // 寻找要借书的会员
    }

    if (c == NULL)
    {
        cout << "未找到该手机号对应的会员信息!" << endl;
        return;
    }
    printf("图书id:");
    scanf("%d", &book_id);
    if ((sum + 1) > MAX_BOOKS)
    {
        printf("图书馆存储空间已满,无法添加!");
        return;
    }
    for (int i = 0; i < num_books; i++)
    {
        if (books[i].book_id == book_id)
        {
            if (books[i].available < books[i].total)
            {
                books[i].available++;
                sum++;
                printf("归还成功\n");
                bb* newbook = new bb();	//newBook
                newbook->bookid = book_id;
                newbook->next = NULL;

                if (c->bbn == 0)
                {
                    c->booklist = newbook; // 如果之前未借过书,直接把新书籍节点设为书单的头
                }
                else
                {
                    bb* cb = c->booklist;	//currentBook
                    while (cb->next != NULL)
                    {
                        cb = cb->next; // 找到书单的末尾节点
                    }
                    cb->next = newbook; // 将新书籍节点插入到末尾节点之后
                    cb = cb->next;
                    cb->next = NULL;
                }
                c->bbn++;
                return;
            }
            else
            {
                printf("此书非待归还状态!\n");
                return;
            }

        }
    }
    printf("未找到该书!");
}

//查找图书 
void searchBook() {
    char keyword[100];
    int found = 0;
    printf("输入查找信息(书名/作者): ");
    scanf("%s", &keyword);
    printf("查找结果:\n");
    for (int i = 0; i < num_books; i++) {
        if (strcmp(books[i].title, keyword) == 0 || strcmp(books[i].author, keyword) == 0)
        {
            printf("书名: %s, 作者: %s,可借阅数目:%d,图书总数:%d", books[i].title, books[i].author, books[i].available, books[i].total);
            found = 1;
        }
    }

    if (!found) {
        printf("无符合要求图书!\n");
    }
}

//删除图书 
void deleteBook() {
    char title[100];
    char author[100];
    int found = 0;
    printf("输入要删除的书名: ");
    scanf("%s", title);
    printf("输入要删除图书的作者名: ");
    scanf("%s", author);
    for (int i = 0; i < num_books; i++) {
        if (strcmp(books[i].title, title) == 0 && strcmp(books[i].author, author) == 0) {
            for (int j = i; j < num_books - 1; j++) {
                books[j] = books[j + 1];
            }
            found = 1;
            break;
        }
    }

    if (found) {
        num_books--;
        printf("图书删除成功!\n");
    }
    else {
        printf("未找到要删除的图书!\n");
    }
}

void writeBook()
{
    FILE* fp = NULL;
    fp = fopen("Book.txt", "w+");//覆盖写入文件 
    int i;
    for (i = 0; i < num_books; i++) {
        fprintf(fp, "%d %s %s %d %d\n", books[i].book_id, books[i].title, books[i].author, books[i].available, books[i].total);
    }
    fclose(fp);
    return;
}

void readBook()
{
    FILE* fp = NULL;
    if (NULL == (fp = fopen("Book.txt", "r"))) {//没有 Book.txt
        return;                               //fclose(fp);
    }
    int i = 0;
    while (
        fscanf(fp, "%d%s%s%d%d\n", &books[i].book_id, books[i].title, books[i].author, &books[i].available, &books[i].total) != EOF
        ) {
        i++;
    }
    num_books = i;
    fclose(fp);
}

int book()
{
    readBook();

    while (1) {
        int choice = bookMenu();
        switch (choice) {
        case 1:
            addBook();
            break;
        case 2:
            borrowBook();
            break;
        case 3:
            returnBook();
            break;
        case 4:
            searchBook();
            break;
        case 5:
            deleteBook();
            break;
        case 6:
            readUser();
            writeBook();
            writeUser();
            printf("修改信息成功!");
            break;
        case 7:
            mainMenu();
            break;
        case 8:
            user();
            break;
        case 9:
            printf("\n\n感谢您的使用!\n\n");
            exit(0);
            break;

        default:
            printf("无效数字!\n");
            //system("pause");
            break;
        }
    }
    return choose();
}

void addUser() {
    char name[15];
    long long phone;
    User* newUser = new User(); // 创建新的会员节点
    newUser->next = NULL;
    printf("请输入要添加的会员姓名:");
    scanf("%s", name);
    printf("请输入要添加会员的手机号:");
    cin >> phone;
    newUser->phone = phone;
    strcpy(newUser->name, name);
    newUser->bbn = 0;
    newUser->booklist = NULL;
    newUser->userid = num_users;
    if (userslist == NULL)
    {
        userslist = newUser; // 如果会员链表为空,直接将新会员节点设为头节点
    }
    else
    {
        User* current = userslist;
        while (current->next != NULL)
        {
            current = current->next; // 找到会员链表的末尾节点
        }
        current->next = newUser; // 将新会员节点插入到末尾节点之后
    }
    printf("会员添加成功!\n");
    num_users++;
}

// 查询会员编号、姓名、手机号、已借阅书籍量、具体借阅书籍 
void searchUser() {
    long long phone;
    cout << "请输入要查询会员的手机号:";
    cin >> phone;

    User* current = userslist;
    while (current != NULL)
    {
        if (current->phone == phone)
        {
            cout << "会员编号:" << current->userid << endl;
            cout << "会员姓名:" << current->name << endl;
            cout << "会员手机号:" << current->phone << endl;
            cout << "已借阅书籍数量:" << current->bbn << endl;
            // 打印会员已借阅的书籍
            if (current->bbn == 0)
            {
                cout << "无借阅记录!" << endl;
                return;
            }
            else
            {
                bb* bookPtr = current->booklist;
                cout << "借阅的书籍:";
                while (bookPtr != NULL)
                {
                    cout << bookPtr->bookid << ", ";
                    bookPtr = bookPtr->next;
                }
                cout << endl;
                return;
            }
        }
        current = current->next;
    }
    cout << "未找到该手机号对应的会员信息!\n" << endl;
}

//删除会员信息
void deleteUser()
{
    long long phone;
    int found = 0;
    printf("输入要删除会员的手机号: ");
    cin >> phone;
    User* current = userslist;
    User* p = NULL;
    // 寻找要删除的会员节点,并记录其前一个节点
    while (current != NULL && current->phone != phone)
    {
        p = current;
        current = current->next;
    }

    // 如果未找到要删除的会员
    if (current == NULL)
    {
        cout << "未找到要删除的会员!" << endl;
        return;
    }

    // 如果要删除的是头节点
    if (p == NULL)
    {
        userslist = current->next;
    }
    else
    {
        p->next = current->next;
    }

    // 释放要删除的节点的内存
    delete current;
    cout << "会员删除成功!" << endl;
    num_users--;
    return;
}


void writeUser()
{
    FILE* fp = NULL;
    fp = fopen("User.txt", "w+");//覆盖写入文件 
    User* member = new User;
    member = userslist;
    while (member != NULL)
    {
        fprintf(fp, "%d %s %lld %d ", member->userid, member->name, member->phone, member->bbn);
        bb* p = new bb;
        p = member->booklist;
        while (p != NULL)
        {
            fprintf(fp, "%s ", p->bookid);
            p = p->next;
        }
        fprintf(fp, "\n");
        member = member->next;
    }
    fclose(fp);
    printf("保存信息成功!\n");
    return;
}

void readUser() {
    FILE* fp = fopen("User.txt", "r");

    User* member = NULL;
    User* p = NULL;
    bb* book = NULL;

    char line[1000];
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        member = new User();
        int bytesRead = sscanf(line, "%d %s %lld %d", &member->userid, member->name, &member->phone, &member->bbn);
        //if (bytesRead != 4) {
        //    cout << "文件格式错误" << endl;
        //    delete member;
        //    continue;
        //}
        for (int i = 0; i < member->bbn; ++i)
        {
            int bookid;
            book = new bb();
            book->bookid = NULL;
            book->next = NULL;

            if (member->booklist == NULL)
            {
                member->booklist = book;
            }
            else
            {
                bb* temp = member->booklist;
                while (temp->next != NULL)
                {
                    temp = temp->next;
                }
                temp->next = book;
            }
        }

        if (p != NULL)
        {
            p->next = member;
            num_users++;
        }
        else
        {
            userslist = member;
        }
        p = member;
    }
    fclose(fp);
    cout << "读取信息成功!" << endl;
    num_users++;
}

int userMenu()
{
    printf("\n**********************************\n");
    printf("*           会员管理系统         *\n");
    printf("*          1. 增加会员           *\n");
    printf("*          2. 查询会员           *\n");
    printf("*          3. 删除会员           *\n");
    printf("*          4. 保存修改           *\n");
    printf("*          5. 返回主系统         *\n");
    printf("*          6. 切换图书管理系统   *\n");
    printf("*          7. 退出系统           *\n");
    printf("**********************************\n");
    return choose();
}

int user()
{
    readUser();

    while (1) {
        int choice = userMenu();
        switch (choice) {
        case 1:
            addUser();
            break;
        case 2:
            searchUser();
            break;
        case 3:
            deleteUser();
            break;
        case 4:
            readBook();
            writeUser();
            writeBook();
            break;
        case 5:
            mainMenu();
            break;
        case 6:
            book();
            break;
        case 7:
            printf("\n\n感谢您的使用!\n\n");
            exit(0);
            break;

        default:
            printf("无效数字!\n");
            //system("pause");
            break;
        }
    }
    return choose();
}
相关推荐
LNTON羚通5 小时前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
哭泣的眼泪4086 小时前
解析粗糙度仪在工业制造及材料科学和建筑工程领域的重要性
python·算法·django·virtualenv·pygame
Microsoft Word7 小时前
c++基础语法
开发语言·c++·算法
天才在此7 小时前
汽车加油行驶问题-动态规划算法(已在洛谷AC)
算法·动态规划
莫叫石榴姐8 小时前
数据科学与SQL:组距分组分析 | 区间分布问题
大数据·人工智能·sql·深度学习·算法·机器学习·数据挖掘
茶猫_9 小时前
力扣面试题 - 25 二进制数转字符串
c语言·算法·leetcode·职场和发展
肥猪猪爸11 小时前
使用卡尔曼滤波器估计pybullet中的机器人位置
数据结构·人工智能·python·算法·机器人·卡尔曼滤波·pybullet
readmancynn11 小时前
二分基本实现
数据结构·算法
萝卜兽编程11 小时前
优先级队列
c++·算法
盼海11 小时前
排序算法(四)--快速排序
数据结构·算法·排序算法