C语言实现简单的图书管理系统(c语言图书馆管理)

  • 功能要求:
    1. 针对图书的管理:新增图书,修改图书,删除图书 ,借阅图书 ,归还图书。针对学生的管理:新增学生 ,修改学生,删除学生

代码分为多个文件,包括头文件和源文件,使用链表来管理图书和学生信息,并通过文件IO进行数据的持久化存储。

项目结构

book_management/
│
├── main.c          # 主程序
├── book_management.h    # 头文件,声明函数和常量
└── book_management.c    # 实现文件,定义函数

1. 头文件 book_management.h

#ifndef BOOK_MANAGEMENT_H
#define BOOK_MANAGEMENT_H

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

#define MAX_TITLE_LENGTH 100
#define MAX_AUTHOR_LENGTH 100
#define MAX_NAME_LENGTH 100

typedef struct Book {
    int id;
    char title[MAX_TITLE_LENGTH];
    char author[MAX_AUTHOR_LENGTH];
    int is_borrowed;
    struct Book* next;
} Book;

typedef struct Student {
    int id;
    char name[MAX_NAME_LENGTH];
    struct Student* next;
} Student;

// 图书管理函数
void add_book(Book** head, int id, const char* title, const char* author);
void modify_book(Book* head, int id, const char* title, const char* author);
void delete_book(Book** head, int id);
void borrow_book(Book* head, int id);
void return_book(Book* head, int id);
void display_books(Book* head);
void save_books_to_file(Book* head, const char* filename);
void load_books_from_file(Book** head, const char* filename);

// 学生管理函数
void add_student(Student** head, int id, const char* name);
void modify_student(Student* head, int id, const char* name);
void delete_student(Student** head, int id);
void display_students(Student* head);
void save_students_to_file(Student* head, const char* filename);
void load_students_from_file(Student** head, const char* filename);

#endif // BOOK_MANAGEMENT_H

2. 源文件 book_management.c

#include "book_management.h"

// 图书管理函数实现

void add_book(Book** head, int id, const char* title, const char* author) {
    Book* new_book = (Book*)malloc(sizeof(Book));
    new_book->id = id;
    strcpy(new_book->title, title);
    strcpy(new_book->author, author);
    new_book->is_borrowed = 0;
    new_book->next = *head;
    *head = new_book;
}

void modify_book(Book* head, int id, const char* title, const char* author) {
    Book* current = head;
    while (current != NULL) {
        if (current->id == id) {
            strcpy(current->title, title);
            strcpy(current->author, author);
            break;
        }
        current = current->next;
    }
}

void delete_book(Book** head, int id) {
    Book* temp = *head;
    Book* prev = NULL;

    if (temp != NULL && temp->id == id) {
        *head = temp->next;
        free(temp);
        return;
    }

    while (temp != NULL && temp->id != id) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) return;

    prev->next = temp->next;
    free(temp);
}

void borrow_book(Book* head, int id) {
    Book* current = head;
    while (current != NULL) {
        if (current->id == id) {
            if (current->is_borrowed) {
                printf("Book is already borrowed.\n");
            } else {
                current->is_borrowed = 1;
                printf("Book borrowed successfully.\n");
            }
            break;
        }
        current = current->next;
    }
}

void return_book(Book* head, int id) {
    Book* current = head;
    while (current != NULL) {
        if (current->id == id) {
            if (current->is_borrowed) {
                current->is_borrowed = 0;
                printf("Book returned successfully.\n");
            } else {
                printf("Book was not borrowed.\n");
            }
            break;
        }
        current = current->next;
    }
}

void display_books(Book* head) {
    Book* current = head;
    while (current != NULL) {
        printf("ID: %d, Title: %s, Author: %s, Borrowed: %s\n",
               current->id, current->title, current->author,
               current->is_borrowed ? "Yes" : "No");
        current = current->next;
    }
}

void save_books_to_file(Book* head, const char* filename) {
    FILE* file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error opening file.\n");
        return;
    }

    Book* current = head;
    while (current != NULL) {
        fprintf(file, "%d %s %s %d\n", current->id, current->title, current->author, current->is_borrowed);
        current = current->next;
    }

    fclose(file);
}

void load_books_from_file(Book** head, const char* filename) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error opening file.\n");
        return;
    }

    int id, is_borrowed;
    char title[MAX_TITLE_LENGTH], author[MAX_AUTHOR_LENGTH];
    while (fscanf(file, "%d %s %s %d", &id, title, author, &is_borrowed) != EOF) {
        add_book(head, id, title, author);
        if (is_borrowed) {
            borrow_book(*head, id);
        }
    }

    fclose(file);
}

// 学生管理函数实现

void add_student(Student** head, int id, const char* name) {
    Student* new_student = (Student*)malloc(sizeof(Student));
    new_student->id = id;
    strcpy(new_student->name, name);
    new_student->next = *head;
    *head = new_student;
}

void modify_student(Student* head, int id, const char* name) {
    Student* current = head;
    while (current != NULL) {
        if (current->id == id) {
            strcpy(current->name, name);
            break;
        }
        current = current->next;
    }
}

void delete_student(Student** head, int id) {
    Student* temp = *head;
    Student* prev = NULL;

    if (temp != NULL && temp->id == id) {
        *head = temp->next;
        free(temp);
        return;
    }

    while (temp != NULL && temp->id != id) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) return;

    prev->next = temp->next;
    free(temp);
}

void display_students(Student* head) {
    Student* current = head;
    while (current != NULL) {
        printf("ID: %d, Name: %s\n", current->id, current->name);
        current = current->next;
    }
}

void save_students_to_file(Student* head, const char* filename) {
    FILE* file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error opening file.\n");
        return;
    }

    Student* current = head;
    while (current != NULL) {
        fprintf(file, "%d %s\n", current->id, current->name);
        current = current->next;
    }

    fclose(file);
}

void load_students_from_file(Student** head, const char* filename) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error opening file.\n");
        return;
    }

    int id;
    char name[MAX_NAME_LENGTH];
    while (fscanf(file, "%d %s", &id, name) != EOF) {
        add_student(head, id, name);
    }

    fclose(file);
}

3. 主程序 main.c

#include "book_management.h"

int main() {
    Book* book_head = NULL;
    Student* student_head = NULL;

    // 加载数据
    load_books_from_file(&book_head, "books.txt");
    load_students_from_file(&student_head, "students.txt");

    int choice;
    do {
        printf("\n1. Add Book\n2. Modify Book\n3. Delete Book\n4. Borrow Book\n5. Return Book\n");
        printf("6. Add Student\n7. Modify Student\n8. Delete Student\n9. Display Books\n10. Display Students\n11. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        int id;
        char title[MAX_TITLE_LENGTH], author[MAX_AUTHOR_LENGTH], name[MAX_NAME_LENGTH];

        switch (choice) {
            case 1:
                printf("Enter book ID, title, and author: ");
                scanf("%d %s %s", &id, title, author);
                add_book(&book_head, id, title, author);
                break;
            case 2:
                printf("Enter book ID, new title, and new author: ");
                scanf("%d %s %s", &id, title, author);
                modify_book(book_head, id, title, author);
                break;
            case 3:
                printf("Enter book ID to delete: ");
                scanf("%d", &id);
                delete_book(&book_head, id);
                break;
            case 4:
                printf("Enter book ID to borrow: ");
                scanf("%d", &id);
                borrow_book(book_head, id);
                break;
            case 5:
                printf("Enter book ID to return: ");
                scanf("%d", &id);
                return_book(book_head, id);
                break;
            case 6:
                printf("Enter student ID and name: ");
                scanf("%d %s", &id, name);
                add_student(&student_head, id, name);
                break;
            case 7:
                printf("Enter student ID and new name: ");
                scanf("%d %s", &id, name);
                modify_student(student_head, id, name);
                break;
            case 8:
                printf("Enter student ID to delete: ");
                scanf("%d", &id);
                delete_student(&student_head, id);
                break;
            case 9:
                display_books(book_head);
                break;
            case 10:
                display_students(student_head);
                break;
            case 11:
                save_books_to_file(book_head, "books.txt");
                save_students_to_file(student_head, "students.txt");
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 11);

    return 0;
}

4. 编译和运行

将上述代码分别保存为 book_management.h、book_management.c 和 main.c 文件,然后使用以下命令编译和运行程序:

gcc main.c book_management.c -o book_management
./book_management

5. 说明

  • 该程序使用链表来管理图书和学生信息。
  • 图书和学生信息通过文件IO进行持久化存储,分别保存在 books.txt 和 students.txt 文件中。
  • 程序提供了简单的命令行界面,用户可以通过输入数字来选择不同的操作

未实现的功能:未建立已借出图书和借阅学生之间的关联关系,如果要做,可增加另外一个结构体,其中包含了学生id、图书id信息,在借书时,新创建节点,存于学生和已经借阅图书的链表中;归还图书时,在学生和已经借阅图书的链表中找到对应图书,并删除该节点。


以上实现了一个基本的图书管理系统,其他功能可在该版本上进行扩展。

原文链接:,转发请注明来源!