From 056803f467069593ecf6e0c56fe9455c1c623576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Thu, 26 Sep 2024 12:59:00 +0800 Subject: [PATCH] fix: book array first var is 0 --- homework1/test4.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homework1/test4.cpp b/homework1/test4.cpp index 4d4f019..185b391 100644 --- a/homework1/test4.cpp +++ b/homework1/test4.cpp @@ -27,14 +27,14 @@ double find_max_price(book *books, int n) { book_and_number max_price_books(book *books, int n) { double max_price = find_max_price(books, n); book_and_number books_and_number; - books_and_number.books=new book[100]; + books_and_number.books = new book[100]; books_and_number.number_of_books = 0; for (int i = 0; i < n; i++) { if (books[i].price == max_price) { 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.books[i].name = books[i].name; + books_and_number.books[i].price = books[i].price; books_and_number.number_of_books++; } } @@ -49,7 +49,7 @@ void input(book *books, int n) { 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++) { + for (int i = 1; i < books_and_number->number_of_books + 1; i++) { cout << books_and_number->books[i].id << " " << books_and_number->books[i].name << " " << fixed << setprecision(2) << books_and_number->books[i].price << endl;