用C语言写一个图书管理系统,功能包括录入,查询删除,修改,浏览等
时间: 2024-03-24 17:42:05 浏览: 59
好的,我可以为您提供一个基本的图书管理系统的 C 语言代码示例,包括录入、查询、删除、修改和浏览功能。以下是代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct book {
char title[50];
char author[50];
int id;
};
struct book books[MAX_BOOKS];
int num_books = 0;
void add_book()
{
if (num_books >= MAX_BOOKS) {
printf("Error: Maximum number of books reached.\n");
return;
}
printf("Enter book title: ");
scanf("%s", books[num_books].title);
printf("Enter book author: ");
scanf("%s", books[num_books].author);
printf("Enter book ID: ");
scanf("%d", &books[num_books].id);
num_books++;
printf("Book added successfully.\n");
}
void search_book()
{
int id;
printf("Enter book ID: ");
scanf("%d", &id);
for (int i = 0; i < num_books; i++) {
if (books[i].id == id) {
printf("Book title: %s\n", books[i].title);
printf("Book author: %s\n", books[i].author);
printf("Book ID: %d\n", books[i].id);
return;
}
}
printf("Book not found.\n");
}
void delete_book()
{
int id;
printf("Enter book ID: ");
scanf("%d", &id);
for (int i = 0; i < num_books; i++) {
if (books[i].id == id) {
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 modify_book()
{
int id;
printf("Enter book ID: ");
scanf("%d", &id);
for (int i = 0; i < num_books; i++) {
if (books[i].id == id) {
printf("Enter new book title: ");
scanf("%s", books[i].title);
printf("Enter new book author: ");
scanf("%s", books[i].author);
printf("Enter new book ID: ");
scanf("%d", &books[i].id);
printf("Book modified successfully.\n");
return;
}
}
printf("Book not found.\n");
}
void view_books()
{
printf("Total number of books: %d\n", num_books);
for (int i = 0; i < num_books; i++) {
printf("Book title: %s\n", books[i].title);
printf("Book author: %s\n", books[i].author);
printf("Book ID: %d\n", books[i].id);
}
}
int main()
{
int choice;
while (1) {
printf("\n");
printf("1. Add book\n");
printf("2. Search book\n");
printf("3. Delete book\n");
printf("4. Modify book\n");
printf("5. View books\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
search_book();
break;
case 3:
delete_book();
break;
case 4:
modify_book();
break;
case 5:
view_books();
break;
case 6:
exit(0);
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
这是一个简单的命令行程序,可以通过菜单来执行不同的功能。您可以根据自己的需要进行修改和扩展,例如添加借阅和归还功能等。
阅读全文