图书管理系统c语言
时间: 2023-07-03 14:18:15 浏览: 63
好的,下面是一个简单的图书管理系统的C语言代码示例,主要实现了图书的添加、删除、查询和显示功能。代码如下:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 20
struct Book {
char title[50];
char author[50];
int pages;
float price;
};
struct Book books[MAX_BOOKS];
int num_books = 0;
int find_book(char* title) {
int i;
for (i = 0; i < num_books; i++) {
if (strcmp(books[i].title, title) == 0) {
return i;
}
}
return -1;
}
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("Error: too many books.\n");
return;
}
struct Book b;
printf("Enter book title: ");
scanf("%s", b.title);
printf("Enter author name: ");
scanf("%s", b.author);
printf("Enter number of pages: ");
scanf("%d", &b.pages);
printf("Enter price: ");
scanf("%f", &b.price);
books[num_books] = b;
num_books++;
printf("Book added.\n");
}
void delete_book() {
char title[50];
printf("Enter book title: ");
scanf("%s", title);
int index = find_book(title);
if (index == -1) {
printf("Error: book not found.\n");
return;
}
int i;
for (i = index; i < num_books - 1; i++) {
books[i] = books[i + 1];
}
num_books--;
printf("Book deleted.\n");
}
void search_book() {
char title[50];
printf("Enter book title: ");
scanf("%s", title);
int index = find_book(title);
if (index == -1) {
printf("Error: book not found.\n");
return;
}
struct Book b = books[index];
printf("Title: %s\n", b.title);
printf("Author: %s\n", b.author);
printf("Pages: %d\n", b.pages);
printf("Price: %.2f\n", b.price);
}
void display_books() {
if (num_books == 0) {
printf("No books found.\n");
return;
}
int i;
printf("Title\tAuthor\tPages\tPrice\n");
for (i = 0; i < num_books; i++) {
struct Book b = books[i];
printf("%s\t%s\t%d\t%.2f\n", b.title, b.author, b.pages, b.price);
}
}
int main() {
int choice;
do {
printf("\n1. Add book\n");
printf("2. Delete book\n");
printf("3. Search book\n");
printf("4. Display all books\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
search_book();
break;
case 4:
display_books();
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 5);
return 0;
}
```
这是一个比较简单的示例,只提供了基本功能,实际应用中还需要考虑更多的情况,比如输入验证、文件存储等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)