From 7e8bb17c722bb7b7deca82663bfc200517ca9985 Mon Sep 17 00:00:00 2001 From: msksbr Date: Thu, 26 Sep 2024 01:08:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=89=80=E6=9C=89=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E4=BA=8C=E4=B8=89=E9=A2=98=E6=A8=A1=E5=9D=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework1/test1.cpp | 2 +- homework1/test2.cpp | 32 ++++++++++++++++++++------------ homework1/test3.cpp | 18 +++++++++++++----- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/homework1/test1.cpp b/homework1/test1.cpp index 6ac12db..6646467 100644 --- a/homework1/test1.cpp +++ b/homework1/test1.cpp @@ -19,7 +19,7 @@ int input(book *books) { } } -int output(book *books, int length) { +void output(book *books, int length) { for (int i = 0; i < length; i++) { cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl; } diff --git a/homework1/test2.cpp b/homework1/test2.cpp index 23a4c35..12ec9c1 100644 --- a/homework1/test2.cpp +++ b/homework1/test2.cpp @@ -15,9 +15,9 @@ void swap(book &a, book &b) { b = temp; } -void sort(book *arr, int size) { - for (int i = 0; i < size - 1; i++) { - for (int j = 0; j < size - 1 - i; j++) { +void sort(book *arr, int length) { + for (int i = 0; i < length - 1; i++) { + for (int j = 0; j < length - 1 - i; j++) { if (arr[j].price < arr[j + 1].price) { swap(arr[j], arr[j + 1]); } @@ -25,18 +25,26 @@ void sort(book *arr, int size) { } } -int main() { - book books[100]; - int size = 0; - for (size = 0; true; size++) { - cin >> books[size].id >> books[size].name >> books[size].price; - if (books[size].id == "0" && books[size].name == "0" && books[size].price == 0) { - break; +int input(book *books) { + int length = 0; + for (int length = 0; true; length++) { + cin >> books[length].id >> books[length].name >> books[length].price; + if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { + return length; } } - sort(books, size); - for (int i = 0; i < size; i++) { +} + +void output(book *books, int length) { + for (int i = 0; i < length; i++) { cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl; } +} + +int main() { + book books[100]; + int length = input(books); + sort(books, length); + output(books, length); return 0; } diff --git a/homework1/test3.cpp b/homework1/test3.cpp index bdc1756..0b52054 100644 --- a/homework1/test3.cpp +++ b/homework1/test3.cpp @@ -15,18 +15,26 @@ void alter_to_110_percent(book *books, int length) { } } -int main() { - book books[100]; +int input(book *books) { int length = 0; - for (length = 0; true; length++) { + for (int length = 0; true; length++) { cin >> books[length].id >> books[length].name >> books[length].price; if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) { - break; + return length; } } - alter_to_110_percent(books, length); +} + +void output(book *books, int length) { for (int i = 0; i < length; i++) { cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl; } +} + +int main() { + book books[100]; + int length = input(books); + alter_to_110_percent(books, length); + output(books, length); return 0; }