Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int MAX_SIZE = 100;
- struct Book
- {
- char title[MAX_SIZE];
- char author[MAX_SIZE];
- char genre[MAX_SIZE];
- double price;
- int sales;
- };
- void Initialize(Book& b)
- {
- cout << "Title" << endl;
- cin.getline(b.title, MAX_SIZE);
- cout << "Author" << endl;
- cin.getline(b.author, MAX_SIZE);
- cout << "Genre" << endl;
- cin.getline(b.genre, MAX_SIZE);
- cout << "Price" << endl;
- cin >> b.price;
- cout << "Sales" << endl;
- cin >> b.sales;
- }
- void print(const Book& b)
- {
- cout << endl;
- cout << "Title: " << b.title << endl;
- cout << "Author: " << b.author << endl;
- cout << "Genre: " << b.genre << endl;
- cout << "Price: " << b.price << endl;
- cout << "Sales: " << b.sales << endl;
- }
- Book& printBookWithLowestPrice(Book* books, size_t count)
- {
- int lowestPrice = books[0].price;
- int bookPosWithLowestPrice = 0;
- for (int i = 0; i < count; i++)
- {
- if (books[i].price < lowestPrice)
- {
- lowestPrice = books[i].price;
- bookPosWithLowestPrice = i;
- }
- }
- return books[bookPosWithLowestPrice];
- }
- Book& getBestBook(Book* books, size_t count)
- {
- int highestSales = books[0].sales;
- int bookPosWithHighestSales = 0;
- for (int i = 1; i < count; i++)
- {
- if (books[i].sales > highestSales)
- {
- highestSales = books[i].sales;
- bookPosWithHighestSales = i;
- }
- }
- return books[bookPosWithHighestSales];
- }
- int main()
- {
- const int booksCount = 3;
- Book books[booksCount];
- for (int i = 0; i < booksCount; i++)
- {
- Initialize(books[i]);
- cin.get();
- }
- Book bestBook = printBookWithLowestPrice(books, booksCount);
- print(bestBook);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement