【附源码 C语言数据结构期末课程设计:图书管理系统】

发布时间:2024年01月23日

系列文章

C语言数据结构期末课程设计:图书管理系统



前言

很多学校的计算机专业开展了数据结构这一门课程,以下的内容将为这些学子提供一个码代码的思维方式,本文尾部附带有作者自己的源代码,代码只用于学术交流,禁止商用或盗用,违者后果自负!!!


一、个人图书管理系统问题描述:

对个人图书进行管理,用文件来存储书籍的各种信息,包括书号、书名、作者名。
基本要求:
(1)提供查询功能,按照书名或作者名查找需要的书籍;
(2)提供插入删除还有更新功能;
(3)提供排序功能,按照某种需求对所有的图书进行排序,并按排序的结果进行显示。

二、代码步骤

1.main函数书写

代码如下(示例):

int main()
{
    BookManager manager;
    initBookManager(&manager);

    int choice, book_id;
    char book_name[50], author_name[50], keyword[50];

    loadBooksFromFile(&manager);

    do {
        printf("\n图书管理系统\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("0. 退出\n");
        printf("请输入您的选择:");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            printf("请输入书号:");
            scanf("%d", &book_id);
            printf("请输入书名:");
            scanf("%s", book_name);
            printf("请输入作者名:");
            scanf("%s", author_name);
            addBook(&manager, book_id, book_name, author_name);
            break;
        case 2:
            printf("请输入要删除的图书的书号:");
            scanf("%d", &book_id);
            deleteBook(&manager, book_id);
            break;
        case 3:
            printf("请输入要更新的图书的书号:");
            scanf("%d", &book_id);
            printf("请输入新的书名:");
            scanf("%s", book_name);
            printf("请输入新的作者名:");
            scanf("%s", author_name);
            updateBook(&manager, book_id, book_name, author_name);
            break;
        case 4:
            printf("请输入查询关键字:");
            scanf("%s", keyword);
            searchBook(&manager, keyword);
            break;
        case 5:
            sortBooksById(&manager);
            printf("图书已按书号排序。\n");
            break;
        case 6:
            sortBooksByName(&manager);
            printf("图书已按书名排序。\n");
            break;
        case 7:
            printBooks(&manager);
            break;
        case 8:
            saveBooksToFile(&manager);
            break;
        case 0:
            printf("感谢使用图书管理系统,再见!\n");
            break;
        default:
            printf("无效的选择,请重新输入。\n");
            break;
        }
    } while (choice != 0);

    return 0;
}

2.引入库与头文件书写

代码如下(示例):

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BOOKS 100
#define FILE_PATH "books.txt"

3.结构体的定义与初始化

代码如下(示例):

// 定义图书结构体
typedef struct {
    int book_id;
    char book_name[50];
    char author_name[50];
} Book;

// 定义图书管理系统结构体
typedef struct {
    Book books[MAX_BOOKS];
    int num_books;
} BookManager;

// 初始化图书管理系统
void initBookManager(BookManager* manager) {
    manager->num_books = 0;
}

4.图书的增加

代码如下(示例):

// 添加图书
void addBook(BookManager* manager, int book_id, const char* book_name, const char* author_name) {
    if (manager->num_books >= MAX_BOOKS) {
        printf("图书数量已达到上限,无法添加新图书。\n");
        return;
    }

    Book* book = &(manager->books[manager->num_books]);
    book->book_id = book_id;
    strcpy(book->book_name, book_name);
    strcpy(book->author_name, author_name);

    manager->num_books++;

    printf("图书添加成功。\n");
}

5.图书的删除

代码如下(示例):


// 删除图书
void deleteBook(BookManager* manager, int book_id) {
    int i, found = 0;

    for (i = 0; i < manager->num_books; i++) {
        if (manager->books[i].book_id == book_id) {
            found = 1;
            break;
        }
    }

    if (found) {
        for (; i < manager->num_books - 1; i++) {
            manager->books[i] = manager->books[i + 1];
        }

        manager->num_books--;

        printf("图书删除成功。\n");
    }
    else {
        printf("未找到指定的图书。\n");
    }
}

6.图书的更改

代码如下(示例):


// 更新图书
void updateBook(BookManager* manager, int book_id, const char* book_name, const char* author_name) {
    int i, found = 0;

    for (i = 0; i < manager->num_books; i++) {
        if (manager->books[i].book_id == book_id) {
            found = 1;
            break;
        }
    }

    if (found) {
        Book* book = &(manager->books[i]);
        strcpy(book->book_name, book_name);
        strcpy(book->author_name, author_name);

        printf("图书更新成功。\n");
    }
    else {
        printf("未找到指定的图书。\n");
    }
}

7.按书名或作者名查询图书

代码如下(示例):

// 按书名或作者名查询图书
void searchBook(BookManager* manager, const char* keyword) {
    int i, found = 0;

    printf("查询结果:\n");

    for (i = 0; i < manager->num_books; i++) {
        if (strstr(manager->books[i].book_name, keyword) != NULL ||
            strstr(manager->books[i].author_name, keyword) != NULL) {
            printf("书号:%d,书名:%s,作者:%s\n",
                manager->books[i].book_id,
                manager->books[i].book_name,
                manager->books[i].author_name);

            found = 1;
        }
    }

    if (!found) {
        printf("未找到符合条件的图书。\n");
    }
}

8.按书号排序图书

代码如下(示例):


// 按书号排序图书
void sortBooksById(BookManager* manager) {
    int i, j;
    Book temp;

    for (i = 0; i < manager->num_books - 1; i++) {
        for (j = 0; j < manager->num_books - i - 1; j++) {
            if (manager->books[j].book_id > manager->books[j + 1].book_id) {
                temp = manager->books[j];
                manager->books[j] = manager->books[j + 1];
                manager->books[j + 1] = temp;
            }
        }
    }
}

9.按书名排序图书

代码如下(示例):


// 按书名排序图书
void sortBooksByName(BookManager* manager) {
    int i, j;
    Book temp;

    for (i = 0; i < manager->num_books - 1; i++) {
        for (j = 0; j < manager->num_books - i - 1; j++) {
            if (strcmp(manager->books[j].book_name, manager->books[j + 1].book_name) > 0) {
                temp = manager->books[j];
                manager->books[j] = manager->books[j + 1];
                manager->books[j + 1] = temp;
            }
        }
    }
}

10.打印所有图书

代码如下(示例):


// 打印所有图书
void printBooks(BookManager* manager) {
    int i;

    printf("图书列表:\n");

    for (i = 0; i < manager->num_books; i++) {
        printf("书号:%d,书名:%s,作者:%s\n",
            manager->books[i].book_id,
            manager->books[i].book_name,
            manager->books[i].author_name);
    }
}

11.保存图书信息到文件

代码如下(示例):


// 保存图书信息到文件
void saveBooksToFile(BookManager* manager) {
    FILE* file = fopen(FILE_PATH, "w");

    if (file == NULL) {
        printf("无法打开文件进行保存。\n");
        return;
    }

    for (int i = 0; i < manager->num_books; i++) {
        fprintf(file, "%d,%s,%s\n",
            manager->books[i].book_id,
            manager->books[i].book_name,
            manager->books[i].author_name);
    }

    fclose(file);

    printf("图书信息已保存到文件。\n");
}

12.从文件中读取图书信息

代码如下(示例):


// 从文件中读取图书信息
void loadBooksFromFile(BookManager* manager) {
    FILE* file = fopen(FILE_PATH, "r");

    if (file == NULL) {
        printf("无法打开文件进行读取。\n");
        return;
    }

    int book_id;
    char book_name[50];
    char author_name[50];

    while (fscanf(file, "%d,%[^,],%[^\n]\n", &book_id, book_name, author_name) != EOF) {
        addBook(manager, book_id, book_name, author_name);
    }

    fclose(file);

    printf("图书信息已从文件中读取。\n");
}

三、源代码

1.代码整体

代码如下(示例):

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BOOKS 100
#define FILE_PATH "books.txt"

// 定义图书结构体
typedef struct {
    int book_id;
    char book_name[50];
    char author_name[50];
} Book;

// 定义图书管理系统结构体
typedef struct {
    Book books[MAX_BOOKS];
    int num_books;
} BookManager;

// 初始化图书管理系统
void initBookManager(BookManager* manager) {
    manager->num_books = 0;
}

// 添加图书
void addBook(BookManager* manager, int book_id, const char* book_name, const char* author_name) {
    if (manager->num_books >= MAX_BOOKS) {
        printf("图书数量已达到上限,无法添加新图书。\n");
        return;
    }

    Book* book = &(manager->books[manager->num_books]);
    book->book_id = book_id;
    strcpy(book->book_name, book_name);
    strcpy(book->author_name, author_name);

    manager->num_books++;

    printf("图书添加成功。\n");
}

// 删除图书
void deleteBook(BookManager* manager, int book_id) {
    int i, found = 0;

    for (i = 0; i < manager->num_books; i++) {
        if (manager->books[i].book_id == book_id) {
            found = 1;
            break;
        }
    }

    if (found) {
        for (; i < manager->num_books - 1; i++) {
            manager->books[i] = manager->books[i + 1];
        }

        manager->num_books--;

        printf("图书删除成功。\n");
    }
    else {
        printf("未找到指定的图书。\n");
    }
}

// 更新图书
void updateBook(BookManager* manager, int book_id, const char* book_name, const char* author_name) {
    int i, found = 0;

    for (i = 0; i < manager->num_books; i++) {
        if (manager->books[i].book_id == book_id) {
            found = 1;
            break;
        }
    }

    if (found) {
        Book* book = &(manager->books[i]);
        strcpy(book->book_name, book_name);
        strcpy(book->author_name, author_name);

        printf("图书更新成功。\n");
    }
    else {
        printf("未找到指定的图书。\n");
    }
}

// 按书名或作者名查询图书
void searchBook(BookManager* manager, const char* keyword) {
    int i, found = 0;

    printf("查询结果:\n");

    for (i = 0; i < manager->num_books; i++) {
        if (strstr(manager->books[i].book_name, keyword) != NULL ||
            strstr(manager->books[i].author_name, keyword) != NULL) {
            printf("书号:%d,书名:%s,作者:%s\n",
                manager->books[i].book_id,
                manager->books[i].book_name,
                manager->books[i].author_name);

            found = 1;
        }
    }

    if (!found) {
        printf("未找到符合条件的图书。\n");
    }
}

// 按书号排序图书
void sortBooksById(BookManager* manager) {
    int i, j;
    Book temp;

    for (i = 0; i < manager->num_books - 1; i++) {
        for (j = 0; j < manager->num_books - i - 1; j++) {
            if (manager->books[j].book_id > manager->books[j + 1].book_id) {
                temp = manager->books[j];
                manager->books[j] = manager->books[j + 1];
                manager->books[j + 1] = temp;
            }
        }
    }
}

// 按书名排序图书
void sortBooksByName(BookManager* manager) {
    int i, j;
    Book temp;

    for (i = 0; i < manager->num_books - 1; i++) {
        for (j = 0; j < manager->num_books - i - 1; j++) {
            if (strcmp(manager->books[j].book_name, manager->books[j + 1].book_name) > 0) {
                temp = manager->books[j];
                manager->books[j] = manager->books[j + 1];
                manager->books[j + 1] = temp;
            }
        }
    }
}

// 打印所有图书
void printBooks(BookManager* manager) {
    int i;

    printf("图书列表:\n");

    for (i = 0; i < manager->num_books; i++) {
        printf("书号:%d,书名:%s,作者:%s\n",
            manager->books[i].book_id,
            manager->books[i].book_name,
            manager->books[i].author_name);
    }
}

// 保存图书信息到文件
void saveBooksToFile(BookManager* manager) {
    FILE* file = fopen(FILE_PATH, "w");

    if (file == NULL) {
        printf("无法打开文件进行保存。\n");
        return;
    }

    for (int i = 0; i < manager->num_books; i++) {
        fprintf(file, "%d,%s,%s\n",
            manager->books[i].book_id,
            manager->books[i].book_name,
            manager->books[i].author_name);
    }

    fclose(file);

    printf("图书信息已保存到文件。\n");
}

// 从文件中读取图书信息
void loadBooksFromFile(BookManager* manager) {
    FILE* file = fopen(FILE_PATH, "r");

    if (file == NULL) {
        printf("无法打开文件进行读取。\n");
        return;
    }

    int book_id;
    char book_name[50];
    char author_name[50];

    while (fscanf(file, "%d,%[^,],%[^\n]\n", &book_id, book_name, author_name) != EOF) {
        addBook(manager, book_id, book_name, author_name);
    }

    fclose(file);

    printf("图书信息已从文件中读取。\n");
}

int main() {
    BookManager manager;
    initBookManager(&manager);

    int choice, book_id;
    char book_name[50], author_name[50], keyword[50];

    loadBooksFromFile(&manager);

    do {
        printf("\n图书管理系统\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("0. 退出\n");
        printf("请输入您的选择:");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            printf("请输入书号:");
            scanf("%d", &book_id);
            printf("请输入书名:");
            scanf("%s", book_name);
            printf("请输入作者名:");
            scanf("%s", author_name);
            addBook(&manager, book_id, book_name, author_name);
            break;
        case 2:
            printf("请输入要删除的图书的书号:");
            scanf("%d", &book_id);
            deleteBook(&manager, book_id);
            break;
        case 3:
            printf("请输入要更新的图书的书号:");
            scanf("%d", &book_id);
            printf("请输入新的书名:");
            scanf("%s", book_name);
            printf("请输入新的作者名:");
            scanf("%s", author_name);
            updateBook(&manager, book_id, book_name, author_name);
            break;
        case 4:
            printf("请输入查询关键字:");
            scanf("%s", keyword);
            searchBook(&manager, keyword);
            break;
        case 5:
            sortBooksById(&manager);
            printf("图书已按书号排序。\n");
            break;
        case 6:
            sortBooksByName(&manager);
            printf("图书已按书名排序。\n");
            break;
        case 7:
            printBooks(&manager);
            break;
        case 8:
            saveBooksToFile(&manager);
            break;
        case 0:
            printf("感谢使用图书管理系统,再见!\n");
            break;
        default:
            printf("无效的选择,请重新输入。\n");
            break;
        }
    } while (choice != 0);

    return 0;
}

总结

以上就是今天要讲的内容,感谢您的阅读

文章来源:https://blog.csdn.net/weixin_72553621/article/details/135741669
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。