用C语言写一个图书馆信息管理系统
时间: 2023-07-18 11:25:22 浏览: 89
好的,下面是一个简单的图书馆信息管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大书籍数量
#define MAX_TITLE_LEN 50 // 书名最大长度
#define MAX_AUTHOR_LEN 30 // 作者名最大长度
typedef struct {
char title[MAX_TITLE_LEN];
char author[MAX_AUTHOR_LEN];
int year;
} book_t;
book_t books[MAX_BOOKS]; // 图书信息数组
int num_books = 0; // 当前图书数量
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("Error: too many books!\n");
return;
}
book_t book;
printf("Enter book title: ");
scanf("%s", book.title);
printf("Enter author name: ");
scanf("%s", book.author);
printf("Enter year published: ");
scanf("%d", &book.year);
books[num_books] = book;
num_books++;
printf("Book added successfully.\n");
}
void search_book() {
char query[MAX_TITLE_LEN];
printf("Enter book title to search: ");
scanf("%s", query);
int found = 0;
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].title, query) == 0) {
printf("Book found:\n");
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Year: %d\n", books[i].year);
found = 1;
break;
}
}
if (!found) {
printf("Book not found.\n");
}
}
void print_books() {
printf("All books in library:\n");
for (int i = 0; i < num_books; i++) {
printf("%d. %s by %s (%d)\n", i+1, books[i].title, books[i].author, books[i].year);
}
}
int main() {
int choice = 0;
while (choice != 4) {
printf("Library Information Management System\n");
printf("1. Add book\n");
printf("2. Search book\n");
printf("3. Print all books\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
search_book();
break;
case 3:
print_books();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice, please try again.\n");
break;
}
}
return 0;
}
```
这个示例实现了一个简单的图书馆信息管理系统,其中包括添加书籍、搜索书籍和打印所有书籍等功能。你可以根据自己的需求对其进行修改和扩展。
阅读全文