fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Book
  6. {
  7. private:
  8. string publisher; // book publisher
  9. string name; // name of a book
  10. int quantity; // quantity of a book
  11. double price; // price of one book
  12. public:
  13. Book(): publisher(""), name(""), quantity(1), price(0.0) { }
  14. Book(string publish, string nam, int quantit, double pric):
  15. publisher(publish), name(nam), quantity(quantit), price(pric) { }
  16. ~Book() { }
  17.  
  18. void Set(string pu, string na, int qu, double pr);
  19. void SetName(string pu);
  20. void SetPrice(double pr);
  21.  
  22. string GetPublisher() { return publisher; };
  23. string GetName() { return name; };
  24. int GetQuantity() { return quantity; };
  25. double GetPrice() { return price; };
  26.  
  27.  
  28. bool operator > (const Book & next);
  29.  
  30. };
  31.  
  32. void Book::Set(string pu, string na, int qu, double pr)
  33. {
  34. publisher = pu;
  35. name = na;
  36. quantity = qu;
  37. price = pr;
  38. }
  39.  
  40. void Book::SetName(string na)
  41. {
  42. name= na;
  43. }
  44.  
  45. void Book::SetPrice(double pr)
  46. {
  47. price = pr;
  48. }
  49.  
  50. bool Book::operator > (const Book & next)
  51. {
  52. return (price > next.price);
  53. }
  54. //----------------------
  55.  
  56.  
  57. class Books
  58. {
  59. public:
  60. static const int Cn = 100; // maximum number of books
  61. private:
  62. Book K[Cn]; // books data
  63. int n; // quantity of books
  64. public:
  65. Books(): n(0) { }
  66. ~Books() { }
  67.  
  68. int Get() // returns quantity of books
  69. { return n; };
  70.  
  71. void Set(Book newBook) // add a new book to array of books
  72. { K[n++] = newBook; } // and pluses one to quantity of books
  73.  
  74. Book Get(int ind) // returns object by index
  75. { return K[ind]; }
  76.  
  77. double Sum();
  78. };
  79.  
  80. double Books::Sum()
  81. {
  82. double sum = 0;
  83.  
  84. for (int i = 0; i < n; i++)
  85. sum += K[i].GetPrice();
  86. return sum;
  87. }
  88.  
  89. double MaxPrice(Books & A) // finds maximum price of a book
  90. {
  91. if(A.Get() == 0) return -1;
  92. Book curExpensiveBook = A.Get(0);
  93. for (int i = 1; i < A.Get(); i++)
  94. {
  95. if(A.Get(i) > curExpensiveBook)
  96. curExpensiveBook = A.Get(i);
  97. }
  98. return curExpensiveBook.GetPrice();
  99. }
  100.  
  101.  
  102. int main() {
  103. // your code goes here
  104. Books collection;
  105. collection.Set(Book("John", "Book 1", 1, 10.0));
  106. collection.Set(Book("Jack", "Book 1", 1, 5.0));
  107. collection.Set(Book("John", "Book 1", 1, 20.0));
  108. printf("Max price: %f\n", MaxPrice(collection));
  109. return 0;
  110. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Max price: 20.000000