修复内存不可达

This commit is contained in:
2024-09-26 12:43:40 +08:00
parent 7b0874026d
commit aee105ec51
+10 -7
View File
@@ -27,11 +27,14 @@ double find_max_price(book *books, int n) {
book_and_number max_price_books(book *books, int n) { book_and_number max_price_books(book *books, int n) {
double max_price = find_max_price(books, n); double max_price = find_max_price(books, n);
book_and_number books_and_number; book_and_number books_and_number;
books_and_number.books=new book[100];
books_and_number.number_of_books = 0; books_and_number.number_of_books = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
if (books[i].price == max_price) { if (books[i].price == max_price) {
books_and_number.books[i] = books[i]; books_and_number.books[i].id = books[i].id;
books_and_number.books[i].name=books[i].name;
books_and_number.books[i].price=books[i].price;
books_and_number.number_of_books++; books_and_number.number_of_books++;
} }
} }
@@ -44,12 +47,12 @@ void input(book *books, int n) {
} }
} }
void output(book_and_number books_and_number) { void output(book_and_number *books_and_number) {
cout << books_and_number.number_of_books << endl; cout << books_and_number->number_of_books << endl;
for (int i = 0; i < books_and_number.number_of_books; i++) { for (int i = 0; i < books_and_number->number_of_books; i++) {
cout << books_and_number.books[i].id << " " << books_and_number.books[i].name << " " << fixed << cout << books_and_number->books[i].id << " " << books_and_number->books[i].name << " " << fixed <<
setprecision(2) << setprecision(2) <<
books_and_number.books[i].price << endl; books_and_number->books[i].price << endl;
} }
} }
@@ -60,6 +63,6 @@ int main() {
input(books, n); input(books, n);
find_max_price(books, n); find_max_price(books, n);
book_and_number books_and_number = max_price_books(books, n); book_and_number books_and_number = max_price_books(books, n);
output(books_and_number); output(&books_and_number);
return 0; return 0;
} }