帮我用c语言写一个图书管理系统,其中包括录入图书,显示图书,查询图书,修改图书,删除图书,录入读者信息,查询读者信息,修改读者信息,删除读者信息这些功能
时间: 2023-06-15 15:07:24 浏览: 153
好的,以下是一个简单的图书管理系统的代码示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_READERS 100
struct Book {
char title[50];
char author[50];
int year;
float price;
};
struct Reader {
char name[50];
int age;
char email[50];
};
struct Book books[MAX_BOOKS];
int num_books = 0;
struct Reader readers[MAX_READERS];
int num_readers = 0;
void add_book() {
struct Book book;
printf("Enter title: ");
scanf("%s", book.title);
printf("Enter author: ");
scanf("%s", book.author);
printf("Enter year: ");
scanf("%d", &book.year);
printf("Enter price: ");
scanf("%f", &book.price);
books[num_books++] = book;
printf("Book added successfully.\n");
}
void display_books() {
if (num_books == 0) {
printf("No books found.\n");
return;
}
printf("Title\tAuthor\tYear\tPrice\n");
for (int i = 0; i < num_books; i++) {
struct Book book = books[i];
printf("%s\t%s\t%d\t%.2f\n", book.title, book.author, book.year, book.price);
}
}
void search_book() {
char title[50];
printf("Enter title to search: ");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
struct Book book = books[i];
if (strcmp(book.title, title) == 0) {
printf("Title\tAuthor\tYear\tPrice\n");
printf("%s\t%s\t%d\t%.2f\n", book.title, book.author, book.year, book.price);
return;
}
}
printf("Book not found.\n");
}
void edit_book() {
char title[50];
printf("Enter title to edit: ");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
struct Book *book = &books[i];
if (strcmp(book->title, title) == 0) {
printf("Enter new title: ");
scanf("%s", book->title);
printf("Enter new author: ");
scanf("%s", book->author);
printf("Enter new year: ");
scanf("%d", &book->year);
printf("Enter new price: ");
scanf("%f", &book->price);
printf("Book edited successfully.\n");
return;
}
}
printf("Book not found.\n");
}
void delete_book() {
char title[50];
printf("Enter title to delete: ");
scanf("%s", title);
for (int i = 0; i < num_books; i++) {
struct Book *book = &books[i];
if (strcmp(book->title, title) == 0) {
for (int j = i; j < num_books - 1; j++) {
books[j] = books[j + 1];
}
num_books--;
printf("Book deleted successfully.\n");
return;
}
}
printf("Book not found.\n");
}
void add_reader() {
struct Reader reader;
printf("Enter name: ");
scanf("%s", reader.name);
printf("Enter age: ");
scanf("%d", &reader.age);
printf("Enter email: ");
scanf("%s", reader.email);
readers[num_readers++] = reader;
printf("Reader added successfully.\n");
}
void display_readers() {
if (num_readers == 0) {
printf("No readers found.\n");
return;
}
printf("Name\tAge\tEmail\n");
for (int i = 0; i < num_readers; i++) {
struct Reader reader = readers[i];
printf("%s\t%d\t%s\n", reader.name, reader.age, reader.email);
}
}
void search_reader() {
char name[50];
printf("Enter name to search: ");
scanf("%s", name);
for (int i = 0; i < num_readers; i++) {
struct Reader reader = readers[i];
if (strcmp(reader.name, name) == 0) {
printf("Name\tAge\tEmail\n");
printf("%s\t%d\t%s\n", reader.name, reader.age, reader.email);
return;
}
}
printf("Reader not found.\n");
}
void edit_reader() {
char name[50];
printf("Enter name to edit: ");
scanf("%s", name);
for (int i = 0; i < num_readers; i++) {
struct Reader *reader = &readers[i];
if (strcmp(reader->name, name) == 0) {
printf("Enter new name: ");
scanf("%s", reader->name);
printf("Enter new age: ");
scanf("%d", &reader->age);
printf("Enter new email: ");
scanf("%s", reader->email);
printf("Reader edited successfully.\n");
return;
}
}
printf("Reader not found.\n");
}
void delete_reader() {
char name[50];
printf("Enter name to delete: ");
scanf("%s", name);
for (int i = 0; i < num_readers; i++) {
struct Reader *reader = &readers[i];
if (strcmp(reader->name, name) == 0) {
for (int j = i; j < num_readers - 1; j++) {
readers[j] = readers[j + 1];
}
num_readers--;
printf("Reader deleted successfully.\n");
return;
}
}
printf("Reader not found.\n");
}
int main() {
int choice;
do {
printf("\n");
printf("1. Add book\n");
printf("2. Display books\n");
printf("3. Search book\n");
printf("4. Edit book\n");
printf("5. Delete book\n");
printf("6. Add reader\n");
printf("7. Display readers\n");
printf("8. Search reader\n");
printf("9. Edit reader\n");
printf("10. Delete reader\n");
printf("0. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 1:
add_book();
break;
case 2:
display_books();
break;
case 3:
search_book();
break;
case 4:
edit_book();
break;
case 5:
delete_book();
break;
case 6:
add_reader();
break;
case 7:
display_readers();
break;
case 8:
search_reader();
break;
case 9:
edit_reader();
break;
case 10:
delete_reader();
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 0);
return 0;
}
```
该程序使用结构体来存储书籍和读者信息,通过调用不同的函数来实现不同的功能。可以通过不断输入不同的选项来进行不同的操作。
阅读全文