图书管理系统c语言教学
时间: 2023-12-08 09:06:56 浏览: 72
基于提供的引用内容,我们可以通过C语言实现一个简单的图书管理系统。下面是一个简单的实现方法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大书籍数量
// 书籍结构体
typedef struct {
char id[20]; // 书籍编号
char name[50]; // 书籍名称
char category[20]; // 书籍类别
int stock; // 库存数量
int borrowed; // 已借出数量
} Book;
// 书籍数组
Book books[MAX_BOOKS];
// 当前书籍数量
int bookCount = 0;
// 添加书籍
void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("已达到最大书籍数量,无法添加!\n");
return;
}
Book book;
printf("请输入书籍编号:");
scanf("%s", book.id);
printf("请输入书籍名称:");
scanf("%s", book.name);
printf("请输入书籍类别:");
scanf("%s", book.category);
printf("请输入库存数量:");
scanf("%d", &book.stock);
printf("请输入已借出数量:");
scanf("%d", &book.borrowed);
books[bookCount++] = book; printf("添加成功!\n");
}
// 删除书籍
void deleteBook() {
char id[20];
printf("请输入要删除的书籍编号:");
scanf("%s", id);
int index = -1;
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该书籍!\n");
return;
}
for (int i = index; i < bookCount - 1; i++) {
books[i] = books[i + 1];
}
bookCount--;
printf("删除成功!\n");
}
// 查找书籍
void searchBook() {
char id[20];
printf("请输入要查找的书籍编号:");
scanf("%s", id);
int index = -1;
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该书籍!\n");
return;
}
printf("书籍编号:%s\n", books[index].id);
printf("书籍名称:%s\n", books[index].name);
printf("书籍类别:%s\n", books[index].category);
printf("库存数量:%d\n", books[index].stock);
printf("已借出数量:%d\n", books[index].borrowed);
}
// 显示所有书籍
void showAllBooks() {
printf("书籍编号\t书籍名称\t书籍类别\t库存数量\t已借出数量\n");
for (int i = 0; i < bookCount; i++) {
printf("%s\t%s\t%s\t%d\t%d\n", books[i].id, books[i].name, books[i].category, books[i].stock, books[i].borrowed);
}
}
// 借书
void borrowBook() {
char id[20];
printf("请输入要借阅的书籍编号:");
scanf("%s", id);
int index = -1;
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该书籍!\n");
return;
}
if (books[index].stock <= 0) {
printf("该书籍库存不足,无法借阅!\n");
return;
}
books[index].stock--;
books[index].borrowed++;
printf("借阅成功!\n");
}
// 还书
void returnBook() {
char id[20];
printf("请输入要归还的书籍编号:");
scanf("%s", id);
int index = -1;
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该书籍!\n");
return;
}
if (books[index].borrowed <= 0) {
printf("该书籍已全部归还,无需操作!\n");
return;
}
books[index].stock++;
books[index].borrowed--;
printf("归还成功!\n");
}
// 保存书籍到文件
void saveBooksToFile() {
FILE *fp = fopen("books.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
for (int i = 0; i < bookCount; i++) {
fprintf(fp, "%s %s %s %d %d\n", books[i].id, books[i].name, books[i].category, books[i].stock, books[i].borrowed);
}
fclose(fp);
printf("保存成功!\n");
}
// 从文件中读取书籍
void loadBooksFromFile() {
FILE *fp = fopen("books.txt", "r");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
while (!feof(fp)) {
Book book;
fscanf(fp, "%s %s %s %d %d\n", book.id, book.name, book.category, &book.stock, &book.borrowed);
if (strlen(book.id) > 0) {
books[bookCount++] = book;
}
}
fclose(fp);
printf("读取成功!\n");
}
int main() {
int choice;
loadBooksFromFile();
while (1) {
printf("请选择操作:\n");
printf("1.添加书籍\n");
printf("2.删除书籍\n");
printf("3.查找书籍\n");
printf("4.显示所有书籍\n");
printf("5.借阅书籍\n");
printf("6.归还书籍\n");
printf("7.保存书籍到文件\n");
printf("0.退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
showAllBooks();
break;
case 5:
borrowBook();
break;
case 6:
returnBook();
break;
case 7:
saveBooksToFile();
break;
case 0:
saveBooksToFile();
printf("谢谢使用!\n");
return 0;
default:
printf("无效的选择!\n");
break;
}
}
return 0;
}
```
阅读全文