新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

C语言怎么实现简单的图书管理系统,C语言怎么实现图形化界面

发布时间:2024-03-08 14:46:28

C语言怎样实现简单的图书管理系统

实现一个简单的图书管理系统可使用C语言来实现,以下是一个简单的示例代码:

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

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

// 初始化图书数组
struct Book library[100];
int num_books = 0;

// 添加图书函数
void addBook() {
    struct Book newBook;
    
    printf("Enter book title: ");
    scanf("%s", newBook.title);
    printf("Enter book author: ");
    scanf("%s", newBook.author);
    printf("Enter publication year: ");
    scanf("%d", &newBook.year);
    
    newBook.id = num_books + 1;
    
    library[num_books] = newBook;
    num_books++;
    printf("Book added successfully!
");
}

// 显示所有图书函数
void showBooks() {
    for (int i = 0; i < num_books; i++) {
        printf("ID: %d
", library[i].id);
        printf("Title: %s
", library[i].title);
        printf("Author: %s
", library[i].author);
        printf("Year: %d
", library[i].year);
        printf("
");
    }
}

int main() {
    int choice;
    
    do {
        printf("1. Add book
");
        printf("2. Show all books
");
        printf("3. Exit
");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                showBooks();
                break;
            case 3:
                printf("Exiting program...
");
                break;
            default:
                printf("Invalid choice. Try again.
");
                break;
        }
    } while (choice != 3);
    
    return 0;
}

这段代码实现了一个简单的图书管理系统,用户可以选择添加图书或显示所有图书的功能。图书被存储在一个结构体数组中,用户可以根据自己的需求扩大更多功能,比如删除图书、搜索图书等。