Advertisement
Guest User

Untitled

a guest
Nov 7th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<fstream>
  4. #include<string>
  5. #include<Windows.h>
  6. using namespace std;
  7.  
  8. struct book
  9. {
  10.   char name[100]{ 0 };
  11.   unsigned year;
  12.   unsigned circulation;
  13. };
  14.  
  15. int count_if(book* b, int sz, int from, int to)
  16. {
  17.   int cn = 0;
  18.   for(int i = 0; i < sz; ++i)
  19.   {
  20.     if(b[i].year >= from && b[i].year <= to)
  21.     {
  22.       ++cn;
  23.     }
  24.   }
  25.   return cn;
  26. }
  27.  
  28. book* get_books_list(book* arr, int n, int from, int to, int& out_sz)
  29. {
  30.   out_sz = count_if(arr, n, from, to);
  31.   if(out_sz == 0)
  32.   {
  33.     return nullptr;
  34.   }
  35.  
  36.   book* array = new book[out_sz];
  37.   int idx = 0;
  38.  
  39.   for(int i = 0; i < n; ++i)
  40.   {
  41.     if(arr[i].year >= from && arr[i].year <= to)
  42.     {
  43.       strcpy_s(array[idx].name, arr[i].name);
  44.       array[idx].year = arr[i].year;
  45.       array[idx].circulation = arr[i].circulation;
  46.       ++idx;
  47.     }
  48.   }
  49.   return array;
  50. }
  51.  
  52. int main()
  53. {
  54.   SetConsoleCP(1251);
  55.   SetConsoleOutputCP(1251);
  56.  
  57.   string kng = "TextFile1.txt";
  58.   int N;
  59.   book *arr;
  60.   int otkr;
  61.   cout << "ВЫБЕРИТЕ СПОСОБ ВВОДА. 0 - С КЛАВИАТУРЫ, 1 - ИЗ ФАЙЛА  \n";
  62.   cin >> otkr;
  63.   if(otkr == 0)
  64.   {
  65.     cout << "введите размер массива \n";
  66.     cin >> N;
  67.     arr = new book[N];
  68.     for(int i = 0; i < N; ++i)
  69.     {
  70.       cout << "Введите название книги\n";
  71.       cin >> arr[i].name;
  72.       cout << "Введите тираж \n";
  73.       cin >> arr[i].circulation;
  74.       cout << "Введите год издания \n";
  75.       cin >> arr[i].year;
  76.     }
  77.  
  78.     for(int i = 0; i < N; ++i)
  79.     {
  80.       cout << arr[i].name << "  " << arr[i].circulation << "  " << arr[i].year << endl;
  81.     }
  82.  
  83.   }
  84.   else
  85.   {
  86.     fstream knigi;
  87.     knigi.open("TextFile1.txt");
  88.     knigi >> N;
  89.     arr = new book[N];
  90.     for(int i = 0; i < N; i++)
  91.     {
  92.       knigi >> arr[i].name >> arr[i].circulation >> arr[i].year;
  93.     }
  94.   }
  95.  
  96.   int sz = 0;
  97.  
  98.   book* books = get_books_list(arr, N, 2000, 2010, sz);
  99.  
  100.   delete[] arr;
  101.  
  102.   if(books)
  103.   {
  104.     cout << "НАЗВАНИЕ И ТИРАЖ КНИГ С ИЗДАННЫХ С 2000-2010 ГГ:\n";
  105.     for(int i = 0; i < sz; ++i)
  106.     {
  107.       cout << books[i].name << "  " << books[i].circulation << "\n";
  108.     }
  109.     delete[] books;
  110.   }
  111.   else
  112.   {
  113.     cout << "ТАКИХ НЕТ.";
  114.   }
  115.   system("pause");
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement