Advertisement
KristianIvanov00

Untitled

Feb 19th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. const int MAX_SIZE = 100;
  7.  
  8. struct Book
  9. {
  10. char title[MAX_SIZE];
  11. char author[MAX_SIZE];
  12. char genre[MAX_SIZE];
  13. double price;
  14. int sales;
  15. };
  16.  
  17. void Initialize(Book& b)
  18. {
  19. cout << "Title" << endl;
  20. cin.getline(b.title, MAX_SIZE);
  21.  
  22. cout << "Author" << endl;
  23. cin.getline(b.author, MAX_SIZE);
  24.  
  25. cout << "Genre" << endl;
  26. cin.getline(b.genre, MAX_SIZE);
  27.  
  28. cout << "Price" << endl;
  29. cin >> b.price;
  30.  
  31. cout << "Sales" << endl;
  32. cin >> b.sales;
  33. }
  34.  
  35. void print(const Book& b)
  36. {
  37. cout << endl;
  38. cout << "Title: " << b.title << endl;
  39. cout << "Author: " << b.author << endl;
  40. cout << "Genre: " << b.genre << endl;
  41. cout << "Price: " << b.price << endl;
  42. cout << "Sales: " << b.sales << endl;
  43. }
  44.  
  45. Book& printBookWithLowestPrice(Book* books, size_t count)
  46. {
  47. int lowestPrice = books[0].price;
  48. int bookPosWithLowestPrice = 0;
  49.  
  50. for (int i = 0; i < count; i++)
  51. {
  52. if (books[i].price < lowestPrice)
  53. {
  54. lowestPrice = books[i].price;
  55. bookPosWithLowestPrice = i;
  56. }
  57. }
  58. return books[bookPosWithLowestPrice];
  59. }
  60.  
  61. Book& getBestBook(Book* books, size_t count)
  62. {
  63. int highestSales = books[0].sales;
  64. int bookPosWithHighestSales = 0;
  65.  
  66. for (int i = 1; i < count; i++)
  67. {
  68. if (books[i].sales > highestSales)
  69. {
  70. highestSales = books[i].sales;
  71. bookPosWithHighestSales = i;
  72. }
  73. }
  74.  
  75. return books[bookPosWithHighestSales];
  76. }
  77. int main()
  78. {
  79. const int booksCount = 3;
  80. Book books[booksCount];
  81.  
  82. for (int i = 0; i < booksCount; i++)
  83. {
  84.  
  85. Initialize(books[i]);
  86. cin.get();
  87. }
  88.  
  89.  
  90. Book bestBook = printBookWithLowestPrice(books, booksCount);
  91. print(bestBook);
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement