将所有第一二三题模块化
This commit is contained in:
+1
-1
@@ -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++) {
|
for (int i = 0; i < length; i++) {
|
||||||
cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl;
|
cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl;
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-12
@@ -15,9 +15,9 @@ void swap(book &a, book &b) {
|
|||||||
b = temp;
|
b = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sort(book *arr, int size) {
|
void sort(book *arr, int length) {
|
||||||
for (int i = 0; i < size - 1; i++) {
|
for (int i = 0; i < length - 1; i++) {
|
||||||
for (int j = 0; j < size - 1 - i; j++) {
|
for (int j = 0; j < length - 1 - i; j++) {
|
||||||
if (arr[j].price < arr[j + 1].price) {
|
if (arr[j].price < arr[j + 1].price) {
|
||||||
swap(arr[j], arr[j + 1]);
|
swap(arr[j], arr[j + 1]);
|
||||||
}
|
}
|
||||||
@@ -25,18 +25,26 @@ void sort(book *arr, int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int input(book *books) {
|
||||||
book books[100];
|
int length = 0;
|
||||||
int size = 0;
|
for (int length = 0; true; length++) {
|
||||||
for (size = 0; true; size++) {
|
cin >> books[length].id >> books[length].name >> books[length].price;
|
||||||
cin >> books[size].id >> books[size].name >> books[size].price;
|
if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) {
|
||||||
if (books[size].id == "0" && books[size].name == "0" && books[size].price == 0) {
|
return length;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-5
@@ -15,18 +15,26 @@ void alter_to_110_percent(book *books, int length) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int input(book *books) {
|
||||||
book books[100];
|
|
||||||
int length = 0;
|
int length = 0;
|
||||||
for (length = 0; true; length++) {
|
for (int length = 0; true; length++) {
|
||||||
cin >> books[length].id >> books[length].name >> books[length].price;
|
cin >> books[length].id >> books[length].name >> books[length].price;
|
||||||
if (books[length].id == "0" && books[length].name == "0" && books[length].price == 0) {
|
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++) {
|
for (int i = 0; i < length; i++) {
|
||||||
cout << books[i].id << "\t" << books[i].name << "\t" << fixed << setprecision(2) << books[i].price << endl;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user