Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream> //Бібліотека для вводу з файлу
  4. using namespace std;
  5.  
  6. class Book {
  7. private:
  8.     string BookName, author;
  9.     int price;
  10. public:
  11.     Book() : BookName(""), author(""), price(0) {} //Конструктор без параметрів(Конструктор за замовчуванням)
  12.      Book(string valueName, string valueAuthor, int valuePrice) { //Конструктор з параметрами
  13.          BookName = valueName;
  14.          author = valueAuthor;
  15.          price = valuePrice;
  16.      }
  17.  
  18.      void input(istream &in) {
  19.          in >> BookName >> author >> price;
  20.      }
  21.      void output(ostream &out) {
  22.          out << BookName << " " << author << " " << price << endl;
  23.      }
  24.      string getAuthor()const {
  25.          return author;
  26.      }
  27.      string getName()const {
  28.          return BookName;
  29.      }
  30.      int getPrice()const {
  31.          return price;
  32.      }
  33. };
  34.  
  35. void sort(Book *mass, int n) {
  36.     Book temp;
  37.     for (int i = 0; i < n; i++) {
  38.         for (int j = 0; j < n - 1 - i; j++) {
  39.             if (mass[j].getAuthor() > mass[j + 1].getAuthor()) {
  40.                 temp = mass[j];
  41.                 mass[j] = mass[j + 1];
  42.                 mass[j + 1] = temp;
  43.             }
  44.         }
  45.     }
  46. }
  47. void print(Book*mass, int n, ostream &out) {
  48.     for (int i = 0; i < n; i++) {
  49.         if (mass[i].getPrice() >= 50 && mass[i].getPrice() <= 100) {
  50.             mass[i].output(out);
  51.         }
  52.     }
  53. }
  54.  
  55. void summary(Book *mass, int n, ostream &out) {
  56.     string myAuthor;
  57.     cin >> myAuthor;
  58.     int sum = 0;
  59.     for (int i = 0; i < n; i++) {
  60.         if (mass[i].getAuthor() == myAuthor) {
  61.             for (int j = 0; j < n - 1; j++) {
  62.                 if (mass[i].getAuthor() == mass[j + 1].getAuthor()) {
  63.                     sum = mass[i].getPrice() + mass[j + 1].getPrice();
  64.                 }
  65.             }
  66.         }
  67.     }
  68.     out << sum << endl;
  69. }
  70.  
  71. int main() {
  72.     int n;
  73.     cin >> n;
  74.     ifstream in("book.txt");
  75.     Book*mass = new Book[n];
  76.     for (int i = 0; i < n; i++) {
  77.         mass[i].input(in);
  78.     }
  79.     sort(mass, n);
  80.     ofstream out("result.txt");
  81.     for (int i = 0; i < n; i++) {
  82.         mass[i].output(out);
  83.     }
  84.     out << "---------------------------------------------------" << endl;
  85.     print(mass, n, out);
  86.     out << "---------------------------------------------------" << endl;
  87.     summary(mass, n, out);
  88.     in.close();
  89.     out.close();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement