初步完成第四题

This commit is contained in:
2024-09-26 01:44:30 +08:00
parent 7e8bb17c72
commit 4733376e5d
2 changed files with 69 additions and 0 deletions
+68
View File
@@ -0,0 +1,68 @@
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct {
string id;
string name;
double price;
} book;
typedef struct {
book books[];
int number_of_books;
} book_and_number;
double max_price(book *books, int n) {
double max_price = 0;
for (int i = 0; i < n; i++) {
if (books[i].price > max_price) {
max_price = books[i].price;
}
}
return max_price;
}
book_and_number max_price_books(book *books, int n) {
double max_price = max_price(books, n);
book_and_number books_and_number;
book max_price_books[];
books_and_number.number_of_books = 0;
for (int i = 0; i < n; i++) {
if (books[i].price == max_price) {
max_price_books[i] = books[i];
books_and_number.number_of_books++;
}
}
books_and_number.books = &max_price_books;
return books_and_number;
}
void input(book *books, int n) {
for (int i = 0; i < n; i++) {
cin >> books[i].id >> books[i].name >> books[i].price;
}
}
void output(book_and_number books_and_number) {
cout << books_and_number.number_of_books << endl;
for (int i = 0; i < books_and_number.number_of_books; i++) {
cout << books_and_number.books[i].id << "\t" << books_and_number.books[i].name << "\t" << fixed <<
setprecision(2) <<
books_and_number.books[i].price << endl;
}
}
int main() {
book books[100];
int n;
cin >> n;
input(books, n);
max_price(books, n);
book_and_number books_and_number = max_price_books(books, n);
output(books_and_number);
return 0;
}