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

// 定义图书结构体 typedef struct { char title[100]; char author[100]; int year; } Book;

// 定义图书管理系统结构体 typedef struct { Book* books; int numBooks; int maxBooks; } Library;

// 初始化图书管理系统 void initLibrary(Library* library, int maxBooks) { library->books = (Book*)malloc(maxBooks * sizeof(Book)); library->numBooks = 0; library->maxBooks = maxBooks; }

// 添加图书 void addBook(Library* library, const char* title, const char* author, int year) { if (library->numBooks >= library->maxBooks) { printf("图书库已满,无法添加新书。\n"); return; }

Book newBook;
strcpy(newBook.title, title);
strcpy(newBook.author, author);
newBook.year = year;

library->books[library->numBooks] = newBook;
library->numBooks++;

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

}

// 显示图书 void displayBooks(Library* library) { if (library->numBooks == 0) { printf("图书库为空。\n"); return; }

printf("图书列表:\n");
for (int i = 0; i < library->numBooks; i++) {
    printf("书名:%s  作者:%s  出版年份:%d\n", library->books[i].title, library->books[i].author, library->books[i].year);
}

}

// 主函数 int main() { Library library; int maxBooks;

printf("请输入图书库的最大容量:");
scanf("%d", &maxBooks);
initLibrary(&library, maxBooks);

int choice;
char title[100];
char author[100];
int year;

while (1) {
    printf("\n");
    printf("图书管理系统\n");
    printf("1. 添加图书\n");
    printf("2. 显示图书\n");
    printf("3. 退出\n");
    printf("请选择操作:");
    scanf("%d", &choice);
    
    switch (choice) {
        case 1:
            printf("请输入书名:");
            scanf("%s", title);
            printf("请输入作者:");
            scanf("%s", author);
            printf("请输入出版年份:");
            scanf("%d", &year);
            addBook(&library, title, author, year);
            break;
        case 2:
            displayBooks(&library);
            break;
        case 3:
            printf("谢谢使用,再见!\n");
            exit(0);
        default:
            printf("无效的选择,请重新输入。\n");
            break;
    }
}

return 0;
用c语言编写图书管理系统

原文地址: https://www.cveoy.top/t/topic/h8DN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录