请用c语言设计一个图书登记管理程序。该程序应该具有下列功能: (1) 能从文件导入图书的信息 (2) 能从键盘录入图书的信息; (3) 给定图书号,显示某本图书信息; 能根据图书号修改图书的信息 能根据图书号删除图书的信息; 能特图书信息导出到指定文件
时间: 2023-04-07 14:01:41 浏览: 102
好的,这是一个关于图书登记管理的问题,我可以回答。以下是一个简单的C语言程序,可以实现这些功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct book {
char id[20];
char title[100];
char author[50];
int year;
float price;
};
struct book books[MAX_BOOKS];
int num_books = 0;
void load_books(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
while (num_books < MAX_BOOKS && fscanf(fp, "%s %s %s %d %f",
books[num_books].id, books[num_books].title, books[num_books].author,
&books[num_books].year, &books[num_books].price) == 5) {
num_books++;
}
fclose(fp);
}
void save_books(char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
for (int i = 0; i < num_books; i++) {
fprintf(fp, "%s %s %s %d %.2f\n",
books[i].id, books[i].title, books[i].author,
books[i].year, books[i].price);
}
fclose(fp);
}
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("Error: too many books\n");
return;
}
printf("Enter book ID: ");
scanf("%s", books[num_books].id);
printf("Enter book title: ");
scanf("%s", books[num_books].title);
printf("Enter book author: ");
scanf("%s", books[num_books].author);
printf("Enter book year: ");
scanf("%d", &books[num_books].year);
printf("Enter book price: ");
scanf("%f", &books[num_books].price);
num_books++;
}
void find_book() {
char id[20];
printf("Enter book ID: ");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(id, books[i].id) == 0) {
printf("Book found:\n");
printf("ID: %s\n", books[i].id);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Year: %d\n", books[i].year);
printf("Price: %.2f\n", books[i].price);
return;
}
}
printf("Book not found\n");
}
void modify_book() {
char id[20];
printf("Enter book ID: ");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(id, books[i].id) == 0) {
printf("Enter new book title (or press Enter to keep the same): ");
scanf("%s", books[i].title);
printf("Enter new book author (or press Enter to keep the same): ");
scanf("%s", books[i].author);
printf("Enter new book year (or press 0 to keep the same): ");
scanf("%d", &books[i].year);
printf("Enter new book price (or press 0 to keep the same): ");
scanf("%f", &books[i].price);
return;
}
}
printf("Book not found\n");
}
void delete_book() {
char id[20];
printf("Enter book ID: ");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(id, books[i].id) == 0) {
for (int j = i; j < num_books - 1; j++) {
books[j] = books[j + 1];
}
num_books--;
printf("Book deleted\n");
return;
}
}
printf("Book not found\n");
}
void export_book() {
char id[20], filename[100];
printf("Enter book ID: ");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(id, books[i].id) == 0) {
printf("Enter filename to export: ");
scanf("%s", filename);
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
fprintf(fp, "%s %s %s %d %.2f\n",
books[i].id, books[i].title, books[i].author,
books[i].year, books[i].price);
fclose(fp);
printf("Book exported to file %s\n", filename);
return;
}
}
printf("Book not found\n");
}
int main() {
load_books("books.txt");
while (1) {
printf("Enter command (add, find, modify, delete, export, quit): ");
char cmd[10];
scanf("%s", cmd);
if (strcmp(cmd, "add") == 0) {
add_book();
} else if (strcmp(cmd, "find") == 0) {
find_book();
} else if (strcmp(cmd, "modify") == 0) {
modify_book();
} else if (strcmp(cmd, "delete") == 0) {
delete_book();
} else if (strcmp(cmd, "export") == 0) {
export_book();
} else if (strcmp(cmd, "quit") == 0) {
break;
} else {
printf("Invalid command\n");
}
}
save_books("books.txt");
return 0;
}
```
这个程序使用了一个结构体 `book` 来存储每本书的信息,包括图书号、书名、作者、出版年份和价格。程序可以从文件中导入图书信息,也可以从键盘录入图书信息。给定图书号,程序可以显示某本图书的信息,也可以根据图书号修改图书的信息或删除图书的信息。程序还可以将特定的图书信息导出到指定文件。
阅读全文