用C语言制作图书管理系统
时间: 2023-09-08 21:09:55 浏览: 63
好的,下面是一个简单的图书管理系统的示例代码。这个示例代码提供了基本的图书信息管理功能,包括添加、删除、查询和修改图书信息。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
// 图书结构体
struct Book {
char name[50];
char author[50];
char publisher[50];
char isbn[20];
double price;
};
// 图书管理系统结构体
struct Library {
struct Book books[MAX_BOOKS];
int count;
};
// 添加图书
void add_book(struct Library *lib, const char *name, const char *author, const char *publisher, const char *isbn, double price) {
if (lib->count >= MAX_BOOKS) {
printf("The library is full!\n");
return;
}
struct Book book;
strcpy(book.name, name);
strcpy(book.author, author);
strcpy(book.publisher, publisher);
strcpy(book.isbn, isbn);
book.price = price;
lib->books[lib->count++] = book;
printf("Add book successfully!\n");
}
// 删除图书
void delete_book(struct Library *lib, const char *isbn) {
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].isbn, isbn) == 0) {
for (int j = i + 1; j < lib->count; j++) {
lib->books[j - 1] = lib->books[j];
}
lib->count--;
printf("Delete book successfully!\n");
return;
}
}
printf("The book is not found!\n");
}
// 查询图书
void query_book(struct Library *lib, const char *isbn) {
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].isbn, isbn) == 0) {
printf("Book name: %s\n", lib->books[i].name);
printf("Author: %s\n", lib->books[i].author);
printf("Publisher: %s\n", lib->books[i].publisher);
printf("ISBN: %s\n", lib->books[i].isbn);
printf("Price: %lf\n", lib->books[i].price);
return;
}
}
printf("The book is not found!\n");
}
// 修改图书
void update_book(struct Library *lib, const char *isbn, double new_price) {
for (int i = 0; i < lib->count; i++) {
if (strcmp(lib->books[i].isbn, isbn) == 0) {
lib->books[i].price = new_price;
printf("Update book successfully!\n");
return;
}
}
printf("The book is not found!\n");
}
int main() {
struct Library lib;
lib.count = 0;
// 添加图书
add_book(&lib, "C语言程序设计", "谭浩强", "清华大学出版社", "9787302339974", 48.0);
// 查询图书
query_book(&lib, "9787302339974");
// 修改图书
update_book(&lib, "9787302339974", 58.0);
// 删除图书
delete_book(&lib, "9787302339974");
return 0;
}
```
以上是一个基本的图书管理系统的示例代码,代码中使用结构体来存储图书信息,同时提供添加、删除、查询和修改图书信息的功能。在实际应用中,需要根据具体需求进行修改和优化。