写一个图书信息管理系统C语言代码,要求:1. 图书信息的浏览: 可打开数据文件,查看每本图书的图书条形码、书名、作者名、出版社、出版时间、定价、馆藏数量等。
时间: 2023-07-04 22:24:35 浏览: 110
下面是一个简单的图书信息管理系统的 C 语言代码示例。该程序可以打开一个数据文件,允许用户查看每本书的信息并进行一些简单的操作,如添加新书、删除书籍等。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50
// 定义图书结构体
typedef struct {
char barcode[MAX_LEN];
char title[MAX_LEN];
char author[MAX_LEN];
char publisher[MAX_LEN];
char publish_date[MAX_LEN];
float price;
int count;
} Book;
// 声明函数
void show_menu();
void show_books(Book* books, int count);
void add_book(Book* books, int* count);
void delete_book(Book* books, int* count);
void save_books(Book* books, int count);
void load_books(Book* books, int* count);
int main() {
Book books[100];
int count = 0;
int choice;
load_books(books, &count);
do {
show_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
show_books(books, count);
break;
case 2:
add_book(books, &count);
break;
case 3:
delete_book(books, &count);
break;
case 4:
save_books(books, count);
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 5);
return 0;
}
// 显示菜单
void show_menu() {
printf("\n");
printf("1. Show books\n");
printf("2. Add book\n");
printf("3. Delete book\n");
printf("4. Save books\n");
printf("5. Quit\n");
printf("Enter your choice: ");
}
// 显示所有书籍信息
void show_books(Book* books, int count) {
printf("Barcode\tTitle\tAuthor\tPublisher\tPublish Date\tPrice\tCount\n");
for (int i = 0; i < count; i++) {
printf("%s\t%s\t%s\t%s\t%s\t%.2f\t%d\n",
books[i].barcode, books[i].title, books[i].author,
books[i].publisher, books[i].publish_date, books[i].price, books[i].count);
}
}
// 添加一本新书
void add_book(Book* books, int* count) {
Book book;
printf("Enter barcode: ");
scanf("%s", book.barcode);
printf("Enter title: ");
scanf("%s", book.title);
printf("Enter author: ");
scanf("%s", book.author);
printf("Enter publisher: ");
scanf("%s", book.publisher);
printf("Enter publish date: ");
scanf("%s", book.publish_date);
printf("Enter price: ");
scanf("%f", &book.price);
printf("Enter count: ");
scanf("%d", &book.count);
books[*count] = book;
(*count)++;
}
// 删除一本书
void delete_book(Book* books, int* count) {
char barcode[MAX_LEN];
printf("Enter barcode of book to delete: ");
scanf("%s", barcode);
for (int i = 0; i < *count; i++) {
if (strcmp(books[i].barcode, barcode) == 0) {
for (int j = i; j < *count - 1; j++) {
books[j] = books[j+1];
}
(*count)--;
printf("Book deleted!\n");
return;
}
}
printf("Book not found!\n");
}
// 保存书籍信息到文件
void save_books(Book* books, int count) {
FILE* fp = fopen("books.dat", "wb");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
fwrite(books, sizeof(Book), count, fp);
fclose(fp);
printf("Books saved to file!\n");
}
// 从文件中加载书籍信息
void load_books(Book* books, int* count) {
FILE* fp = fopen("books.dat", "rb");
if (fp == NULL) {
printf("No data file found.\n");
return;
}
fread(books, sizeof(Book), 100, fp);
fclose(fp);
for (int i = 0; i < 100; i++) {
if (strlen(books[i].barcode) > 0) {
(*count)++;
}
}
printf("%d books loaded from file.\n", *count);
}
```
注意:该程序中使用了一个数据文件 "books.dat" 来存储书籍信息。在运行程序前,请确保该文件已经存在。如果该文件不存在,程序将提示用户无法打开数据文件。
阅读全文