Advertisement
cacodemon665

Лаба 7 Вариант 8

Nov 27th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include "pch.h" //для версий вижлы старше последних версий 2017 здесь должно быть #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct BookData
  7. {
  8.     int id;
  9.     char author[50];
  10.     char title[50];
  11.     int year;
  12.     char publisher[50];
  13.     int page_cnt;
  14. };
  15.  
  16.  
  17. int main()
  18. {
  19.     int n;
  20.     cout << "Enter number of books:";
  21.     cin >> n;
  22.  
  23.     BookData * data = new BookData[n];
  24.  
  25.  
  26.     for (int i = 0; i < n; i++)
  27.     {
  28.         cout << i + 1 << endl;
  29.  
  30.         cout << "Enter id:";
  31.         cin >> data[i].id;
  32.         cin.ignore(1, '\n');
  33.         cout << "Enter author:";
  34.         cin.getline(data[i].author, 50);
  35.         cout << "Enter title:";
  36.         cin.getline(data[i].title, 50);
  37.         cout << "Enter year:";
  38.         cin >> data[i].year;
  39.         cin.ignore(1, '\n');
  40.         cout << "Enter publisher:";
  41.         cin.getline(data[i].publisher, 50);
  42.         cout << "Enter number of pages:";
  43.         cin >> data[i].page_cnt;
  44.     }
  45.  
  46.     int year;
  47.     cout << "Enter year to filter:";
  48.     cin >> year;
  49.  
  50.     BookData * filtered_data = new BookData[n];
  51.     int filtered_cnt = 0;
  52.  
  53.     //filter
  54.     for (int i = 0; i < n; i++)
  55.     {
  56.         if (data[i].year > year)
  57.         {
  58.             filtered_data[filtered_cnt++] = data[i];
  59.         }
  60.     }
  61.  
  62.     //sort
  63.     for (int i = filtered_cnt - 1; i >= 0; i--)
  64.     {
  65.         for (int j = 0; j < i; j++)
  66.         {
  67.             if (strcmp(filtered_data[j].author, filtered_data[j + 1].author) > 0)
  68.             {
  69.                 BookData temp = filtered_data[j];
  70.                 filtered_data[j] = filtered_data[j + 1];
  71.                 filtered_data[j + 1] = temp;
  72.             }
  73.         }
  74.     }
  75.  
  76.     //print result
  77.  
  78.     for (int i = 0; i < filtered_cnt; i++)
  79.     {
  80.         cout << i + 1 << endl;
  81.  
  82.         //cin.ignore(1, '\n');
  83.  
  84.         cout << "Id: " << filtered_data[i].id << endl;
  85.         cout << "Author: " << filtered_data[i].author << endl;
  86.         cout << "Title: " << filtered_data[i].title << endl;
  87.         cout << "Year: " << filtered_data[i].year << endl;
  88.         cout << "Publisher: " << filtered_data[i].publisher << endl;
  89.         cout << "Number of pages:" << filtered_data[i].page_cnt << endl;
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement