用C语言写。 整个图书管理系统采用结构体作为基本数据结构,最终数据一定要保存到数据文件中。主函数通过switch 语句来根据用户的需求跳转到各个模块,以实现用户的需要。系统要求能进行图书信息的浏览、添加、查询、修改、删除和排序功能。基本要求: 1.图书信息的浏览:可打开数据文件,查看每本图书的图书条形码、书名、作者名、出版社、出版时间、定价、馆藏数量等。2.图书信息的添加:能够通过键盘录入图书条形码、书名、作者名、出版社、出版时间、定价、馆藏数量等,并将数据添加到数据文件中。3.图书信息的查询:(⑴)根据图书条形码查询2)根据书名查询(3)根据作者名查询(4)根据出版社查询,每种查询均列出查找到的所有条记录信息,且最下面需要显示出汇总信息,即共有多少条符合条件的记录。4.图书信息的修改:可根据图书条形码或书名查找出图书信息,输入是否要修改,若确认修改,则输入新的信息,再次确认后将修改后信息存储到文件中。5.图书信息的删除: (⑴)能够根据图书条形码除指定图书的记录(2)能够根据书名删除指定图书的记录,并将最终数据存储到文件中。6.图书信息的排序:(1)根据图书条形码排序(⑵)根据馆藏数量排序(3)根据出版时间排序,屏幕上显示排序后的所有图书信息。7.图书统计:(1)统计馆藏数量小于某个值的图书信息(2)统计出版日期早于某个值的图书信息。
时间: 2023-06-16 19:04:51 浏览: 173
很好,这是一个非常实用的项目。以下是一个简单的C语言代码示例,可以作为你的参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 1000
// 图书信息结构体
typedef struct {
char barcode[20];
char name[50];
char author[50];
char publisher[50];
char publish_date[20];
float price;
int amount;
} Book;
// 图书管理系统结构体
typedef struct {
Book books[MAX_BOOKS];
int count;
} BookManagementSystem;
// 初始化图书管理系统
void init(BookManagementSystem* system) {
system->count = 0;
}
// 读取图书信息文件
void load_file(BookManagementSystem* system) {
FILE* fp = fopen("book.dat", "rb");
if (fp != NULL) {
fread(&system->count, sizeof(int), 1, fp);
fread(system->books, sizeof(Book), system->count, fp);
fclose(fp);
}
}
// 保存图书信息文件
void save_file(BookManagementSystem* system) {
FILE* fp = fopen("book.dat", "wb");
if (fp != NULL) {
fwrite(&system->count, sizeof(int), 1, fp);
fwrite(system->books, sizeof(Book), system->count, fp);
fclose(fp);
}
}
// 添加图书信息
void add_book(BookManagementSystem* system) {
if (system->count >= MAX_BOOKS) {
printf("Error: the book list is full!\n");
return;
}
Book book;
printf("Barcode: ");
scanf("%s", book.barcode);
printf("Name: ");
scanf("%s", book.name);
printf("Author: ");
scanf("%s", book.author);
printf("Publisher: ");
scanf("%s", book.publisher);
printf("Publish date: ");
scanf("%s", book.publish_date);
printf("Price: ");
scanf("%f", &book.price);
printf("Amount: ");
scanf("%d", &book.amount);
system->books[system->count++] = book;
printf("Success: the book has been added!\n");
}
// 查询图书信息
void query_book(BookManagementSystem* system) {
int type;
char keyword[50];
printf("Query type (1: Barcode, 2: Name, 3: Author, 4: Publisher): ");
scanf("%d", &type);
printf("Keyword: ");
scanf("%s", keyword);
int count = 0;
for (int i = 0; i < system->count; i++) {
Book book = system->books[i];
char* str = NULL;
switch (type) {
case 1:
str = book.barcode;
break;
case 2:
str = book.name;
break;
case 3:
str = book.author;
break;
case 4:
str = book.publisher;
break;
default:
printf("Error: invalid query type!\n");
return;
}
if (strstr(str, keyword) != NULL) {
printf("Barcode: %s\n", book.barcode);
printf("Name: %s\n", book.name);
printf("Author: %s\n", book.author);
printf("Publisher: %s\n", book.publisher);
printf("Publish date: %s\n", book.publish_date);
printf("Price: %.2f\n", book.price);
printf("Amount: %d\n", book.amount);
printf("----------------------------\n");
count++;
}
}
printf("Total: %d\n", count);
}
// 修改图书信息
void modify_book(BookManagementSystem* system) {
int type;
char keyword[50];
printf("Query type (1: Barcode, 2: Name): ");
scanf("%d", &type);
printf("Keyword: ");
scanf("%s", keyword);
int index = -1;
for (int i = 0; i < system->count; i++) {
Book book = system->books[i];
char* str = NULL;
switch (type) {
case 1:
str = book.barcode;
break;
case 2:
str = book.name;
break;
default:
printf("Error: invalid query type!\n");
return;
}
if (strcmp(str, keyword) == 0) {
index = i;
break;
}
}
if (index < 0) {
printf("Error: no book found!\n");
return;
}
Book book = system->books[index];
printf("Barcode: %s\n", book.barcode);
printf("Name: %s\n", book.name);
printf("Author: %s\n", book.author);
printf("Publisher: %s\n", book.publisher);
printf("Publish date: %s\n", book.publish_date);
printf("Price: %.2f\n", book.price);
printf("Amount: %d\n", book.amount);
printf("----------------------------\n");
char confirm[10];
printf("Do you want to modify this book? (yes or no): ");
scanf("%s", confirm);
if (strcmp(confirm, "yes") == 0) {
printf("Barcode: ");
scanf("%s", book.barcode);
printf("Name: ");
scanf("%s", book.name);
printf("Author: ");
scanf("%s", book.author);
printf("Publisher: ");
scanf("%s", book.publisher);
printf("Publish date: ");
scanf("%s", book.publish_date);
printf("Price: ");
scanf("%f", &book.price);
printf("Amount: ");
scanf("%d", &book.amount);
system->books[index] = book;
printf("Success: the book has been modified!\n");
}
}
// 删除图书信息
void delete_book(BookManagementSystem* system) {
int type;
char keyword[50];
printf("Query type (1: Barcode, 2: Name): ");
scanf("%d", &type);
printf("Keyword: ");
scanf("%s", keyword);
int count = 0;
for (int i = 0; i < system->count; i++) {
Book book = system->books[i];
char* str = NULL;
switch (type) {
case 1:
str = book.barcode;
break;
case 2:
str = book.name;
break;
default:
printf("Error: invalid query type!\n");
return;
}
if (strcmp(str, keyword) == 0) {
count++;
}
}
if (count == 0) {
printf("Error: no book found!\n");
return;
}
Book* books = (Book*)malloc(count * sizeof(Book));
int j = 0;
for (int i = 0; i < system->count; i++) {
Book book = system->books[i];
char* str = NULL;
switch (type) {
case 1:
str = book.barcode;
break;
case 2:
str = book.name;
break;
default:
printf("Error: invalid query type!\n");
return;
}
if (strcmp(str, keyword) != 0) {
books[j++] = book;
}
}
system->count = j;
memcpy(system->books, books, j * sizeof(Book));
free(books);
printf("Success: %d books have been deleted!\n", count);
}
// 排序图书信息
void sort_book(BookManagementSystem* system) {
int type;
printf("Sort type (1: Barcode, 2: Amount, 3: Publish date): ");
scanf("%d", &type);
for (int i = 0; i < system->count - 1; i++) {
for (int j = i + 1; j < system->count; j++) {
Book* a = &system->books[i];
Book* b = &system->books[j];
switch (type) {
case 1:
if (strcmp(a->barcode, b->barcode) > 0) {
Book temp = *a;
*a = *b;
*b = temp;
}
break;
case 2:
if (a->amount < b->amount) {
Book temp = *a;
*a = *b;
*b = temp;
}
break;
case 3:
if (strcmp(a->publish_date, b->publish_date) > 0) {
Book temp = *a;
*a = *b;
*b = temp;
}
break;
default:
printf("Error: invalid sort type!\n");
return;
}
}
}
printf("Success: the book list has been sorted!\n");
}
// 统计图书信息
void statistics_book(BookManagementSystem* system) {
int type;
printf("Statistics type (1: Amount, 2: Publish date): ");
scanf("%d", &type);
int count = 0;
switch (type) {
case 1: {
int amount;
printf("Amount: ");
scanf("%d", &amount);
for (int i = 0; i < system->count; i++) {
if (system->books[i].amount < amount) {
count++;
}
}
printf("Total: %d\n", count);
break;
}
case 2: {
char date[20];
printf("Publish date: ");
scanf("%s", date);
for (int i = 0; i < system->count; i++) {
if (strcmp(system->books[i].publish_date, date) < 0) {
count++;
}
}
printf("Total: %d\n", count);
break;
}
default:
printf("Error: invalid statistics type!\n");
return;
}
}
// 显示菜单
void show_menu() {
printf("Book Management System\n");
printf("-----------------------\n");
printf("1. Add book\n");
printf("2. Query book\n");
printf("3. Modify book\n");
printf("4. Delete book\n");
printf("5. Sort book\n");
printf("6. Statistics book\n");
printf("7. Exit\n");
printf("-----------------------\n");
printf("Please input your choice: ");
}
int main() {
BookManagementSystem system;
init(&system);
load_file(&system);
while (1) {
show_menu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_book(&system);
save_file(&system);
break;
case 2:
query_book(&system);
break;
case 3:
modify_book(&system);
save_file(&system);
break;
case 4:
delete_book(&system);
save_file(&system);
break;
case 5:
sort_book(&system);
save_file(&system);
break;
case 6:
statistics_book(&system);
break;
case 7:
save_file(&system);
exit(0);
default:
printf("Error: invalid choice!\n");
}
}
return 0;
}
```
这个代码示例实现了基本的图书管理系统功能,包括添加、查询、修改、删除、排序和统计。你可以在此基础上进行扩展和优化。注意,这个示例代码并没有考虑并发和异常处理等情况,你需要进行额外的处理来保证程序的健壮性和可靠性。
阅读全文