图书管理系统借阅图书c语言
时间: 2023-12-27 16:25:35 浏览: 75
根据提供的引用内容,以下是一个简单的C语言图书借阅系统的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct {
char code[10];
char title[100];
int quantity;
int status; // 0表示可借阅,1表示不可借阅
} Book;
Book library[MAX_BOOKS];
int numBooks = 0;
void addBook(char code[], char title[], int quantity) {
if (numBooks < MAX_BOOKS) {
strcpy(library[numBooks].code, code);
strcpy(library[numBooks].title, title);
library[numBooks].quantity = quantity;
library[numBooks].status = 0;
numBooks++;
printf("图书添加成功!\n");
} else {
printf("图书库已满,无法添加新图书!\n");
}
}
void borrowBook(char code[]) {
int i;
for (i = 0; i < numBooks; i++) {
if (strcmp(library[i].code, code) == 0) {
if (library[i].quantity > 0) {
library[i].quantity--;
if (library[i].quantity == 0) {
library[i].status = 1;
}
printf("借阅成功!\n");
return;
} else {
printf("该图书已被借完!\n");
return;
}
}
}
printf("未找到该图书!\n");
}
void displayBooks() {
int i;
printf("图书编码\t图书名称\t库存数量\t借阅状态\n");
for (i = 0; i < numBooks; i++) {
printf("%s\t\t%s\t\t%d\t\t%s\n", library[i].code, library[i].title, library[i].quantity, library[i].status == 0 ? "可借阅" : "不可借阅");
}
}
int main() {
int choice;
char code[10];
char title[100];
int quantity;
while (1) {
printf("\n图书管理系统\n");
printf("1. 添加图书\n");
printf("2. 借阅图书\n");
printf("3. 显示图书\n");
printf("4. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入图书编码:");
scanf("%s", code);
printf("请输入图书名称:");
scanf("%s", title);
printf("请输入库存数量:");
scanf("%d", &quantity);
addBook(code, title, quantity);
break;
case 2:
printf("请输入要借阅的图书编码:");
scanf("%s", code);
borrowBook(code);
break;
case 3:
displayBooks();
break;
case 4:
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
阅读全文