#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
char author[30];
int pages;
};
const char* findMostPagesBook(struct Book books[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (books[i].pages > books[maxIndex].pages) {
maxIndex = i;
}
}
return books[maxIndex].title;
}
int main() {
struct Book library[] = {
{"C Primer Plus", "Stephen Prata", 960},
{"Effective C++", "Scott Meyers", 336},
{"Clean Code", "Robert C. Martin", 464}
};
int size = sizeof(library) / sizeof(library[0]);
const char* mostPagesBookTitle = findMostPagesBook(library, size);
printf("页数最多的书的标题: %s\n", mostPagesBookTitle);
return 0;
}
- 结构体定义:定义了
struct Book
结构体,包含title
(书名)、author
(作者)和pages
(页数)成员。
- 函数
findMostPagesBook
:该函数接收struct Book
类型的数组和数组大小作为参数,通过遍历数组找到页数最多的书,并返回该书的标题。
main
函数:创建了一个struct Book
类型的数组library
,调用findMostPagesBook
函数找到页数最多的书的标题,并输出结果。