图书管理系统c语言
时间: 2023-07-03 22:08:49 浏览: 36
图书管理系统可以分为两个部分:前端与后端。前端即用户交互界面,后端则负责数据管理。
以下是一个简单的图书管理系统的C语言代码示例:
前端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct Book {
char title[100];
char author[50];
char publisher[50];
int year;
} Book;
void print_menu() {
printf("\n");
printf("1. Add book\n");
printf("2. Remove book\n");
printf("3. Print books\n");
printf("4. Exit\n");
printf("\n");
}
void add_book(Book books[], int *num_books) {
if (*num_books == MAX_BOOKS) {
printf("Error: Maximum number of books reached.\n");
return;
}
Book book;
printf("Title: ");
fgets(book.title, 100, stdin);
book.title[strcspn(book.title, "\n")] = 0; // remove trailing newline
printf("Author: ");
fgets(book.author, 50, stdin);
book.author[strcspn(book.author, "\n")] = 0; // remove trailing newline
printf("Publisher: ");
fgets(book.publisher, 50, stdin);
book.publisher[strcspn(book.publisher, "\n")] = 0; // remove trailing newline
printf("Year: ");
scanf("%d", &book.year);
getchar(); // remove trailing newline
books[*num_books] = book;
(*num_books)++;
}
void remove_book(Book books[], int *num_books) {
if (*num_books == 0) {
printf("Error: No books to remove.\n");
return;
}
char title[100];
printf("Title: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0; // remove trailing newline
int i;
for (i = 0; i < *num_books; i++) {
if (strcmp(books[i].title, title) == 0) {
break;
}
}
if (i == *num_books) {
printf("Error: Book not found.\n");
} else {
for (int j = i; j < *num_books - 1; j++) {
books[j] = books[j + 1];
}
(*num_books)--;
}
}
void print_books(Book books[], int num_books) {
printf("%-50s%-20s%-20s%-10s\n", "Title", "Author", "Publisher", "Year");
for (int i = 0; i < num_books; i++) {
printf("%-50s%-20s%-20s%-10d\n", books[i].title, books[i].author, books[i].publisher, books[i].year);
}
}
int main() {
Book books[MAX_BOOKS];
int num_books = 0;
while (1) {
print_menu();
int choice;
printf("Choice: ");
scanf("%d", &choice);
getchar(); // remove trailing newline
switch (choice) {
case 1:
add_book(books, &num_books);
break;
case 2:
remove_book(books, &num_books);
break;
case 3:
print_books(books, num_books);
break;
case 4:
return 0;
default:
printf("Error: Invalid choice.\n");
}
}
}
```
后端:
由于数据存储方式有多种,这里只提供一种简单的实现方式,即使用文件存储数据。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct Book {
char title[100];
char author[50];
char publisher[50];
int year;
} Book;
void add_book(Book book) {
FILE *file = fopen("books.dat", "ab");
if (file == NULL) {
printf("Error: Could not open file.\n");
return;
}
fwrite(&book, sizeof(Book), 1, file);
fclose(file);
}
void remove_book(char *title) {
FILE *file = fopen("books.dat", "rb");
if (file == NULL) {
printf("Error: Could not open file.\n");
return;
}
Book book;
int found = 0;
FILE *temp = fopen("temp.dat", "wb");
while (fread(&book, sizeof(Book), 1, file) == 1) {
if (strcmp(book.title, title) != 0) {
fwrite(&book, sizeof(Book), 1, temp);
} else {
found = 1;
}
}
fclose(file);
fclose(temp);
remove("books.dat");
rename("temp.dat", "books.dat");
if (!found) {
printf("Error: Book not found.\n");
}
}
void print_books() {
FILE *file = fopen("books.dat", "rb");
if (file == NULL) {
printf("Error: Could not open file.\n");
return;
}
Book book;
printf("%-50s%-20s%-20s%-10s\n", "Title", "Author", "Publisher", "Year");
while (fread(&book, sizeof(Book), 1, file) == 1) {
printf("%-50s%-20s%-20s%-10d\n", book.title, book.author, book.publisher, book.year);
}
fclose(file);
}
int main() {
while (1) {
printf("\n");
printf("1. Add book\n");
printf("2. Remove book\n");
printf("3. Print books\n");
printf("4. Exit\n");
printf("\n");
int choice;
printf("Choice: ");
scanf("%d", &choice);
getchar(); // remove trailing newline
switch (choice) {
case 1: {
Book book;
printf("Title: ");
fgets(book.title, 100, stdin);
book.title[strcspn(book.title, "\n")] = 0; // remove trailing newline
printf("Author: ");
fgets(book.author, 50, stdin);
book.author[strcspn(book.author, "\n")] = 0; // remove trailing newline
printf("Publisher: ");
fgets(book.publisher, 50, stdin);
book.publisher[strcspn(book.publisher, "\n")] = 0; // remove trailing newline
printf("Year: ");
scanf("%d", &book.year);
getchar(); // remove trailing newline
add_book(book);
break;
}
case 2: {
char title[100];
printf("Title: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0; // remove trailing newline
remove_book(title);
break;
}
case 3:
print_books();
break;
case 4:
return 0;
default:
printf("Error: Invalid choice.\n");
}
}
}
```
这只是一个简单的示例,实际上图书管理系统还有很多细节需要考虑,比如数据校验、数据查询、数据备份等等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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://csdnimg.cn/download_wenku/file_type_ask_c1.png)