From c5cd37b2a9737425a250329282d4812f061a86d5 Mon Sep 17 00:00:00 2001 From: msksbr Date: Thu, 26 Sep 2024 01:04:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E7=AC=AC=E4=B8=80=E9=A2=98=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework1/test1.cpp | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/homework1/test1.cpp b/homework1/test1.cpp index f0e6b2c..6ac12db 100644 --- a/homework1/test1.cpp +++ b/homework1/test1.cpp @@ -4,25 +4,30 @@ using namespace std; typedef struct book { - string no; + string id; string name; double price; } book; -int main() { - book books[100]; - int i = 0; - - // input - for (i; true; i++) { - cin >> books[i].no >> books[i].name >> books[i].price; - if (books[i].no == "0" && books[i].name == "0" && books[i].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; } } - // output - cout << i << endl; - for (int j = 0; j < i; j++) { - cout << books[j].no << "\t" << books[j].name << "\t" << fixed << setprecision(2) << books[j].price << endl; +} + +int 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); + output(books, length); + return 0; +}