将第一题模块化

This commit is contained in:
2024-09-26 01:04:28 +08:00
parent f2061faf17
commit c5cd37b2a9
+20 -15
View File
@@ -4,25 +4,30 @@
using namespace std; using namespace std;
typedef struct book { typedef struct book {
string no; string id;
string name; string name;
double price; double price;
} book; } book;
int main() { int input(book *books) {
book books[100]; int length = 0;
int i = 0; for (int length = 0; true; length++) {
cin >> books[length].id >> books[length].name >> books[length].price;
// input if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) {
for (i; true; i++) { return length;
cin >> books[i].no >> books[i].name >> books[i].price;
if (books[i].no == "0" && books[i].name == "0" && books[i].price == 0) {
break;
} }
} }
// output }
cout << i << endl;
for (int j = 0; j < i; j++) { int output(book *books, int length) {
cout << books[j].no << "\t" << books[j].name << "\t" << fixed << setprecision(2) << books[j].price << endl; 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;
} }