怎么写c语言音像图书租借管理系统
时间: 2023-11-29 18:04:31 浏览: 148
以下是一个简单的C语言音像图书租借管理系统的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_CUSTOMERS 100
typedef struct {
char title[50];
char author[50];
int year;
int available;
} Book;
typedef struct {
char name[50];
char address[50];
char phone[15];
Book *rented_books[5];
} Customer;
Book books[MAX_BOOKS];
Customer customers[MAX_CUSTOMERS];
int num_books = 0;
int num_customers = 0;
void add_book();
void list_books();
void add_customer();
void list_customers();
void rent_book();
void return_book();
int main() {
int choice;
do {
printf("1. Add book\n");
printf("2. List books\n");
printf("3. Add customer\n");
printf("4. List customers\n");
printf("5. Rent book\n");
printf("6. Return book\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
list_books();
break;
case 3:
add_customer();
break;
case 4:
list_customers();
break;
case 5:
rent_book();
break;
case 6:
return_book();
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 0);
return 0;
}
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("Maximum number of books reached!\n");
return;
}
Book book;
printf("Enter title: ");
scanf("%s", book.title);
printf("Enter author: ");
scanf("%s", book.author);
printf("Enter year: ");
scanf("%d", &book.year);
book.available = 1;
books[num_books++] = book;
printf("Book added successfully!\n");
}
void list_books() {
printf("Title\tAuthor\tYear\tAvailable\n");
for (int i = 0; i < num_books; i++) {
printf("%s\t%s\t%d\t%s\n", books[i].title, books[i].author, books[i].year, books[i].available ? "Yes" : "No");
}
}
void add_customer() {
if (num_customers >= MAX_CUSTOMERS) {
printf("Maximum number of customers reached!\n");
return;
}
Customer customer;
printf("Enter name: ");
scanf("%s", customer.name);
printf("Enter address: ");
scanf("%s", customer.address);
printf("Enter phone: ");
scanf("%s", customer.phone);
for (int i = 0; i < 5; i++) {
customer.rented_books[i] = NULL;
}
customers[num_customers++] = customer;
printf("Customer added successfully!\n");
}
void list_customers() {
printf("Name\tAddress\tPhone\n");
for (int i = 0; i < num_customers; i++) {
printf("%s\t%s\t%s\n", customers[i].name, customers[i].address, customers[i].phone);
}
}
void rent_book() {
char book_title[50];
printf("Enter book title: ");
scanf("%s", book_title);
Book *book = NULL;
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].title, book_title) == 0) {
book = &books[i];
break;
}
}
if (book == NULL) {
printf("Book not found!\n");
return;
}
if (!book->available) {
printf("Book is not available for rent!\n");
return;
}
char customer_name[50];
printf("Enter customer name: ");
scanf("%s", customer_name);
Customer *customer = NULL;
for (int i = 0; i < num_customers; i++) {
if (strcmp(customers[i].name, customer_name) == 0) {
customer = &customers[i];
break;
}
}
if (customer == NULL) {
printf("Customer not found!\n");
return;
}
for (int i = 0; i < 5; i++) {
if (customer->rented_books[i] == NULL) {
customer->rented_books[i] = book;
book->available = 0;
printf("Book rented successfully!\n");
return;
}
}
printf("Customer has reached maximum number of rented books!\n");
}
void return_book() {
char book_title[50];
printf("Enter book title: ");
scanf("%s", book_title);
Book *book = NULL;
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].title, book_title) == 0) {
book = &books[i];
break;
}
}
if (book == NULL) {
printf("Book not found!\n");
return;
}
if (book->available) {
printf("Book is already available!\n");
return;
}
char customer_name[50];
printf("Enter customer name: ");
scanf("%s", customer_name);
Customer *customer = NULL;
for (int i = 0; i < num_customers; i++) {
if (strcmp(customers[i].name, customer_name) == 0) {
customer = &customers[i];
break;
}
}
if (customer == NULL) {
printf("Customer not found!\n");
return;
}
for (int i = 0; i < 5; i++) {
if (customer->rented_books[i] == book) {
customer->rented_books[i] = NULL;
book->available = 1;
printf("Book returned successfully!\n");
return;
}
}
printf("Customer has not rented this book!\n");
}
```
这个系统提供了以下功能:
- 添加图书
- 列出图书
- 添加客户
- 列出客户
- 租借图书
- 归还图书
每本图书包含标题、作者、年份、可用性等信息。每个客户包含名称、地址、电话和租用的图书列表。租用图书时,用户必须提供其名称和所需的图书标题。如果该书可用,则将其标记为不可用,并将其添加到客户的租用列表中。还书时,用户必须提供其名称和要归还的图书标题。如果该书已被租用,则将其标记为可用,并将其从客户的租用列表中删除。
阅读全文