图书管理系统中可以查找借阅次数最多和最少的借阅次数的代码C语言
时间: 2024-02-25 12:55:12 浏览: 64
以下是一个简单的图书管理系统中查找借阅次数最多和最少的借阅次数的C语言代码示例:
```c
#include <stdio.h>
#define MAX_BOOKS 100
struct Book {
int id;
char title[50];
char author[50];
int borrow_count;
};
int main() {
struct Book books[MAX_BOOKS] = {
{1, "The Great Gatsby", "F. Scott Fitzgerald", 10},
{2, "To Kill a Mockingbird", "Harper Lee", 7},
{3, "1984", "George Orwell", 5},
{4, "Pride and Prejudice", "Jane Austen", 3},
{5, "The Catcher in the Rye", "J.D. Salinger", 2},
};
int num_books = 5;
int max_borrow_count = 0;
int min_borrow_count = 0;
int i;
// Find max and min borrow counts
for (i = 0; i < num_books; i++) {
if (books[i].borrow_count > books[max_borrow_count].borrow_count) {
max_borrow_count = i;
}
if (books[i].borrow_count < books[min_borrow_count].borrow_count) {
min_borrow_count = i;
}
}
// Print results
printf("Book with the most borrows:\n");
printf("Title: %s\n", books[max_borrow_count].title);
printf("Author: %s\n", books[max_borrow_count].author);
printf("Borrow count: %d\n", books[max_borrow_count].borrow_count);
printf("\nBook with the least borrows:\n");
printf("Title: %s\n", books[min_borrow_count].title);
printf("Author: %s\n", books[min_borrow_count].author);
printf("Borrow count: %d\n", books[min_borrow_count].borrow_count);
return 0;
}
```
在这个例子中,我们使用一个包含5本书的结构体数组来表示图书馆的书籍。我们遍历这个数组来查找借阅次数最多和最少的书籍,并打印出它们的标题、作者和借阅次数。
阅读全文