用C语言设计一个计算机管理系统完成图书管理基本业务。 【基本要求】 1)每种书的登记内容包括书号、书名、著作者、现存量和库存量; 2)对书号建立索引表(线性表)以提高查找效率; 3)系统主要功能如下: *采编入库:新购一种书,确定书号后,登记到图书帐目表中,如果表中已有,则只将库存量增加; *借阅:如果一种书的现存量大于0,则借出一本,登记借阅者的书证号和归还期限,改变现存量; *归还:注销对借阅者的登记,改变该书的现存量。
时间: 2024-01-22 19:19:05 浏览: 95
首先,我们需要定义一些数据结构来存储图书信息和借阅信息:
```c
#define MAX_BOOKS 1000 // 最多存储的图书数量
// 图书信息结构体
typedef struct {
char id[20]; // 书号
char title[100]; // 书名
char author[50]; // 著作者
int stock; // 现存量
int total; // 库存量
} Book;
// 借阅信息结构体
typedef struct {
char book_id[20]; // 书号
int reader_id; // 读者证号
int return_date; // 归还日期
} Borrow;
```
接下来,我们需要定义一些函数来对图书信息和借阅信息进行增删改查操作:
```c
// 初始化图书列表
void init_books(Book books[], int *count);
// 添加一本新书
void add_book(Book books[], int *count, char id[], char title[], char author[], int total);
// 根据书号查找图书
Book *find_book(Book books[], int count, char id[]);
// 借出一本书
void borrow_book(Book books[], int count, char id[], int reader_id, int return_date);
// 归还一本书
void return_book(Book books[], int count, char id[], int reader_id);
// 初始化借阅列表
void init_borrows(Borrow borrows[], int *count);
// 添加一条借阅记录
void add_borrow(Borrow borrows[], int *count, char book_id[], int reader_id, int return_date);
// 根据书号和读者证号查找借阅记录
Borrow *find_borrow(Borrow borrows[], int count, char book_id[], int reader_id);
// 删除一条借阅记录
void remove_borrow(Borrow borrows[], int *count, char book_id[], int reader_id);
```
具体实现细节可以参考下面的代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 1000
typedef struct {
char id[20];
char title[100];
char author[50];
int stock;
int total;
} Book;
typedef struct {
char book_id[20];
int reader_id;
int return_date;
} Borrow;
void init_books(Book books[], int *count) {
*count = 0;
}
void add_book(Book books[], int *count, char id[], char title[], char author[], int total) {
// 先查找是否已经有该书
Book *book = find_book(books, *count, id);
if (book == NULL) { // 没有该书,添加新书
if (*count >= MAX_BOOKS) {
printf("图书数量已达上限,无法添加新书!\n");
return;
}
book = &books[*count];
strcpy(book->id, id);
strcpy(book->title, title);
strcpy(book->author, author);
book->stock = total;
book->total = total;
(*count)++;
} else { // 已有该书,增加库存量
book->stock += total;
book->total += total;
}
printf("成功添加新书:%s\n", title);
}
Book *find_book(Book books[], int count, char id[]) {
for (int i = 0; i < count; i++) {
if (strcmp(books[i].id, id) == 0) {
return &books[i];
}
}
return NULL;
}
void borrow_book(Book books[], int count, char id[], int reader_id, int return_date) {
// 查找该书是否存在
Book *book = find_book(books, count, id);
if (book == NULL) {
printf("未找到该书!\n");
return;
}
// 检查库存量是否足够
if (book->stock <= 0) {
printf("该书已经全部借出!\n");
return;
}
// 减少库存量,增加借阅记录
book->stock--;
add_borrow(borrows, &borrow_count, id, reader_id, return_date);
printf("借阅成功:%s\n", book->title);
}
void return_book(Book books[], int count, char id[], int reader_id) {
// 查找该书是否存在
Book *book = find_book(books, count, id);
if (book == NULL) {
printf("未找到该书!\n");
return;
}
// 增加库存量,删除借阅记录
book->stock++;
remove_borrow(borrows, &borrow_count, id, reader_id);
printf("归还成功:%s\n", book->title);
}
void init_borrows(Borrow borrows[], int *count) {
*count = 0;
}
void add_borrow(Borrow borrows[], int *count, char book_id[], int reader_id, int return_date) {
if (*count >= MAX_BOOKS) {
printf("借阅记录数量已达上限,无法添加新记录!\n");
return;
}
Borrow *borrow = &borrows[*count];
strcpy(borrow->book_id, book_id);
borrow->reader_id = reader_id;
borrow->return_date = return_date;
(*count)++;
}
Borrow *find_borrow(Borrow borrows[], int count, char book_id[], int reader_id) {
for (int i = 0; i < count; i++) {
if (strcmp(borrows[i].book_id, book_id) == 0 && borrows[i].reader_id == reader_id) {
return &borrows[i];
}
}
return NULL;
}
void remove_borrow(Borrow borrows[], int *count, char book_id[], int reader_id) {
for (int i = 0; i < *count; i++) {
if (strcmp(borrows[i].book_id, book_id) == 0 && borrows[i].reader_id == reader_id) {
// 找到该借阅记录,将其删除
for (int j = i; j < *count - 1; j++) {
borrows[j] = borrows[j + 1];
}
(*count)--;
return;
}
}
}
```
最后,我们可以编写一个主函数来测试我们的图书管理系统:
```c
int main() {
Book books[MAX_BOOKS];
int book_count = 0;
init_books(books, &book_count);
Borrow borrows[MAX_BOOKS];
int borrow_count = 0;
init_borrows(borrows, &borrow_count);
add_book(books, &book_count, "001", "C程序设计", "谭浩强", 100);
add_book(books, &book_count, "002", "数据结构", "严蔚敏", 50);
add_book(books, &book_count, "003", "计算机网络", "谢希仁", 80);
borrow_book(books, book_count, "001", 1234, 20211001);
borrow_book(books, book_count, "001", 5678, 20211010);
borrow_book(books, book_count, "002", 1234, 20211005);
borrow_book(books, book_count, "003", 5678, 20211008);
return_book(books, book_count, "001", 1234);
return_book(books, book_count, "001", 5678);
return_book(books, book_count, "002", 1234);
return_book(books, book_count, "003", 5678);
return 0;
}
```
阅读全文